使用Java的Lucene搜尋工具對檢索結果進行分組和分頁_java

來源:互聯網
上載者:User

使用GroupingSearch對搜尋結果進行分組
Package org.apache.lucene.search.grouping Description

這個模組可以對Lucene的搜尋結果進行分組,指定的單範圍被聚集到一起。比如,根據”author“域進行分組,“author”域值相同的的文檔分成一個組。

進行分組的時候需要輸入一些必要的資訊:

1、groupField:根據這個域進行分組。比如,如果你使用“author”域進行分組,那麼每一個組裡面的書籍都是同一個作者。沒有這個域的文檔將被分到一個單獨的組裡面。

2、groupSort:組排序。

3、topNGroups:保留多少組。比如,10表示只保留前10組。

4、groupOffset:對排在前面的哪些分組組進行檢索。比如,3表示返回7個組(假設opNGroups等於10)。在分頁裡面很有用,比如每頁只顯示5個組。

5、withinGroupSort:組內文檔排序。注意:這裡和groupSort的區別

6、withingroupOffset:對每一個分組裡面的哪些排在前面的文檔進行檢索。


使用GroupingSearch 對搜尋結果分組比較簡單

GroupingSearch API文檔介紹:

Convenience class to perform grouping in a non distributed environment.

非分布式環境下分組

WARNING: This API is experimental and might change in incompatible ways in the next release.

這裡使用的是4.3.1版本

一些重要的方法:

  • GroupingSearch:setCaching(int maxDocsToCache, boolean cacheScores) 緩衝
  • GroupingSearch:setCachingInMB(double maxCacheRAMMB, boolean cacheScores) 緩衝第一次搜尋結果,用於第二次搜尋
  • GroupingSearch:setGroupDocsLimit(int groupDocsLimit) 指定每組返回的文檔數,不指定時,預設返回一個文檔
  • GroupingSearch:setGroupSort(Sort groupSort) 指定分組排序

範例程式碼:

1.先看建索引的代碼

public class IndexHelper {  private Document document;  private Directory directory;  private IndexWriter indexWriter;   public Directory getDirectory(){    directory=(directory==null)? new RAMDirectory():directory;    return directory;  }   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();      return null;    }  }   public IndexSearcher getIndexSearcher() throws IOException {    return new IndexSearcher(DirectoryReader.open(getDirectory()));  }   /**   * Create index for group test   * @param author   * @param content   */  public void createIndexForGroup(int id,String author,String content) {    indexWriter = getIndexWriter();    document = new Document();    document.add(new IntField("id",id, Field.Store.YES));    document.add(new StringField("author", author, Field.Store.YES));    document.add(new TextField("content", content, Field.Store.YES));    try {      indexWriter.addDocument(document);      indexWriter.commit();      indexWriter.close();    } catch (IOException e) {      e.printStackTrace();    }  }}

2.分組:

public class GroupTestpublic void group(IndexSearcher indexSearcher,String groupField,String content) throws 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(content);     TopGroups<BytesRef> result = groupingSearch.search(indexSearcher, query, 0, 1000);     System.out.println("搜尋命中數:" + result.totalHitCount);    System.out.println("搜尋結果分組數:" + result.groups.length);     Document document;    for (GroupDocs<BytesRef> groupDocs : result.groups) {      System.out.println("分組:" + 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) throws IOException, ParseException {    IndexHelper indexHelper = new IndexHelper();    indexHelper.createIndexForGroup(1,"紅薯", "開源中國");    indexHelper.createIndexForGroup(2,"紅薯", "開源社區");    indexHelper.createIndexForGroup(3,"紅薯", "代碼設計");    indexHelper.createIndexForGroup(4,"紅薯", "設計");    indexHelper.createIndexForGroup(5,"覺先", "Lucene開發");    indexHelper.createIndexForGroup(6,"覺先", "Lucene實戰");    indexHelper.createIndexForGroup(7,"覺先", "開源Lucene");    indexHelper.createIndexForGroup(8,"覺先", "開源solr");     indexHelper.createIndexForGroup(9,"散仙", "散仙開源Lucene");    indexHelper.createIndexForGroup(10,"散仙", "散仙開源solr");    indexHelper.createIndexForGroup(11,"散仙", "開源");    GroupTest groupTest = new GroupTest();     groupTest.group(indexHelper.getIndexSearcher(),"author", "開源");  }}

4.測試結果:

兩種分頁方式
Lucene有兩種分頁方式:

1、直接對搜尋結果進行分頁,資料量比較少的時候可以用這種方式,分頁代碼核心參照:


ScoreDoc[] sd = XXX;// 查詢起始記錄位置int begin = pageSize * (currentPage - 1);// 查詢終止記錄位置int end = Math.min(begin + pageSize, sd.length);for (int i = begin; i < end && i <totalHits; i++) {//對搜尋結果資料進行處理的代碼}

2、使用searchAfter(...)

Lucene提供了五個重載方法,可以根據需要使用

ScoreDoc after:為上次搜尋結果ScoreDoc總量減1;

Query query:查詢方式

int n:為每次查詢返回的結果數,即每頁的結果總量

一個簡單的使用樣本:

//可以使用Map儲存必要的搜尋結果Map<String, Object> resultMap = new HashMap<String, Object>();ScoreDoc after = null;Query query = XXTopDocs td = search.searchAfter(after, query, size); //擷取命中數resultMap.put("num", td.totalHits); ScoreDoc[] sd = td.scoreDocs;for (ScoreDoc scoreDoc : sd) {//經典的搜尋結果處理}//搜尋結果ScoreDoc總量減1after = sd[td.scoreDocs.length - 1]; //儲存after用於下次搜尋,即下一頁開始 resultMap.put("after", after); return resultMap;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.