我使用的是Solr-5.0.0。
我打算使用Java查询Solr。我在这里使用group by子句。
这是我的代码
SolrQuery qry = new SolrQuery();
qry.setQuery("product_name:(laptops)");
qry.addFilterQuery("brand:dell OR brand: sony OR brand:samsung");
qry.setParam("group", true);
qry.setParam("group.field", "brand");
qry.setParam("stats", true);
qry.setParam("stats.field", "product_price");
qry.setFields("brand");
System.out.println(qry.toString());
QueryRequest qryReq = new QueryRequest(qry);
QueryResponse resp = qryReq.process(solr);
System.out.println(resp.getResponse().toString());
// Here I am getting required response;
SolrDocumentList docs = resp.getResults();
//Below code giving me exception null pointer
for(int i = 0; i < docs.size(); i++) {
System.out.println(docs.get(i));
}
但是我无法解析查询的结果,我只需要从列表或数组中的查询中获取品牌及其对应的计数
发布于 2015-04-03 10:17:53
我不熟悉您使用的SolrQuery类。
我在我的程序中使用solr,方法是创建一个URL(Solr请求)并获得JSon形式的响应,如下所示。
将请求定义为url
String url = "http://"+solr_server+":"+solr_port+"/solr/collection_name/select?q="+brand+"&wt=json&indent=true";
从请求url中以JSon格式读取solr结果
JSONObject json = readJsonFromUrl(url);
获取响应,然后获取文档-然后遍历文档
JSONObject j1 = (JSONObject) json.get("response");
JSONArray j2 = j1.getJSONArray("docs");
for(int i=0;i<j2.length();i++){
//do whatever you want to do
}
记住将来自http://www.java2s.com/Code/JarDownload/java-json/java-json.jar.zip的jar放在类路径中。
发布于 2015-05-21 21:45:08
使用a link调试查询,并查看是否正确构建了查询。然后您就可以采用java了。
https://stackoverflow.com/questions/29430031
复制