solrj实现solr的group查询:

来源:互联网 发布:车牌识别软件破解版 编辑:程序博客网 时间:2024/05/21 21:02

参考wiki:http://wiki.apache.org/solr/FieldCollapsing#Result_Grouping_.2BAC8_Field_Collapsing


一、实现的相关类:SolrServer、SolrQuery、QueryResponse、GroupResponse、GroupCommand、Group、SolrDocumentList

    (1)  SolrServer类 提供与Solr实例的连接与通信。

     

SolrServer solr = new HttpSolrServer("http://localhost:8983/solr");
  

   (2) SolrQuery类 提供查询的相关参数;

        SolrQuery继承于 ModifiableSolrParams,而ModifiableSolrParams则继承与SolrParams。

         

// http://localhost:8983/solr/select?q=学生&group=true&group.field=age//ModifiableSolrParams params = new ModifiableSolrParams();SolrQuery params = new SolrQuery();//the common parameters for all searchparams.set("q", "*:*");params.set("fq", "age:[20 TO 30]", "grade:[70 TO *]"); // filter queryparams.set("fl", "*,score");  // field listparams.set("sort", "grade desc" );  //default score desc.params.set("start", "0");params.set("rows", "10");params.set("timeAllowed", "30000"); //miliseconds//params.set("wt", "xml"); // the response writer typeparams.set("omitHeader", "true"); //default falseparams.set("cache", "false");     //default true//parameters only for grouping resultparams.set("group", "true");params.set("group.field", "id", "age");params.set("group.query", "学生", "学习", "grade:[0 TO 59.9]", "grade:[60 TO *]", "age:[10 TO 19]", "age:[20 TO *]" );//params.set("group.func", "grade GRATERTHAN 60"); // not found, don't use it!!!params.set("group.sort", "grade desc");params.set("group.format", "grouped"); //default:simple, other:groupedparams.set("group.main", "false");    // when /*group.format=simple and */ group.main=true, just return the documentList only!!! params.set("group.ngroups", "true");params.set("group.truncate", "true"); //default is false;params.set("group.cache.percent", "50"); //default is 0;params.set("group.offset", "0");params.set("group.limit", "10");// 分布式设置//params.set("shards", "localhost:8983/solr1", "localhost:8983/solr2"); //shards=host:port/base_url[,host:port:/base_url,[....]]//params.set("shards.qt", "/select"); // qt: query type// to indicate the request Handler to use

(3) QueryResponse类提供查询的的结果:

QueryResponse response = null;try {response = solr.query(params);//System.out.println("查询耗时:" + response.getQTime());} catch (SolrServerException e) {System.err.println(e.getMessage());e.printStackTrace();} catch (Exception e) {System.err.println(e.getMessage());e.printStackTrace();} finally {solr.shutdown();}
(4)GroupResponse提供基于分组的查询结果:

  获取GroupResponse:

GroupResponse groupResponse = response.getGroupResponse();


(5) 通过GroupResponse获取GroupCommand,进而通过GroupCommand获取Group,而Group又可以获得SolrDocumentList

if (response != null) {GroupResponse groupResponse = response.getGroupResponse();if (groupResponse != null) {List<GroupCommand> groupCommandList = groupResponse.getValues();for (GroupCommand groupCommand : groupCommandList) {System.out.println("GroupCommand Name : " + groupCommand.getName());System.out.println("Num of Groups Found: " + groupCommand.getNGroups());System.out.println("Num of documents Found: " + groupCommand.getMatches());System.out.println("The groups are: ");List<Group> groups = groupCommand.getValues();for (Group group : groups) {System.out.println("group value: " + group.getGroupValue());SolrDocumentList solrDocumentList = group.getResult();System.out.println("Num of Documents in this group: " + solrDocumentList.getNumFound());System.out.println("start: " + solrDocumentList.getStart());System.out.println("Max score: " + solrDocumentList.getMaxScore());// solrDocumentList.get(index)for (SolrDocument doc : solrDocumentList) {System.out.println("the Fields of document:");Collection<String> names = doc.getFieldNames();for (String name : names) {System.out.println(name + ": " + doc.getFieldValue(name));}System.out.println("\n");}System.out.println("\n\n");}System.out.println("\n\n");}}



solrj实现分组查询代码:

package cn.wzb;import java.util.Collection;import java.util.List;import org.apache.solr.client.solrj.SolrQuery;import org.apache.solr.client.solrj.SolrServer;import org.apache.solr.client.solrj.SolrServerException;import org.apache.solr.client.solrj.impl.HttpSolrServer;import org.apache.solr.client.solrj.response.Group;import org.apache.solr.client.solrj.response.GroupCommand;import org.apache.solr.client.solrj.response.GroupResponse;import org.apache.solr.client.solrj.response.QueryResponse;import org.apache.solr.common.SolrDocument;import org.apache.solr.common.SolrDocumentList;import org.apache.solr.common.params.ModifiableSolrParams;public class TestGroup {public static void main(String[] args) {SolrServer solr = new HttpSolrServer("http://localhost:8983/solr");// http://localhost:8983/solr/select?q=学生&group=true&group.field=age//ModifiableSolrParams params = new ModifiableSolrParams();SolrQuery params = new SolrQuery();//the common parameters for all searchparams.set("q", "*:*");params.set("fq", "age:[20 TO 30]", "grade:[70 TO *]"); // filter queryparams.set("fl", "*,score");  // field listparams.set("sort", "grade desc" );  //default score desc.params.set("start", "0");params.set("rows", "10");params.set("timeAllowed", "30000"); //miliseconds//params.set("wt", "xml"); // the response writer typeparams.set("omitHeader", "true"); //default falseparams.set("cache", "false");     //default true//parameters only for grouping resultparams.set("group", "true");params.set("group.field", "id", "age");params.set("group.query", "学生", "学习", "grade:[0 TO 59.9]", "grade:[60 TO *]", "age:[10 TO 19]", "age:[20 TO *]" );//params.set("group.func", "grade GRATERTHAN 60"); // not found, don't use it!!!params.set("group.sort", "grade desc");params.set("group.format", "grouped"); //default:simple, other:groupedparams.set("group.main", "false");    // when /*group.format=simple and */ group.main=true, just return the documentList only!!! params.set("group.ngroups", "true");params.set("group.truncate", "true"); //default is false;params.set("group.cache.percent", "50"); //default is 0;params.set("group.offset", "0");params.set("group.limit", "10");// 分布式设置//params.set("shards", "localhost:8983/solr1", "localhost:8983/solr2"); //shards=host:port/base_url[,host:port:/base_url,[....]]//params.set("shards.qt", "/select"); // qt: query type// to indicate the request Handler to useQueryResponse response = null;try {response = solr.query(params);//System.out.println("查询耗时:" + response.getQTime());} catch (SolrServerException e) {System.err.println(e.getMessage());e.printStackTrace();} catch (Exception e) {System.err.println(e.getMessage());e.printStackTrace();} finally {solr.shutdown();}if (response != null) {GroupResponse groupResponse = response.getGroupResponse();if (groupResponse != null) {List<GroupCommand> groupCommandList = groupResponse.getValues();for (GroupCommand groupCommand : groupCommandList) {System.out.println("GroupCommand Name : " + groupCommand.getName());System.out.println("Num of Groups Found: " + groupCommand.getNGroups());System.out.println("Num of documents Found: " + groupCommand.getMatches());System.out.println("The groups are: ");List<Group> groups = groupCommand.getValues();for (Group group : groups) {System.out.println("group value: " + group.getGroupValue());SolrDocumentList solrDocumentList = group.getResult();System.out.println("Num of Documents in this group: " + solrDocumentList.getNumFound());System.out.println("start: " + solrDocumentList.getStart());System.out.println("Max score: " + solrDocumentList.getMaxScore());// solrDocumentList.get(index)for (SolrDocument doc : solrDocumentList) {System.out.println("the Fields of document:");Collection<String> names = doc.getFieldNames();for (String name : names) {System.out.println(name + ": " + doc.getFieldValue(name));}System.out.println("\n");}System.out.println("\n\n");}System.out.println("\n\n");}}//System.out.println("response = " + response);//System.out.println(response.getStatus());System.out.println("查询耗时:" + response.getQTime());}solr.shutdown();}}

测试结果:

2012-8-17 14:04:52 org.apache.solr.client.solrj.impl.HttpClientUtil createClient信息: Creating new http client, config:maxConnections=128&maxConnectionsPerHost=32&followRedirects=falseGroupCommand Name : idNum of Groups Found: 3Num of documents Found: 3The groups are: group value: no3Num of Documents in this group: 1start: 0Max score: 1.0the Fields of document:id: no3number: 3name: 王五grade: 94.8age: 25introduction: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人text_auto: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人text: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人title: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人title_smart: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人entranceTime: Wed Jan 05 00:05:00 CST 2005deposit: 3000.0009score: 1.0group value: no4Num of Documents in this group: 1start: 0Max score: 1.0the Fields of document:id: no4number: 4name: 沈六grade: 80.8age: 30introduction: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabartext_auto: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabartext: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabartitle: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabartitle_smart: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabarentranceTime: Tue Jan 10 00:09:00 CST 2006deposit: 90000.000788score: 1.0group value: no5Num of Documents in this group: 1start: 0Max score: 1.0the Fields of document:id: no5number: 4name: 江八grade: 70.8age: 25introduction: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafootext_auto: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafootext: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafootitle: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafootitle_smart: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafooentranceTime: Wed Jan 10 00:09:00 CST 2007deposit: 2343900.000788score: 1.0GroupCommand Name : ageNum of Groups Found: 2Num of documents Found: 3The groups are: group value: 25Num of Documents in this group: 2start: 0Max score: 1.0the Fields of document:id: no3number: 3name: 王五grade: 94.8age: 25introduction: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人text_auto: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人text: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人title: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人title_smart: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人entranceTime: Wed Jan 05 00:05:00 CST 2005deposit: 3000.0009score: 1.0the Fields of document:id: no5number: 4name: 江八grade: 70.8age: 25introduction: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafootext_auto: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafootext: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafootitle: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafootitle_smart: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafooentranceTime: Wed Jan 10 00:09:00 CST 2007deposit: 2343900.000788score: 1.0group value: 30Num of Documents in this group: 1start: 0Max score: 1.0the Fields of document:id: no4number: 4name: 沈六grade: 80.8age: 30introduction: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabartext_auto: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabartext: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabartitle: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabartitle_smart: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabarentranceTime: Tue Jan 10 00:09:00 CST 2006deposit: 90000.000788score: 1.0GroupCommand Name : sub(grade, 60)Num of Groups Found: 3Num of documents Found: 3The groups are: group value: 34.800003Num of Documents in this group: 1start: 0Max score: 1.0the Fields of document:id: no3number: 3name: 王五grade: 94.8age: 25introduction: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人text_auto: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人text: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人title: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人title_smart: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人entranceTime: Wed Jan 05 00:05:00 CST 2005deposit: 3000.0009score: 1.0group value: 20.800003Num of Documents in this group: 1start: 0Max score: 1.0the Fields of document:id: no4number: 4name: 沈六grade: 80.8age: 30introduction: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabartext_auto: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabartext: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabartitle: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabartitle_smart: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabarentranceTime: Tue Jan 10 00:09:00 CST 2006deposit: 90000.000788score: 1.0group value: 10.800003Num of Documents in this group: 1start: 0Max score: 1.0the Fields of document:id: no5number: 4name: 江八grade: 70.8age: 25introduction: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafootext_auto: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafootext: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafootitle: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafootitle_smart: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafooentranceTime: Wed Jan 10 00:09:00 CST 2007deposit: 2343900.000788score: 1.0GroupCommand Name : 学生Num of Groups Found: nullNum of documents Found: 3The groups are: group value: 学生Num of Documents in this group: 1start: 0Max score: 1.0the Fields of document:id: no3number: 3name: 王五grade: 94.8age: 25introduction: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人text_auto: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人text: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人title: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人title_smart: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人entranceTime: Wed Jan 05 00:05:00 CST 2005deposit: 3000.0009score: 1.0GroupCommand Name : 学习Num of Groups Found: nullNum of documents Found: 3The groups are: group value: 学习Num of Documents in this group: 2start: 0Max score: 1.0the Fields of document:id: no3number: 3name: 王五grade: 94.8age: 25introduction: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人text_auto: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人text: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人title: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人title_smart: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人entranceTime: Wed Jan 05 00:05:00 CST 2005deposit: 3000.0009score: 1.0the Fields of document:id: no5number: 4name: 江八grade: 70.8age: 25introduction: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafootext_auto: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafootext: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafootitle: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafootitle_smart: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafooentranceTime: Wed Jan 10 00:09:00 CST 2007deposit: 2343900.000788score: 1.0GroupCommand Name : grade:[0 TO 59.9]Num of Groups Found: nullNum of documents Found: 3The groups are: group value: grade:[0 TO 59.9]Num of Documents in this group: 0start: 0Max score: NaNGroupCommand Name : grade:[60 TO *]Num of Groups Found: nullNum of documents Found: 3The groups are: group value: grade:[60 TO *]Num of Documents in this group: 3start: 0Max score: 1.0the Fields of document:id: no3number: 3name: 王五grade: 94.8age: 25introduction: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人text_auto: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人text: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人title: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人title_smart: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人entranceTime: Wed Jan 05 00:05:00 CST 2005deposit: 3000.0009score: 1.0the Fields of document:id: no4number: 4name: 沈六grade: 80.8age: 30introduction: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabartext_auto: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabartext: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabartitle: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabartitle_smart: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabarentranceTime: Tue Jan 10 00:09:00 CST 2006deposit: 90000.000788score: 1.0the Fields of document:id: no5number: 4name: 江八grade: 70.8age: 25introduction: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafootext_auto: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafootext: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafootitle: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafootitle_smart: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafooentranceTime: Wed Jan 10 00:09:00 CST 2007deposit: 2343900.000788score: 1.0GroupCommand Name : age:[10 TO 19]Num of Groups Found: nullNum of documents Found: 3The groups are: group value: age:[10 TO 19]Num of Documents in this group: 0start: 0Max score: NaNGroupCommand Name : age:[20 TO *]Num of Groups Found: nullNum of documents Found: 3The groups are: group value: age:[20 TO *]Num of Documents in this group: 3start: 0Max score: 1.0the Fields of document:id: no3number: 3name: 王五grade: 94.8age: 25introduction: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人text_auto: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人text: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人title: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人title_smart: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人entranceTime: Wed Jan 05 00:05:00 CST 2005deposit: 3000.0009score: 1.0the Fields of document:id: no4number: 4name: 沈六grade: 80.8age: 30introduction: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabartext_auto: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabartext: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabartitle: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabartitle_smart: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabarentranceTime: Tue Jan 10 00:09:00 CST 2006deposit: 90000.000788score: 1.0the Fields of document:id: no5number: 4name: 江八grade: 70.8age: 25introduction: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafootext_auto: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafootext: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafootitle: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafootitle_smart: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafooentranceTime: Wed Jan 10 00:09:00 CST 2007deposit: 2343900.000788score: 1.0查询耗时:0