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 NoOpResponseParser.java
.
CloudSolrServer server = new CloudSolrServer("localhost:2181");
server.setDefaultCollection("collection1");
server.connect();
SolrQuery query = new SolrQuery("*:*");
query.setRows(1);
QueryRequest req = new QueryRequest(query);
NoOpResponseParser dontMessWithSolr = new NoOpResponseParser();
dontMessWithSolr.setWriterType("json");
server.setParser(dontMessWithSolr);
NamedList<Object> resp = server.request(req);
String jsonResponse = (String) resp.get("response");
System.out.println(jsonResponse);
Assert.assertTrue("Verify that we get back some JSON",
jsonResponse.startsWith("{\"responseHeader"));
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.