solrj solr 의 group 조회 실현:

위 키 참조:http://wiki.apache.org/solr/FieldCollapsing#Result_Grouping_.2BAC8_Field_Collapsing
1. 실 현 된 관련 클래스: SolrServer, SolrQuery, Query Response, 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) Query Response 클래스 가 조회 한 결 과 를 제공 합 니 다.
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 = 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("
"); } System.out.println("

"); } System.out.println("

"); } }

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("
"); } System.out.println("

"); } System.out.println("

"); } } //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

좋은 웹페이지 즐겨찾기