2

我正在进行搜索,我试图将响应从 a 转换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

4 回答 4

5

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

于 2012-07-05T21:03:54.633 回答
3

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

所以下面是我写的一段代码(我使用了 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

希望能帮助到你

于 2012-11-08T03:24:40.217 回答
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

于 2013-03-22T06:31:26.430 回答
-1

导入 Gson 并使用它的实用方法。

于 2017-06-13T10:00:12.020 回答