Hello Everyone! Today we will discuss another important aspect in solr which is widely used in enterprise-grade applications harnessing the power of solr. In most scenarios, we need to interact with data types of different types and from different sources. Solr provides with multiple client Api’s to deal with this type of scenario and do much more.
Introduction to Client APIs
At its heart, Solr is a Web application, but because it is built on open protocols, any type of client application can use Solr.
HTTP is the fundamental protocol used between client applications and Solr. The client makes a request and Solr does some work and provides a response. Clients use requests to ask Solr to do things like performing queries or index documents.
Client applications can reach Solr by creating HTTP requests and parsing the HTTP responses. Client APIs encapsulate much of the work of sending requests and parsing responses, which makes it much easier to write client applications.
Clients use Solr’s five fundamental operations to work with Solr. The operations are query, index, delete, commit, and optimize.
Queries are executed by creating a URL that contains all the query parameters. Solr examines the request URL, performs the query, and returns the results. The other operations are similar, although in certain cases the HTTP request is a POST operation and contains information beyond whatever is included in the request URL. An index operation, for example, may contain a document in the body of the request.
Choosing an Output Format
Many programming environments are able to send HTTP requests and retrieve responses. Parsing the responses is a slightly more thorny problem. Fortunately, Solr makes it easy to choose an output format that will be easy to handle on the client side.
Specify a response format using the
wt parameter in a query.
Most client APIs hide this detail for you, so for many types of client applications, you won’t ever have to specify a
Client API Lineup
As of now, solr has follwoing client API’s in provision:
HTTP requests can be sent to Solr using the standard
Building and Running SolrJ Applications
The SolrJ API ships with Solr, so you do not have to download or install anything else. But you will need to configure your build to include SolrJ and its dependencies.
Common Build Systems
Most mainstream build systems greatly simplify dependency management, making it easy to add SolrJ to your project.
For projects built with Ant (using Ivy), place the following in your
For projects built with Maven, place the following in your
For projects built with Gradle, place the following in your
Adding SolrJ to the Classpath Manually
If you are not using one of the above build system, it’s still easy to add SolrJ to your build.
At build time, all that is required is the SolrJ jar itself:
solr-solrj-7.4.0.jar. To compile code manually that uses SolrJ, use a
javac command similar to:
At runtime, you need a few of SolrJ’s dependencies, in addition to SolrJ itself. For convenience, these dependencies are made available in the
dist/solrj-lib directory. Run your project with a classpath like:
Querying in SolrJ
SolrClient has a number of
query() methods for fetching results from Solr. Each of these methods takes in a
SolrParams,an object encapsulating arbitrary query-parameters. And each method outputs a
QueryResponse, a wrapper which can be used to access the result documents and other related metadata.
The following snippet uses a SolrClient to query Solr’s “techproducts” example collection, and iterate over the results.
SolrParams has a
SolrQuery subclass, which provides some convenience methods that greatly simplifies query creation. The following snippet shows how the query from the previous example can be built using some of the convenience methods in
Indexing in SolrJ
Indexing is also simple using SolrJ. Users build the documents they want to index as instances of
SolrInputDocument, and provide them as arguments to one of the
add() methods on
The following example shows how to use SolrJ to add a document to Solr’s “techproducts” example collection:
So , this is it about solr client API’s. We will be back with another post very soon.10 Solr Client API’s