Using SolrJ CloudSolrServer and retrieving JSON

Eric PughJanuary 8, 2015

On a project I’m working on we want to return the Solr JSON formatted results to our rich front end application based on the Spyglass framework. However, in order to support High Availalbity (HA), we are using SolrCloud.

So what happens when a Solr node goes down? Well, if every node is in the load balancer, then we can have our incoming queries be directed to any Solr node, and intenrally Solr will collect all the results and then return them in the JSON format. However, this means that every node needs to be in the load balancer, and that adds an additional configuration step as we bring up and down nodes.

Instead, since we have ZooKeeper tracking the state of the cluster, then we want to take advantage of all the support that the CloudServer provides. However then we stubbed our toe, by default the response is a set of Java objects, and we don’t want to convert those back to JSON objects and return them. Also, since SpyGlass is expecting the results in the Solr JSON format, we don’t want to change that.

So how can we combine SolrJ’s robust connection with the JSON output? It’s easy, with the awkwardly named

CloudSolrServer server = new CloudSolrServer("localhost:2181");

SolrQuery query = new SolrQuery("*:*");
QueryRequest req = new QueryRequest(query);

NoOpResponseParser dontMessWithSolr = new NoOpResponseParser();
NamedList<Object> resp = server.request(req);
String jsonResponse = (String) resp.get("response");
Assert.assertTrue("Verify that we get back some JSON",

NoOpResponseParser returns the full JSON output and stores it under the key “response”. You can do this for any of other output formats, like PHP, Ruby that you want to mess with as well.

More blog articles:

We've been Solr-istas since day one!

Our founder wrote the first book on Solr, now in 3rd edition. We've helped organizations from the US Patent and Trademark Office to Cisco build smarter search solutions with Solr.

Solr Services Solr Relevance Engineer Training