SolrJ Java API for Solr

SolrJ Java API for Solr


Introduction


SolrJ is a Java client to access solr. SolrJ offers a java interface to add, update, and query the solr index.


HttpSolrServer


I use Spring to load this properties file:
solr.host = 127.0.0.1
solr.port = 8983
solr.core = documents

# defaults to 0. > 1 not recommended
solr.maxretries = 1

# 5 seconds to establish TCP
solr.connectiontimeout = 5000

# socket read timeout
solr.socketreadtimeout = 50000

# max connections per host
solr.maxconnectionsperhost = 250

# max total connections
solr.maxtotalconnections = 100

# defaults to false
solr.followredirects = false

# defaults to false
# Server side must support gzip or deflate for this to have any effect.
solr.allowcompression = true

and to populate this method:
public static HttpSolrServer transform(String url, boolean allowcompression, Integer connectiontimeout, String core, boolean followredirects, Integer maxconnectionsperhost, Integer maxretries, Integer maxtotalconnections, Integer socketreadtimeout) throws AdapterValidationException {
HttpSolrServer server = new HttpSolrServer(url);

server.setMaxRetries(maxretries);
server.setConnectionTimeout(connectiontimeout);

/* Setting the XML response parser is only required for cross
version compatibility and only when one side is 1.4.1 or
earlier and the other side is 3.1 or later. */
server.setParser(new XMLResponseParser());

/* The following settings are provided here for completeness.
They will not normally be required, and should only be used
after consulting javadocs to know whether they are truly required. */
server.setSoTimeout(socketreadtimeout);
server.setDefaultMaxConnectionsPerHost(maxconnectionsperhost);
server.setMaxTotalConnections(maxtotalconnections);
server.setFollowRedirects(followredirects);
server.setAllowCompression(allowcompression);

return server;
}

By populating the method above with the values from the properties file, the system is able to work with a configured instance of the HttpSolrServer.


Query Snippets


This code will find the total records that mention the word "climate":
public static void main(String... args) throws Throwable {

HttpSolrServer server = HttpSolrServerAdapter.transform();
assertNotNull(server);

SolrQuery q = new SolrQuery("text:climate");
q.setRows(0); // dont actually request any data

long total = server.query(q).getResults().getNumFound();
logger.info("Total Records (total = %s)", total);

}


A reusable method for executing a query:
public static QueryResponse execute(
String queryName,
HttpSolrServer server,
SolrQuery solrQuery)
throws BusinessException {
try {

QueryResponse queryResponse = server.query(solrQuery);
logger.debug("Query Statistics " +
"(query-name = %s, elapsed-time = %s, query-time = %s, status = %s, request-url = %s)",
queryName,
queryResponse.getElapsedTime(),
queryResponse.getQTime(),
queryResponse.getStatus(),
queryResponse.getRequestUrl());

SolrDocumentList solrDocumentList = queryResponse.getResults();
logger.debug("Total Records " +
"(query-name = %s, total = %s, query = %s)",
queryName,
StringUtils.format(solrDocumentList.size()),
solrQuery.toString());

return queryResponse;

} catch (SolrServerException e) {
logger.error(e);
throw new BusinessException("Unable to Query Server (query-name = %s, message = %s)", e.getMessage());
}
}



References

  1. [Apache] SolrJ Wiki
  2. [Apache]  SolrJ Reference Guide

download file now

Comments

Popular posts from this blog

Spider Man Friend Or Foe ISO Free Download PPSSPP Setting

Sorry Mutter closed unexpectedly on Ubuntu 10 10

Sony Xperia T2 Ultra dual