solrj实现solr的group查询:

6 篇文章 0 订阅

参考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 search
		params.set("q", "*:*");
		params.set("fq", "age:[20 TO 30]", "grade:[70 TO *]"); // filter query
		params.set("fl", "*,score");  // field list
		params.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 type
		params.set("omitHeader", "true"); //default false
		params.set("cache", "false");     //default true
		
		//parameters only for grouping result
		params.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:grouped
		params.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 search
		params.set("q", "*:*");
		params.set("fq", "age:[20 TO 30]", "grade:[70 TO *]"); // filter query
		params.set("fl", "*,score");  // field list
		params.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 type
		params.set("omitHeader", "true"); //default false
		params.set("cache", "false");     //default true
		
		//parameters only for grouping result
		params.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:grouped
		params.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

		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();
		}
		
		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=false
GroupCommand Name : id
Num of Groups Found: 3
Num of documents Found: 3
The groups are: 
group value: no3
Num of Documents in this group: 1
start: 0
Max score: 1.0
the Fields of document:
id: no3
number: 3
name: 王五
grade: 94.8
age: 25
introduction: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人
text_auto: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人
text: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人
title: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人
title_smart: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人
entranceTime: Wed Jan 05 00:05:00 CST 2005
deposit: 3000.0009
score: 1.0





group value: no4
Num of Documents in this group: 1
start: 0
Max score: 1.0
the Fields of document:
id: no4
number: 4
name: 沈六
grade: 80.8
age: 30
introduction: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabar
text_auto: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabar
text: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabar
title: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabar
title_smart: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabar
entranceTime: Tue Jan 10 00:09:00 CST 2006
deposit: 90000.000788
score: 1.0





group value: no5
Num of Documents in this group: 1
start: 0
Max score: 1.0
the Fields of document:
id: no5
number: 4
name: 江八
grade: 70.8
age: 25
introduction: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafoo
text_auto: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafoo
text: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafoo
title: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafoo
title_smart: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafoo
entranceTime: Wed Jan 10 00:09:00 CST 2007
deposit: 2343900.000788
score: 1.0








GroupCommand Name : age
Num of Groups Found: 2
Num of documents Found: 3
The groups are: 
group value: 25
Num of Documents in this group: 2
start: 0
Max score: 1.0
the Fields of document:
id: no3
number: 3
name: 王五
grade: 94.8
age: 25
introduction: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人
text_auto: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人
text: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人
title: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人
title_smart: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人
entranceTime: Wed Jan 05 00:05:00 CST 2005
deposit: 3000.0009
score: 1.0


the Fields of document:
id: no5
number: 4
name: 江八
grade: 70.8
age: 25
introduction: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafoo
text_auto: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafoo
text: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafoo
title: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafoo
title_smart: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafoo
entranceTime: Wed Jan 10 00:09:00 CST 2007
deposit: 2343900.000788
score: 1.0





group value: 30
Num of Documents in this group: 1
start: 0
Max score: 1.0
the Fields of document:
id: no4
number: 4
name: 沈六
grade: 80.8
age: 30
introduction: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabar
text_auto: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabar
text: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabar
title: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabar
title_smart: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabar
entranceTime: Tue Jan 10 00:09:00 CST 2006
deposit: 90000.000788
score: 1.0








GroupCommand Name : sub(grade, 60)
Num of Groups Found: 3
Num of documents Found: 3
The groups are: 
group value: 34.800003
Num of Documents in this group: 1
start: 0
Max score: 1.0
the Fields of document:
id: no3
number: 3
name: 王五
grade: 94.8
age: 25
introduction: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人
text_auto: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人
text: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人
title: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人
title_smart: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人
entranceTime: Wed Jan 05 00:05:00 CST 2005
deposit: 3000.0009
score: 1.0





group value: 20.800003
Num of Documents in this group: 1
start: 0
Max score: 1.0
the Fields of document:
id: no4
number: 4
name: 沈六
grade: 80.8
age: 30
introduction: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabar
text_auto: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabar
text: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabar
title: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabar
title_smart: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabar
entranceTime: Tue Jan 10 00:09:00 CST 2006
deposit: 90000.000788
score: 1.0





group value: 10.800003
Num of Documents in this group: 1
start: 0
Max score: 1.0
the Fields of document:
id: no5
number: 4
name: 江八
grade: 70.8
age: 25
introduction: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafoo
text_auto: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafoo
text: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafoo
title: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafoo
title_smart: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafoo
entranceTime: Wed Jan 10 00:09:00 CST 2007
deposit: 2343900.000788
score: 1.0








GroupCommand Name : 学生
Num of Groups Found: null
Num of documents Found: 3
The groups are: 
group value: 学生
Num of Documents in this group: 1
start: 0
Max score: 1.0
the Fields of document:
id: no3
number: 3
name: 王五
grade: 94.8
age: 25
introduction: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人
text_auto: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人
text: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人
title: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人
title_smart: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人
entranceTime: Wed Jan 05 00:05:00 CST 2005
deposit: 3000.0009
score: 1.0








GroupCommand Name : 学习
Num of Groups Found: null
Num of documents Found: 3
The groups are: 
group value: 学习
Num of Documents in this group: 2
start: 0
Max score: 1.0
the Fields of document:
id: no3
number: 3
name: 王五
grade: 94.8
age: 25
introduction: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人
text_auto: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人
text: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人
title: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人
title_smart: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人
entranceTime: Wed Jan 05 00:05:00 CST 2005
deposit: 3000.0009
score: 1.0


the Fields of document:
id: no5
number: 4
name: 江八
grade: 70.8
age: 25
introduction: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafoo
text_auto: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafoo
text: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafoo
title: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafoo
title_smart: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafoo
entranceTime: Wed Jan 10 00:09:00 CST 2007
deposit: 2343900.000788
score: 1.0








GroupCommand Name : grade:[0 TO 59.9]
Num of Groups Found: null
Num of documents Found: 3
The groups are: 
group value: grade:[0 TO 59.9]
Num of Documents in this group: 0
start: 0
Max score: NaN






GroupCommand Name : grade:[60 TO *]
Num of Groups Found: null
Num of documents Found: 3
The groups are: 
group value: grade:[60 TO *]
Num of Documents in this group: 3
start: 0
Max score: 1.0
the Fields of document:
id: no3
number: 3
name: 王五
grade: 94.8
age: 25
introduction: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人
text_auto: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人
text: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人
title: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人
title_smart: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人
entranceTime: Wed Jan 05 00:05:00 CST 2005
deposit: 3000.0009
score: 1.0


the Fields of document:
id: no4
number: 4
name: 沈六
grade: 80.8
age: 30
introduction: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabar
text_auto: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabar
text: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabar
title: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabar
title_smart: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabar
entranceTime: Tue Jan 10 00:09:00 CST 2006
deposit: 90000.000788
score: 1.0


the Fields of document:
id: no5
number: 4
name: 江八
grade: 70.8
age: 25
introduction: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafoo
text_auto: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafoo
text: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafoo
title: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafoo
title_smart: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafoo
entranceTime: Wed Jan 10 00:09:00 CST 2007
deposit: 2343900.000788
score: 1.0








GroupCommand Name : age:[10 TO 19]
Num of Groups Found: null
Num of documents Found: 3
The groups are: 
group value: age:[10 TO 19]
Num of Documents in this group: 0
start: 0
Max score: NaN






GroupCommand Name : age:[20 TO *]
Num of Groups Found: null
Num of documents Found: 3
The groups are: 
group value: age:[20 TO *]
Num of Documents in this group: 3
start: 0
Max score: 1.0
the Fields of document:
id: no3
number: 3
name: 王五
grade: 94.8
age: 25
introduction: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人
text_auto: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人
text: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人
title: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人
title_smart: bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人
entranceTime: Wed Jan 05 00:05:00 CST 2005
deposit: 3000.0009
score: 1.0


the Fields of document:
id: no4
number: 4
name: 沈六
grade: 80.8
age: 30
introduction: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabar
text_auto: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabar
text: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabar
title: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabar
title_smart: bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabar
entranceTime: Tue Jan 10 00:09:00 CST 2006
deposit: 90000.000788
score: 1.0


the Fields of document:
id: no5
number: 4
name: 江八
grade: 70.8
age: 25
introduction: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafoo
text_auto: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafoo
text: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafoo
title: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafoo
title_smart: bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafoo
entranceTime: Wed Jan 10 00:09:00 CST 2007
deposit: 2343900.000788
score: 1.0








查询耗时:0




  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值