concept SolrJ in category solr

This is an excerpt from Manning's book Solr in Action.
SolrJ is a Java-based client library provided with the Solr project to communicate with your Solr server from a Java application. In this section, we’ll implement a simple SolrJ client to send documents using Java. If you’re not a Java developer or your application isn’t written in Java, then you’ll be happy to know there are many other Solr client libraries available for other languages such as .NET, Ruby, Python, and PHP. See chapter 12 (section 12.8.2) for more information about Solr client libraries available for various languages.
The following listing provides a simple example using SolrJ to add our two example tweet documents to the index and perform a hard commit. After committing, the example code sends a match all docs query (*:*) to Solr to get the documents we indexed back in the search results.
In addition to deploying the solr.war file to a servlet container, it is also possible to run Solr embedded within another Java application. This can be particularly useful if you need to add search functionality to your application but want to keep Solr self-contained within the scope of your application and not exposed as a service that could be hit by other applications or across your network. The process of starting Solr embedded within your Java application makes use of the SolrJ Java client for Solr and will be covered, along with SolrJ, in section 12.8.3. Regardless of whether you are embedding Solr within an application or running it as a standalone server, making sure that you have the right kind of hardware for your use case and having your system properly configured are important for performance. The next section will dive into some of these considerations.
As you may remember from chapter 5, SolrJ enables you to communicate with Solr from your own application’s Java code. SolrJ ships with Solr, which means that when you pull the Solr code or download a Solr release, the correct version of the SolrJ library is pulled as well. SolrJ can also (optionally) utilize a Java binary request/response format, reducing the serialization/deserialization overhead associated with other communication formats. Using SolrJ, you can communicate across an HTTP connection, as with any other client library, but you can also embed Solr within your application and talk to it directly without exposing an HTTP connection on your server.
As you can see, you have many SolrServer implementations from which to choose based upon the specific nature of your application and Solr configuration. Because HttpSolrServer serves as the lowest common denominator among your options, we will use it to demonstrate the basic SolrJ operations. You saw how to use SolrJ to send documents to Solr for indexing in chapter 5 (section 5.5.2). In addition to sending document updates, SolrJ can be used to invoke the full range of Solr requests directly from Java. The following listing demonstrates how to perform basic document updates, deletes, and searches.
![]()