将Solr响应(solrdocumentlist)转换为json(或xml) [英] Converting the solr response (solrdocumentlist) to json (or xml)

查看:265
本文介绍了将Solr响应(solrdocumentlist)转换为json(或xml)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将响应从HttpSolrServer转换为json格式的搜索.响应以SolrDocumentList形式出现.我现在拥有的代码是:

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

使用示例数据在canon上进行搜索.

for a search on canon using their example data.

如果我改用return rsp.toString();,则会以以下方式获取标头信息:

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

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

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.

我似乎找不到任何有关将响应转换为json的信息.有什么想法吗?

I cannot seem to find anything about converting the response to a json. Any ideas?

推荐答案

虽然这是一个老问题,但我也提出了类似的问题,并且在挖掘代码后,我能够将响应转换为json,尽管我仍然对代码的速度和性能持怀疑态度,以防万一需求过多.

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..

因此,下面是我编写的代码(我使用了Jettison的JSON库,而忽略了for循环的愚蠢结构):

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

基本上,这将使您能够在json中获得结果... solrj不允许您​​使用除javabin之外的任何类型尽管您可以在查询中显式设置wt

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

希望有帮助

这篇关于将Solr响应(solrdocumentlist)转换为json(或xml)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆