Time for action – playing with an external HttpSolrServer

An EmbeddedSolrServer instance can be substituted for by other server wrapper types, for example, an HttpSolrServer instance, which exposes the same basic API and differs only in the way it is instantiated.

Once we adopt an HttpSolrServer instance, we can send our data to an external remote Solr instance via HTTP. This is the most common way to communicate with Solr via a Java interface, whether they are on the same machine or not, because it permits us to decouple the client from the server (for example, they will not necessarily share the same system resource).

HttpSolrServer server = new HttpSolrServer("http://localhost:8983/solr/arts");

// construct a generic SolrDocument
SolrInputDocument doc = new SolrInputDocument();
doc.addField("uri", "http://TEST/Leonardo_da_Vinci");
doc.addField("artist", "Leonardo Da Vinci");
doc.addField("city", "Vinci");
doc.addField("note", "document updated");
server.add(doc);

server.commit();

// constructing a query with fluent api...
SolrQuery solrQuery = new SolrQuery()
  .setQuery("vermeer~0.5")
  .setFacet(true).setFacetMinCount(1).setFacetLimit(8)
  .addFacetField("museum_entity").addFacetField("city")
  .setHighlight(true).setHighlightSnippets(3)
  .setParam("hl.fl", "artist");

// … and seeing how our query is actually made
System.out.printf("QUERY: %s/%s

", server.getBaseURL(), solrQuery);

// executing the query
QueryResponse rsp = server.query(solrQuery);

// retrieving the collection of documents
  System.out.println("#### DOCs");
for (SolrDocument d : rsp.getResults()) {j
  System.out.println(d);
}

// retrieving the collection of facets
System.out.println("#### facets");
for (FacetField e : rsp.getLimitingFacets()) {
  System.out.println(e);
}

// retrieving the collection of highligths
System.out.println("#### highligths");
for (Entry<?,?> e : rsp.getHighlighting().entrySet()) {
  System.out.println(e);
}

server.shutdown();

As you can see in the preceding code, the results are available in sections, one for every section that we already know from its XML representation. Some generic document collection results, faceting results, highlighting results, and others are made available by a specific method and collection.

What just happened?

We first created an HttpSolrServer object that will be used as a bridge to encapsulate the interaction with a remote Solr application instance. Once created, we interacted directly with this object as if it was a reference to a local instance.

We can construct a generic SolrInputDocument instance to send to the server the data to be indexed. We have already seen this class in action while playing with the updating chain, and this approach is much more general than using a Java bean, as it's not being shaped over a particular domain class. It represents only a generic fluid container for metadata. Note how its consistency with the schema is verified only when the object will be received and handled by an actual remote Solr core.

In this second example, the query is constructed by chaining the method calls that are designed to be easily used by adopting this Fluent API, which is simple to read.

If we print the solrQuery object that is yet to be constructed, it's easy to find out that it is indeed equivalent to the following query:

>> curl -X GET 'http://localhost:8983/solr/arts/q=vermeer%7E0.5&facet=true&facet.mincount=1&facet.limit=8&facet.field=museum_entity&facet.field=city&hl=true&hl.snippets=3&hl.fl=artist'

It's easy to figure out how to construct the object in the reverse direction, by taking a query we have already tested with cURL and deconstructing it on a chain of method calls.

Note how the results are wrapped for convenience into independent collections. For example, for obtaining a faceting results collection, we need to use the getLimitingFacets() method on the QueryResponse object, and then we will be able to iterate over the collection.

The collections used here could be Lists, Maps, or NamedLists in certain cases. In every case, they will be iterable or will expose an iterator in order to simplify our needs for consuming their data, even on a streaming scenario where a huge quantity of results are returned.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.185.196