To connect Java to Apache Solr, use SolrJ: add its Maven dependency, create a SolrClient, send documents that match your collection’s schema, and submit queries with SolrQuery. This guide targets Apache Solr 10.0 and SolrJ 10.0.0. The Solr server requires Java 21 or later; SolrJ client libraries require Java 17 or later, so those requirements apply to separate server and client processes.
1. Add SolrJ and choose the client version
Solr communicates with applications over HTTP. SolrJ wraps request construction and response parsing in Java APIs; direct HTTP clients are also possible. The Apache Solr Reference Guide describes SolrJ as an API for Java and other JVM applications to talk to Solr: SolrJ :: Apache Solr Reference Guide. This tutorial uses the guide’s SolrJ 10.0.0 Maven coordinate:
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>10.0.0</version>
</dependency>
The dependency above supplies HttpJdkSolrClient. Add solr-solrj-jetty if you choose a Jetty-based client. Solr 10 no longer brings optional modules such as ZooKeeper into the SolrJ Maven POM automatically; direct ZooKeeper access and Streaming Expressions require their optional modules. Check the release-specific guide before adding them: Major Changes in Solr 10.
The live /latest/ documentation can change as Solr releases. The 10.0 guide and examples are the version basis here; code from earlier SolrJ releases may not compile unchanged. The guide describes Solr 9.x as the stable line in the consulted text, while its navigation also lists Solr 9.11-beta and Solr 10.0. If you maintain an older server, use that release’s guide and matching client coordinates rather than assuming SolrJ 10 compatibility. Solr 10’s server minimum is Java 21, while its SolrJ client minimum is Java 17; see the Solr 10 upgrade notes.
Quick wins for a faster PC:
Scan for outdated or missing drivers - takes under a minuteDriver Scan →Repair Windows errors before they cause bigger problemsFix Now →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →2. Choose a SolrClient for your deployment
SolrClient is the central SolrJ abstraction for communicating with Solr and managing client configuration. Select its implementation according to deployment topology and workload, not an assumed universal speed ranking.
| Client | Best fit | Behavior and dependencies |
|---|---|---|
HttpJdkSolrClient |
General-purpose HTTP access | Uses the JDK HTTP client and is available from the base solr-solrj artifact without an additional client dependency. |
HttpJettySolrClient |
General-purpose access where its asynchronous features are useful | Supports asynchronous/non-blocking operation and HTTP/1.1 and HTTP/2; the current guide calls it the most used and tested option. Include solr-solrj-jetty. |
CloudSolrClient |
SolrCloud | Uses cluster state to route requests and can distribute update documents to nodes. |
ConcurrentUpdateJettySolrClient |
Indexing-heavy workloads | Buffers documents before sending larger batches; it uses the Jetty client module. |
LBSolrClient |
Internal client implementation | A failover/load-balancing abstraction for clients targeting multiple nodes, rather than the usual application-level starting point. |
These distinctions are described in the SolrJ client documentation; they are not comparative benchmark results.
3. Connect to a Solr collection
For URL-based builders, give the client the Solr root URL, ordinarily ending in /solr, and optionally set a default collection. In Solr 10, do not supply a collection-specific URL where a root URL is required. The collection name can then be passed explicitly per request, as in the examples below.
Rank #2
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.impl.HttpJdkSolrClient;
String solrRootUrl = "http://localhost:8983/solr";
SolrClient client = new HttpJdkSolrClient.Builder(solrRootUrl).build();
String collection = "products";
Replace the example address and collection with values for your deployment. Configure connection and read timeouts to suit the application and network; the documentation shows how to configure clients, but does not establish universal production timeout values. Keep the client lifecycle in application-level configuration rather than creating a new client for every request. SolrJ client builders and configuration are covered in the official SolrJ guide.
Connecting to SolrCloud
For SolrCloud, use CloudSolrClient when you want SolrJ to use cluster state for routing. The Solr URLs given to its builder provide cluster layout and health information; they are not simply a collection endpoint. Current Solr 10 notes encourage Solr URLs over direct ZooKeeper connections and deprecate the ZooKeeper Hosts constructor. Follow the Solr 10 upgrade notes for the version-specific builder path.
4. Match your documents to the collection schema
Solr stores documents as named fields. A field such as id is commonly configured as the unique key, analogous to a database primary key. The collection’s schema determines which fields are accepted or mapped, and how configured field types analyze tokenized text. Unknown fields may be ignored or matched by a dynamic-field rule. Confirm the collection schema before sending application data; a Java object’s property names alone do not define Solr’s field behavior. See the Documents, Fields, and Schema Design guide.
For example, this document has an ID, a title, and body text. It is a syntax demonstration: the target collection must support these fields.
import org.apache.solr.common.SolrInputDocument;
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "product-1042");
doc.addField("title", "Compact travel mug");
doc.addField("body", "Insulated stainless-steel mug with a spill-resistant lid.");
Use a stable source identifier if a later indexing request should replace the same logical record. The example uses a stable value; randomly generating a new ID for each update would instead create a different document. Data can also reach Solr through imports such as CSV, XML, database tables, or files including Word and PDF. Solr Cell uses Apache Tika for document extraction, while a custom Java application offers control over ingestion and transformation; see the Solr document and field guide.
PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minute5. Index documents without committing each record
Use SolrClient.add() to send a SolrInputDocument. For clarity, this example adds one document; it demonstrates the API shape, not a recommended production batch size.
Rank #4
import java.io.IOException;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.common.SolrInputDocument;
try {
client.add(collection, doc);
} catch (SolrServerException | IOException e) {
throw new RuntimeException("Could not index document", e);
}
For normal workloads, group documents into larger batches rather than making a separate round trip for each record. Solr administrators should configure autocommit for the deployment; application code should not hard-commit after every document. Explicit commit, delete, and optimize are available operations, not mandatory steps in every indexing request. The SolrJ guide discusses adding documents and commit configuration.
SolrJ also supports annotated beans: annotate bean properties with @Field, then use addBean() to index an instance. This can reduce manual field mapping when the application model aligns with the collection schema.
6. Query Solr and map the response
Build a SolrQuery with the query expression, requested fields, sort order, and a bounded row count. Then call client.query(collection, query). Solr 10 moved SolrQuery packages, so import it from the 10.0 package rather than copying an older example blindly.
Recommended Free Tools
Best Value
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
SolrQuery query = new SolrQuery();
query.setQuery("title:mug");
query.setFields("id", "title");
query.setSort("id", SolrQuery.ORDER.asc);
query.setRows(20);
QueryResponse response = client.query(collection, query);
System.out.println("Matches: " + response.getResults().getNumFound());
for (SolrDocument result : response.getResults()) {
System.out.println(result.getFieldValue("id") + ": "
+ result.getFieldValue("title"));
}
The row limit bounds the returned page; numFound reports the total matching count. Request only the fields the application needs. Solr query syntax, escaping, validation, authorization, and the design of a public API are application-specific decisions: the SolrJ API does not by itself establish a safe public endpoint policy.
For bean mapping, annotate Java properties with @Field, then use QueryResponse.getBeans(YourType.class) to convert results. This is convenient when the bean’s field mapping matches the schema; use SolrDocument when a dynamic or partial field result is more suitable. SolrJ query construction and response mapping are documented in the SolrJ reference.
7. Account for HTTP, topology, and workload
“HTTP is the fundamental protocol used between client applications and Solr,” according to the Client APIs :: Apache Solr Reference Guide. SolrJ is one way to use that protocol from Java; a direct HTTP client remains possible when its request and response handling suits the application. Solr operations include querying, indexing, deleting, committing, and optimizing, but they do not form a required sequence for every request.
Timeouts, batch size, schema design, query fields, and cluster topology depend on the workload and deployment. The client distinctions above describe supported usage patterns, not proof that one implementation is faster in a given environment. Measure the choices against your own data, traffic, and Solr configuration.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

