3. SolrOxmClient

The SolrOxmClient provides for easier client communication with Apache Solr. It marshalls/unmarshalls searches and updates to and from a JavaBean. It also allows calls to commit, rollback, and optimize. It is built on top of HttpClientTemplate and HttpClientOxmTemplate.

[Note]Note

The Spring configuration and code are from the Solr Client example and are not part of this module.

Spring Configuration

A base URL to Solr and a marshaller and unmarshaller must be defined to use it. To read more about the test look at the Solr Client example.

HttpClientOxmTemplateTest-context.xml
                    
<bean id="solrOxmClient" class="org.springbyexample.httpclient.solr.SolrOxmClient"
      p:baseUrl="http://${solr.host}:${solr.port}/solr"
      p:marshaller-ref="catalogItemMarshaller" 
      p:unmarshaller-ref="catalogItemMarshaller" />
                    
                

Code Example

Example 10. Excerpt from SolrOxmClientIT.testSearch()

Any Solr search parameter can be passed in and the results are unmarshalled into a specific JavaBean.

                    
List<CatalogItem> lCatalogItems = client.search(SEARCH_QUERY_PARAM);
                    
                

Example 11. Excerpt from SolrOxmClientIT.testPaginatedSearch()

To pass in other parameters to the search, a Map can be passed in. The query is specified by the 'q' key, and the 'start' & 'rows' indicate what range of the results to return.

                    
Map<String, String> hParams = new HashMap<String, String>();
hParams.put("q", "electronics");
hParams.put("start", "5");
hParams.put("rows", "5");

List<CatalogItem> lCatalogItems = client.search(hParams);

                    
                

Example 12. Excerpt from SolrOxmClientIT.testUpdate()

Updates a list of CatalogItem instances.

                    
client.update(lCatalogItems);