SolrJ is an API that makes it easy for Java applications to talk to Solr. SolrJ hides a lot of the details of connecting to Solr and allows your application to interact with Solr with simple high-level methods.
The center of SolrJ is the
org.apache.solr.client.solrj package, which contains just five main classes. Begin by creating a
SolrClient, which represents the Solr instance you want to use. Then send
SolrQuerys and get back SolrResponses.
SolrClient is abstract, so to connect to a remote Solr instance, you'll actually create an instance of either
CloudSolrClient . Both communicate with Solr via HTTP, the difference is that
HttpSolrClient is configured using an explicit Solr URL, while
CloudSolrClient is configured using the zkHost String for a SolrCloud cluster
Once you have a
SolrClient, you can use it by calling methods like
Building and Running SolrJ Applications
The SolrJ API is included with Solr, so you do not have to download or install anything else. However, in order to build and run applications that use SolrJ, you have to add some libraries to the classpath.
At build time, the examples presented with this section require
solr-solrj-x.y.z.jar to be in the classpath.
At run time, the examples in this section require the libraries found in the 'dist/solrj-lib' directory.
The Ant script bundled with this sections' examples includes the libraries as appropriate when building and running.
You can sidestep a lot of the messing around with the JAR files by using Maven instead of Ant. All you will need to do to include SolrJ in your application is to put the following dependency in the project's
If you are worried about the SolrJ libraries expanding the size of your client application, you can use a code obfuscator like ProGuard to remove APIs that you are not using.
SolrJ uses a binary format, rather than XML, as its default format. Users of earlier Solr releases who wish to continue working with XML must explicitly set the parser to the XMLResponseParser, like so:
query() to have Solr search for results. You have to pass a
SolrQuery object that describes the query, and you will get back a QueryResponse (from the
SolrQuery has methods that make it easy to add parameters to choose a request handler and send parameters to it. Here is a very simple example that uses the default request handler and sets the
To choose a different request handler, for example, just set the
qt parameter like this:
Once you have your
SolrQuery set up, submit it with
The client makes a network connection and sends the query. Solr processes the query, and the response is sent and parsed into a
QueryResponse is a collection of documents that satisfy the query parameters. You can retrieve the documents directly with
getResults() and you can call other methods to find out information about highlighting or facets.
Other operations are just as simple. To index (add) a document, all you need to do is create a
SolrInputDocument and pass it along to the
Uploading Content in XML or Binary Formats
SolrJ lets you upload content in XML and binary formats instead of the default XML format. Use the following to upload using binary format, which is the same format SolrJ uses to fetch results.
Using the ConcurrentUpdateSolrClient
When implementing java applications that will be bulk loading a lot of documents at once,
ConcurrentUpdateSolrClient is an alternative to consider instead of using
ConcurrentUpdateSolrClient buffers all added documents and writes them into open HTTP connections. This class is thread safe. Although any SolrClient request can be made with this implementation, it is only recommended to use the
EmbeddedSolrServer class provides an implementation of the
SolrClient client API talking directly to an micro-instance of Solr running directly in your Java application. This embedded approach is not recommended in most cases and fairly limited in the set of features it supports – in particular it can not be used with SolrCloud or Index Replication.
EmbeddedSolrServer exists primarily to help facilitate testing.
For information on how to use
EmbeddedSolrServer please review the SolrJ JUnit tests in the
org.apache.solr.client.solrj.embedded package of the Solr source release.