October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk5 min

How to Build a Java Search API with Apache Solr

Use SolrJ 10.0.0 to connect a Java application to Solr 10, index schema-compatible documents, and query results with SolrQuery.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Index 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.

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.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. Shenzhen desk3 min
    HONOR Expands Beyond Smartphones With Humanoid Robot RevealHONOR said it unveiled its first humanoid robot at MWC 2026 and named shopping assistance, workplace inspections, and supportive companionship as intended uses. Later Robotics D1 claims and a reported…
  2. Cupertino desk5 min
    Apple Unveils AirPods Max 2: The Upgrade That Should Have Happened Years AgoAirPods Max 2 adds H2-powered audio features and Apple claims up to 1.5× more effective ANC, but its design, Smart Case, and 20-hour battery rating are unchanged. Wired lossless audio…
  3. Cupertino desk4 min
    Apple’s OLED Touch MacBooks Are Coming—but the Dynamic Island Is the Real GambleApple has not announced an OLED touchscreen MacBook, but reports point to high-end models arriving in late 2026 or early 2027. The reported Mac Dynamic Island could be useful, but…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.