GroupingSearch를 사용하여 검색 결과를 그룹화합니다
패키지 org.apache.lucene.search.grouping 설명
이 모듈은 Lucene의 검색 결과를 그룹화 할 수 있으며 지정된 단일 값 필드가 함께 수집됩니다. 예를 들어, "저자"필드에 따른 그룹은 동일한 "저자"필드 값을 가진 문서가 그룹으로 나뉩니다.
그룹화 할 때 필요한 정보를 입력해야합니다.
1. Groupfield :이 필드에 따라 그룹. 예를 들어, "저자"필드를 그룹에 사용하는 경우 각 그룹의 책은 동일한 저자입니다. 이 도메인이없는 문서는 별도의 그룹으로 나뉩니다.
2. Groupsort : 그룹 분류.
3. Topngroups : 얼마나 많은 그룹이 유지됩니까? 예를 들어, 10은 처음 10 개의 그룹 만 유지됨을 의미합니다.
4. GroupOffset : 어떤 그룹 그룹이 먼저 순위를 매기는지 검색합니다. 예를 들어, 3은 7 개의 그룹을 반환하는 것을 의미합니다 (OPNGroup이 10과 같다고 가정). 페이지 당 5 개의 그룹 만 표시되는 것이기에 매우 유용합니다.
5. WithingRoupSort : 문서를 그룹으로 정렬하십시오. 참고 : 여기와 Groupsort의 차이점
6. withgroupOffset : 각 그룹에서 첫 번째 문서를 검색합니다.
검색 결과 그룹화는 GroupingSearch를 사용하는 것이 더 간단합니다
Groupingsearch API 문서 소개 :
비 분산 환경에서 그룹화를 수행하는 편의 클래스.
비 분산 환경에서 그룹화
경고 :이 API는 실험적이며 다음 릴리스에서 호환되지 않는 방식으로 변경 될 수 있습니다.
버전 4.3.1은 여기에서 사용됩니다
몇 가지 중요한 방법 :
샘플 코드 :
1. 먼저 인덱스 코드를보십시오
Public Class IndexHelper {개인 문서 문서; 개인 디렉토리 디렉토리; 개인 인덱스 라이터 인덱스 라이터; 공개 디렉토리 getDirectory () {directory = (directory == null)? New Ramdirectory () : 디렉토리; 리턴 디렉토리; } private indexwriterConfig getConfig () {return new IndexWriterConfig (version.lucene_43, new ikanalyzer (true)); } private indexwriter getIndexWriter () {try {return new Indexwriter (getDirectory (), getConfig ()); } catch (ioexception e) {e.printstacktrace (); 널 리턴; }} public indexsearcher getIndexSearcher ()는 ioException {return new IndexSearcher (driercoryReader.Open (getDirectory ())); } / ** * 그룹 테스트를위한 색인 생성 * @param author * @param content * / public void createIndexForGroup (int id, String awork, String content) {indexWriter = getIndexWriter (); document = 새 문서 (); document.add (new intfield ( "id", id, field.store.yes)); document.add (new Stringfield ( "저자", 저자, field.store.yes)); document.add (new Textfield ( "내용", 내용, field.store.yes); try {indexwriter.addDocument (document); indexwriter.commit (); indexwriter.close (); } catch (ioexception e) {e.printstacktrace (); }}}2. 그룹화 :
공개 클래스 GrouptestPublic Void Group (IndexSearcher IndexSearcher, String Groupfield, String Content) IoException, parseException {GroupingSearch GroupingSearch = New GroupingSearch (Groupfield); GroupingSearch.setGroupSort (New Sort (Sortfield.field_score)); Groupingsearch.SetFillsortFields (True); Groupingsearch.setCachingInmb (4.0, true); Groupingsearch.setAllGroups (true); //groupingsearch.setallgroupheads(true); Groupingsearch.SetGroupDocSlimit (10); QueryParser Parser = New QueryParser (version.lucene_43, "Content", New Ikanalyzer (true)); query query = parser.parse (컨텐츠); TOPGROUPS <BYTESREF> result = GroupingSearch.search (IndexSearcher, Query, 0, 1000); System.out.println ( "검색 히트 :" + result.totalHitCount); System.out.println ( "검색 결과 그룹화 :" + result.groups.length); 문서 문서; for (groupDocs <BytesRef> GroupDocs : result.groups) {System.out.println ( "Group :" + groupDocs.groupValue.utf8toString ()); System.out.println ( "그룹 내 레코드 :" + groupdocs.totalhits); //system.out.println("groupdocs.scoredocs.length : " + groupdocs.scoredocs.length); for (scoredoc scoredoc : groupdocs.scoredocs) {system.out.println (indexsearcher.doc (scoredoc.doc)); }}}3. 간단한 테스트 :
public static void main (String [] args)은 ioexception, parseexception {indexHelper indexHelper = new IndexHelper (); indexhelper.createindexforgroup (1, "고구마", "오픈 소스 중국"); indexhelper.createindexforgroup (2, "고구마", "오픈 소스 커뮤니티"); indexhelper.createindexforgroup (3, "고구마", "코드 디자인"); indexhelper.createindexforgroup (4, "고구마", "디자인"); indexhelper.createindexforgroup (5, "jiexian", "Lucene Development"); indexhelper.createIndexforGroup (6, "Jiexian", "Lucene 실제 전투"); indexhelper.createIndexforGroup (7, "jiexian", "오픈 소스 루센"); indexhelper.createIndexForGroup (8, "jiexian", "오픈 소스 Solr"); indexhelper.createIndexForGroup (9, "산 넥스", "산 넥스 오픈 소스 루센"); indexHelper.CreateIndexForGroup (10, "Sanxian", "Sanxian 오픈 소스 솔라"); indexhelper.createIndexForGroup (11, "Sanxian", "오픈 소스"); grouptest grouptest = new grouptest (); grouptest.group (indexHelper.getIndexSearcher (), "author", "오픈 소스"); }} 4. 테스트 결과 :
페이징의 두 가지 방법
루센은 두 가지 페이징 방법을 가지고 있습니다.
1. 검색 결과를 직접적으로 이끄는 경우. 이 방법은 데이터 볼륨이 비교적 작을 때 사용할 수 있습니다. 페이징 코드의 핵심 참조는 다음과 같습니다.
scoredoc [] sd = xxx; // 쿼리 시작 레코드 위치 int int int int = pagesize * (currentpage -1); // 쿼리 레코드 위치 종료 int int int = math.min (시작+pagesize, sd.length); for (int i = 시작; i <attalhits; i ++) {// 검색 결과 데이터 처리를위한 코드}2. SearchAfter (...) 사용
Lucene은 필요에 따라 사용할 수있는 5 가지 과부하 방법을 제공합니다.
스코어 토 후 : 마지막 검색 결과에 대한 총 스코어 양을 1로 줄입니다.
쿼리 쿼리 : 쿼리 메소드
int n : 각 쿼리에 대해 반환 된 결과 수, 즉 페이지 당 총 결과 수입니다.
사용의 간단한 예 :
// 맵을 사용하여 필요한 검색 결과를 저장할 수 있습니다. <문자열, 개체> resultmap = new Hashmap <string, object> (); scoredoc apj // hit number resultmap.put를 얻습니다 ( "num", td.totalhits); 스코어 진 [] sd = td.scoredocs; for (scoredoc scoredoc : sd) {// classic search result processing} // 검색 결과 Scredocs 총 금액은 1 후 1 씩 줄어 듭니다 [td.scoredocs.length -1]; // 다음 검색을 위해 후에 저장, 즉 다음 페이지는 resultMap.put ( "After", After)을 시작합니다. 반환 결과 맵;