问题
I am working on a search in which I am trying to convert the response from a HttpSolrServer
to json format. The response comes as a SolrDocumentList
. The code I have right now is:
SolrQuery solrQuery = new SolrQuery(query);
solrQuery.setParam("wt", "json"); //doesn't affect the return format
QueryResponse rsp = solrServer.query(solrQuery);
SolrDocumentList docs = rsp.getResults();
return docs.toString();
When I print out the return, it comes back as:
{numFound=2,start=0,docs=[SolrDocument{cat=[electronics, camera], features=[3x zoop, 7.1 megapixel Digital ELPH, movie clips up to 640x480 @30 fps, 2.0" TFT LCD, 118,000 pixels, built in flash, red-eye reduction], id=9885A004, inStock=true, includes=32MB SD card, USB cable, AV cable, battery, manu=Canon Inc., manufacturedate_dt=Mon Feb 13 10:26:37 EST 2006, name=Canon PowerShot SD500, popularity=7, price=329.95, store=45.17614,-93.87341, weight=6.4}, SolrDocument{cat=[electronics, multifunction printer, printer, scanner, copier], features=[Multifunction ink-jet color photo printer, Flatbed scanner, optical scan resolution of 1,200 x 2,400 dpi, 2.5" color LCD preview screen, Duplex Copying, Printing speed up to 29ppm black, 19ppm color, Hi-Speed USB, memory card: CompactFlash, Micro Drive, SmartMedia, Memory Stick, Memory Stick Pro, SD Card, and MultiMediaCard], id=0579B002, inStock=true, manu=Canon Inc., name=Canon PIXMA MP500 All-In-One Photo Printer, popularity=6, price=179.99, store=45.17614,-93.87341, weight=352.0}]}}
for a search on canon
using their example data.
If I instead do return rsp.toString();
I get back the header information with it as:
{responseHeader={status=0,QTime=1,params={indent=true,q=canon\*,wt=xml,version=2.2}},response={numFound=2,start=0,docs=[SolrDocument{cat=[electronics, camera], features=[3x zoop, 7.1 megapixel Digital ELPH, movie clips up to 640x480 @30 fps, 2.0" TFT LCD, 118,000 pixels, built in flash, red-eye reduction], id=9885A004, inStock=true, includes=32MB SD card, USB cable, AV cable, battery, manu=Canon Inc., manufacturedate_dt=Mon Feb 13 10:26:37 EST 2006, name=Canon PowerShot SD500, popularity=7, price=329.95, store=45.17614,-93.87341, weight=6.4}, SolrDocument{cat=[electronics, multifunction printer, printer, scanner, copier], features=[Multifunction ink-jet color photo printer, Flatbed scanner, optical scan resolution of 1,200 x 2,400 dpi, 2.5" color LCD preview screen, Duplex Copying, Printing speed up to 29ppm black, 19ppm color, Hi-Speed USB, memory card: CompactFlash, Micro Drive, SmartMedia, Memory Stick, Memory Stick Pro, SD Card, and MultiMediaCard], id=0579B002, inStock=true, manu=Canon Inc., name=Canon PIXMA MP500 All-In-One Photo Printer, popularity=6, price=179.99, store=45.17614,-93.87341, weight=352.0}]}}
I know that with HttpSolrServer
the response format can only currently be of xml or javabin (which I have set to xml). This seems to have no impact on the actual returned results and their format.
I cannot seem to find anything about converting the response to a json. Any ideas?
回答1:
Well although it is an old question, I have come up with a similar issue and after digging the code, i was able to convert the response into json, although i am still sceptical about the speed and performance of the code in case there are too many reqs are coming..
so below is the piece of code that I wrote(I used the JSON lib from Jettison and ignore the stupid structure of the for loop):
QueryResponse qp = server.query(solrQuery);
SolrDocumentList docList= qp.getResults();
JSONObject returnResults = new JSONObject();
Map<Integer, Object> solrDocMap = new HashMap<Integer, Object>();
int counter = 1;
for(Map singleDoc : docList)
{
solrDocMap.put(counter, new JSONObject(singleDoc));
counter++;
}
returnResults.put("docs", solrDocMap);
basically this will let you get results in json... solrj doesnt let you use any kind other than javabin although you may set wt in the query explicitly
hope it helps
回答2:
For what you are attempting to do, you do not need to use solrj library. You can use CommonsHTTPClient to send query parameters over HTTP directly AND set 'wt=json' - to retrieve the response in JSON.
回答3:
The following code works
SolrDocumentList list = new SolrDocumentList();
JSONArray jArray =new JSONArray();
QueryResponse result = solr.query(parameters);
list=result.getResults();
for (int i = 0; i < list.size(); i++) {
JSONObject json = new JSONObject(list.get(i));
jArray.put(json);
}
Also add the parameter json.nl=map
.
Source: http://wiki.apache.org/solr/SolJSON
回答4:
Import Gson and use its utility methods.
来源:https://stackoverflow.com/questions/11351917/converting-the-solr-response-solrdocumentlist-to-json-or-xml