http://sujitpal.blogspot.com/2007/04/lucene-search-within-search-with.html
http://blog.csdn.net/lansine2005/article/details/6787472
 

Lucene聚类分组统计功能(grouping)

分类: Lucene 229人阅读 评论(1) 收藏 举报
  1. FirstPassGroupingCollector c1 = new FirstPassGroupingCollector("author", groupSort, groupOffset + topNGroups);  
  2. indexSearcher.search(new TermQuery(new Term("content", searchTerm)), c1);  
  3.    
  4. Collection<SearchGroup> topGroups = c1.getTopGroups(groupOffset, fillFields);  
  5.    
  6. if (topGroups == null) {  
  7.    // No groups matched  
  8.   return;  
  9. }  
  10.    
  11. boolean getScores = true;  
  12. boolean getMaxScores = true;  
  13. boolean fillFields = true;  
  14. SecondPassGroupingCollector c2 = new SecondPassGroupingCollector("author", topGroups, groupSort, docSort, docOffset + docsPerGroup, getScores, getMaxScores, fillFields);  
  15.     indexSearcher.search(new TermQuery(new Term("content", searchTerm)), c2);  
  16.    
  17. TopGroups groupsResult = c2.getTopGroups(docOffset);  



如果search的性能耗费大,则可以考虑使用CachingCollector。这个cache可以缓存第一次search时的文档Id和评分,并提供给之后的查询使用。使用方法请参考grouping documentation


有另一个收集器叫AllGroupsCollector,它可以收集查询结果的所有组。下面的例子为得到各个分组收集的总数量:

  1. // First pass search has been executed  
  2. boolean getScores = true;  
  3. boolean getMaxScores = true;  
  4. boolean fillFields = true;  
  5. AllGroupsCollector c3 = new AllGroupsCollector("author");  
  6. SecondPassGroupingCollector c2 = new SecondPassGroupingCollector("author", topGroups, groupSort, docSort, docOffset + docsPerGroup, getScores, getMaxScores, fillFields);  
  7. indexSearcher.search(new TermQuery(new Term("content", searchTerm)), MultiCollector.wrap(c2, c3));  
  8.    
  9. TopGroups groupsResult = c2.getTopGroups(docOffset);  
  10. groupsResult = new TopGroups(groupsResult, c3.getGroupCount());  



通过MultiCollectorAllGroupsCollector能很好的封装SecondPassGroupingCollectorAllGroupsCollector 也能独立的在其他的收集器中使用。

结果分组在solr中的使用
现在Solr主干上还没有使用lucene 分组模块;它使用它自己的分组模块。Solr还没有使用lucene的分组模块的原因,是它还没有对function和query的支持。然而Solr3.1还没有实现分组的支持,用户仍需要自己下分支然后编译。更坏的是,很多用户还在使用过时的补丁SOLR-236,这是我为什么创建SOLR-2524的原因.

SOLR-2524 分支涉及到集成lucene的扩展包到solr3.x分支中。这个版本也为集成分组模块的主干版本到Solr4.0中作为参考。Solr3.x的主干分组将支持相同的响应格式和请求参数,具体描述请参见Solr FieldCollapse wiki page;它还不支持的功能还是function和query。

如果运行良好,这个分支将会提交到Solr3.2版本中,Solr的用户就可以直接使用啦

相关文章:

  • 2021-08-21
  • 2021-07-20
  • 2021-07-16
  • 2022-12-23
  • 2021-07-27
  • 2021-08-26
猜你喜欢
  • 2022-01-01
  • 2021-09-06
  • 2022-12-23
  • 2021-05-20
  • 2022-01-10
  • 2022-12-23
  • 2021-08-26
相关资源
相似解决方案