How to set ResponseWriter to non-default in Solr

2019-08-02 00:04发布

问题:

I have the following code, which is simply searching the Solr Server.

SolrServer server = new CommonsHttpSolrServer(url);
SolrQuery searchquery = new SolrQuery("company profile");
QueryResponse response = server.query(searchquery)

I want to have the response in json, other than the default which is xml. So I went into the solrconfig.xml file and enabled the following line:

<queryResponseWriter name="json" class="org.apache.solr.request.JSONResponseWriter" />

However, from the console, I'm still getting wt=javabin encoded to the search query request.

Also, I've modified the above code like this:

SolrServer server = new CommonsHttpSolrServer(url);
SolrQuery searchquery = new SolrQuery("company profile");
searchquery.setParam("wt", "json");
QueryResponse response = server.query(searchquery)

But I'm still getting wt=javabin encoded and wt=json also appended, such that the query now looks like this:

webapp/solr path=/select params={wt=javabin&wt=json}

Is there anything I'm doing wrong?

Thanks

回答1:

SolrJ only supports the javabin and xml formats (configurable with CommonHttpSolrServer.setParser).

But why would you want to use JSON? Javabin is by far the format which provides the best decompression speed, its drawback being that is is not human-readable, on the contrary to JSON and XML (which is not a problem in this case since SolrJ is parsing the result for you).



回答2:

With SolrJ and json-lib, I was able to get json response like this:

SolrServer server = new CommonsHttpSolrServer(url);
SolrQuery searchquery = new SolrQuery("company profile");
QueryResponse response = server.query(searchquery)

JSONArray jsonObject = JSONArray.fromObject( response.getResults() );
log.log(Level.INFO, "received jsonObject is {0}", jsonObject.toString());
Iterator data = jsonObject.iterator();

SolrResultBean bean = new SolrResultBean();
List output = new ArrayList();
while(data.hasNext()) {
  output.add(data.next());
}
log.log(Level.INFO, "json items in the List are {0}", output);
bean.setObject(output);

// wicket page redirect
setResponsePage(SearchPage.class, new PageParameters());