solr查询返回JSON格式

这篇博客介绍了如何将Solr查询结果转化为JSON格式,包括使用JSONObject和JSONArray两种方式,以及如何组合显示数据。同时,文章提供了多个参考资料,帮助读者深入理解Solr的索引字段、动态字段创建以及查询操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


SolrDocumentList docs = response.getResults();

1、JSONObject返回类似{"SolrDocument":{“1”:{},“2”:{}}

Map<Integer,Object> mapDoc = new HashMap<Iteger, Object>();

int count = 1;

for(SolrDocument doc : docs){

mapDoc.put(count, doc);

count++;

}

JSONObject json = new JSONObject();

json.put("SolrDocument", mapDoc);

//json.put(mapDoc);输出{{“1”:{},“2”:{}}

String strResult = json.toString();

2、JSONArray返回格式:[{},{}]为json数组

JSONArray arrJson = new JSONArray();

for(SolrDocument doc :docs){

arrJson.add(doc);

}

String strResult = arrJson.toString(); 

3、JSONObject和JSONArray组合显示{"data":[{},{},{}]}

在2的基础上继续向下写:

JSONObject json = new JSONObject();

json.put("data",arrJson);

String strResult = json.toString();

4、query添加param

param中设置显示字段,添加到query时,没有作用,在query中添加显示字段,可以按要求显示字段


sorl创建索引字段和动态自动、创建索引、查询的相关参考资料:

http://codego.net/477246/

http://my.oschina.net/HuifengWang/blog/307501

http://www.importnew.com/12707.html

http://blog.youkuaiyun.com/kissmelove01/article/details/45196941

http://www.360doc.com/content/14/0306/18/203871_358295621.shtml

http://www.tuicool.com/articles/uAzQnaz

http://www.boyunjian.com/javadoc/org.apache.servicemix.bundles/org.apache.servicemix.bundles.solr-solrj/4.3.0_1/_/org/apache/solr/client/solrj/SolrQuery.html#setFields(java.lang.String...)

http://blog.youkuaiyun.com/itbasketplayer/article/details/8086160

http://www.blogjava.net/conans/articles/379556.html

http://zhidao.baidu.com/link?url=pAi93scWL7cEnD7K1MIpjR8FBRqn0hC8yY45tb9OdZJHzjEBC-8u7i8DcfOJKZRnQCPdO1-EhDwIiPLXENlDrSElKcXcgZ4qo-uVUkrQT4C

http://www.sxt.cn/u/2718/blog/4090

http://blog.sina.com.cn/s/blog_a61684c501019n5h.html

http://www.fx114.net/qa-202-108415.aspx

http://www.sxt.cn/info-5072-u-756.html


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值