What is SolrJ? and Query Example

What is SolrJ?

SolrJ is a Java client library for Apache Solr, a powerful open-source search platform that is designed to handle large volumes of structured and unstructured data. SolrJ provides a convenient and easy-to-use interface for interacting with Solr from Java, allowing developers to build Java applications that can search and index data using Solr.

With SolrJ, developers can easily index and search data stored in Solr, as well as manage and configure Solr instances and cores. SolrJ supports all of the features and functionality provided by Solr, including full-text search, faceted search, hit highlighting, and more.

SolrJ is an essential tool for any Java developer who needs to build search and indexing functionality into their applications. It is easy to use and well-documented, making it an ideal choice for developers of all skill levels.

SolrJ features

In addition to the basic search and indexing functionality provided by SolrJ, the library also offers a number of advanced features and capabilities. Some examples include:

  1. Distributed search: SolrJ can be used to perform distributed searches across multiple Solr instances or shards, allowing developers to build highly scalable and fault-tolerant search applications.
  2. Real-time indexing: SolrJ can be used to perform real-time indexing, which means that data can be indexed and made searchable as soon as it is added to the Solr index. This makes it possible to build applications that can provide up-to-date search results at all times.
  3. Advanced query options: SolrJ supports a wide range of advanced query options, including faceted search, hit highlighting, and spatial search. These options can be used to build powerful and flexible search applications that meet a wide range of needs and requirements.
  4. Performance tuning: SolrJ provides a number of tools and options for optimizing the performance of Solr-based applications, including the ability to fine-tune index and query settings, and to implement caching and other performance-enhancing techniques.

Overall, SolrJ is a powerful and versatile tool for building search and indexing functionality into Java applications. Whether you are building a simple search engine or a complex enterprise application, SolrJ can provide the tools and capabilities you need to get the job done.

SolrJ query example

Here is the full code to create and execute a SolrQuery using SolrJ:

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;

public class SolrQueryExample {

   public static void main(String[] args) throws SolrServerException, IOException {
      // create a SolrClient object
      SolrClient solrClient = new HttpSolrClient.Builder("http://localhost:8983/solr/mycore").build();

      // create a SolrQuery object
      SolrQuery query = new SolrQuery();
      query.setSort("price", SolrQuery.ORDER.asc);

      // execute the query and get the response
      QueryResponse response = solrClient.query(query);

      // get the list of documents from the response
      SolrDocumentList documents = response.getResults();

      // iterate through the documents and process the results
      for (SolrDocument document : documents) {
         String id = (String) document.getFieldValue("id");
         String name = (String) document.getFieldValue("name");
         Float price = (Float) document.getFieldValue("price");
         // process the document fields as needed

      // get the facet results from the response
      Map<String, Integer> facetCounts = response.getFacetField("category").getValues();

      // iterate through the facet results and process them
      for (Map.Entry<String, Integer> facet : facetCounts.entrySet()) {
         String category = facet.getKey();
         int count = facet.getValue();
         // process the facet results as needed

This code creates a SolrClient object and uses it to execute a SolrQuery that searches for all documents in the Solr index, sorts the results by the “price” field in ascending order, and enables faceting on the

Here are some additional notes about the SolrQuery example code:

  • The code assumes that you have a Solr instance running on your local machine at the URL specified in the SolrClient constructor (http://localhost:8983/solr/mycore). You will need to modify this URL if you are using a different Solr instance or core.
  • The code sets the start and rows parameters to paginate the search results. The setStart method specifies the offset of the first result to be returned, and the setRows method specifies the maximum number of results to be returned. In this example, the search will return the first 10 results (rows 0 to 9).
  • The code enables faceting on the “category” field and sets the facet limit to 5. This means that the search results will include a list of the top 5 most common values for the “category” field, along with the number of documents that match each value.
  • The code iterates through the search results and the facet results, processing the fields and values as needed. You can modify this code to perform any additional processing or analysis on the search results.

Leave a Comment