To build a Java search layer for Apache Solr, use SolrJ: add the version-matched SolrJ dependency, create a SolrClient, send documents that match your collection’s schema, and query them with SolrQuery. This tutorial targets Apache Solr 10.0 and SolrJ 10.0.0. Solr itself communicates with applications over HTTP; SolrJ packages request construction and response parsing into Java APIs.
Choose compatible Solr and Java versions
The examples below target Solr 10.0 and org.apache.solr:solr-solrj:10.0.0, the coordinate shown in the current Apache SolrJ guide. Solr 10’s server requires Java 21 or later. SolrJ 10 can run on JDK 17; these are requirements for separate server and client processes, so the Java version running your application need not be the same as the one running Solr.
The guide’s /latest/ pages can change as Solr releases advance, and Solr 10 introduced source and dependency changes. If your server is on Solr 9.x or another release, use that release’s documentation and matching client version rather than assuming SolrJ 10 code or coordinates will work unchanged.
Add SolrJ to Maven
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>10.0.0</version>
</dependency>
This base artifact includes HttpJdkSolrClient. If you choose Jetty-based clients, add solr-solrj-jetty as well. Solr 10 no longer pulls optional modules such as ZooKeeper through the SolrJ Maven POM automatically; direct ZooKeeper access and Streaming Expressions require their respective optional modules.
Select a client for your Solr deployment
SolrClient is the central abstraction for sending requests and managing client configuration. SolrJ offers several implementations; choose by topology and workload rather than assuming one is universally fastest.
| Client | Best fit | Dependency and behavior |
|---|---|---|
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 when its async features are useful | Supports asynchronous/non-blocking operation and HTTP/1.1 and HTTP/2; the current guide describes it as the most used and tested option. Add solr-solrj-jetty. |
CloudSolrClient |
SolrCloud | Uses cluster state for routing and can distribute update documents to nodes. Configure it with Solr URLs for cluster layout or health information. |
ConcurrentUpdateJettySolrClient |
Indexing-heavy workloads | Buffers documents before sending larger batches; it uses the Jetty-based client dependency. |
LBSolrClient |
Internal client implementation needs | An internal failover and load-balancing abstraction for clients aimed at multiple nodes, rather than the usual first choice for application code. |
Solr’s client-server protocol is HTTP. You can use a direct HTTP client instead of SolrJ, but SolrJ provides Java request and response types that make common indexing and query work more convenient. For SolrCloud, prefer the CloudSolrClient’s Solr URL configuration; Solr 10 deprecates the ZooKeeper Hosts constructor.
Connect to Solr with SolrJ
For URL-based clients, use the Solr root URL, ordinarily ending in /solr, not a collection-specific URL when the builder expects a root URL. Set a default collection on the builder so operations can omit the collection parameter where appropriate. Solr 10’s guide includes client configuration examples, but it does not prescribe universal connection or read timeouts: set them according to your application and deployment.
Rank #2
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.impl.HttpJdkSolrClient;
SolrClient client = new HttpJdkSolrClient.Builder("http://localhost:8983/solr")
.withDefaultCollection("articles")
.build();
Replace the example URL and collection name with values for your environment. Keep the client in an application-managed lifecycle, and close it when the application shuts down.
Recommended Free Tools
Make documents match the collection schema
Solr stores records as documents made of named fields. A unique ID field usually plays a role similar to a database primary key. The collection’s schema determines which fields are accepted or mapped, and what analysis is applied to fields configured for tokenization. Unknown fields may be ignored or matched by a dynamic-field rule, depending on the schema.
For a Java ingestion application, a document can be created with SolrInputDocument:
import org.apache.solr.common.SolrInputDocument;
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "article-1042");
doc.addField("title", "Building a Java search API");
doc.addField("body", "Index documents and query them with SolrJ.");
This is a syntax example, not a complete schema: the target collection must support id, title, and body with suitable field definitions. Choose an ID derived from a stable source identifier when a later update should replace the same record; a newly generated random ID for every ingestion can instead create another document.
Java is only one way to feed Solr. The platform can also ingest CSV or XML, database tables, and documents such as Word or PDF files; Solr Cell uses Apache Tika for file extraction. A custom Java application is useful when the ingestion flow needs application-specific transformation or coordination.
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Scan for outdated or missing drivers - takes under a minute3Repair Windows errors before they cause bigger problemsIndex documents without committing every record
Send a document with SolrClient.add. This one-document example demonstrates the API call; normal workloads should generally group documents into larger batches.
Rank #4
client.add("articles", doc);
For routine indexing, Solr’s guide recommends that administrators configure autocommit rather than having application code call commit() after every record. Explicit commit is an available Solr operation, but a per-record hard-commit loop is not the recommended production pattern. Choose batch size and commit visibility behavior for the application’s workload.
Query Solr and map results into Java
Build a SolrQuery with the query string, requested fields, sort order, and a bounded row count. Submit it through the client, then read the returned QueryResponse and its SolrDocumentList.
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("java AND search");
query.setFields("id", "title");
query.setSort("id", SolrQuery.ORDER.asc);
query.setRows(20);
QueryResponse response = client.query("articles", query);
long totalMatches = response.getResults().getNumFound();
for (SolrDocument result : response.getResults()) {
System.out.println(result.getFieldValue("title"));
}
The query limit controls how many matching documents are returned in this response; numFound reports the total number of matches. SolrJ can also map documents to application beans: annotate bean properties with @Field, use addBean() when indexing, and getBeans() when reading results. In Solr 10, check imports against the matching guide because SolrQuery has moved packages compared with older versions.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Best Value
Query syntax, escaping, input validation, authorization, and the design of any public API that calls Solr are application decisions. The SolrJ client API does not by itself establish a web framework or deployment-specific security configuration.
Account for topology and operations
SolrJ can issue queries, index and delete documents, commit changes, and request optimization; those are capabilities, not a sequence every application request must perform. A SolrCloud deployment adds routing: CloudSolrClient uses cluster state to direct requests, and its configured Solr URLs are for discovering cluster layout or health, not a collection-specific endpoint.
Connection and read timeouts, document batching, schema, query fields, and cluster topology all affect behavior. The official guide describes client configuration and usage, not universal production values or a performance ranking. Measure these choices against your own workload and deployment.
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.
The Tool Desk
Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →




