将solr响应(solrdocumentlist)转换为json(或xml)

时间:2012-07-05 20:15:06

标签: json solr

我正在进行搜索,我正在尝试将响应从HttpSolrServer转换为json格式。响应来自SolrDocumentList。我现在的代码是:

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();

当我打印出退货时,它会以:

返回
{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}]}}

使用他们的示例数据搜索canon

如果我改为return rsp.toString();,我会将其标记为:

{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}]}}

我知道使用HttpSolrServer时,响应格式目前只能是xml或javabin(我已设置为xml)。这似乎对实际返回的结果及其格式没有影响。

我似乎无法找到将响应转换为json的任何内容。有什么想法吗?

4 个答案:

答案 0 :(得分:5)

对于您尝试执行的操作,您不需要使用solrj库。您可以使用CommonsHTTPClient直接通过HTTP发送查询参数并设置'wt = json' - 以JSON格式检索响应。

答案 1 :(得分:3)

虽然这是一个老问题,但我提出了类似的问题,在挖掘代码之后,我能够将响应转换为json,尽管我仍然对代码的速度和性能持怀疑态度有太多的要求即将到来..

以下是我编写的代码段(我使用了Jettison的JSON库并忽略了for循环的愚蠢结构):

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);

基本上这会让你在json中得到结果... solrj并不允许你使用javabin 之外的任何类型,尽管你可以明确地在查询中设置wt

希望有所帮助

答案 2 :(得分:2)

以下代码可以使用

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);           
}

同时添加参数json.nl=map

来源:http://wiki.apache.org/solr/SolJSON

答案 3 :(得分:-2)

导入Gson并使用其实用程序方法。