@Test public void testTermAllGroupHeadsCollector() throws Exception { String groupField = "title"; Sort sortWithinGroup = new Sort(); searcherManager.maybeRefresh(); IndexSearcher searcher = searcherManager.acquire(); // Render groupsResult... Note that the groupValue of each GroupDocs will be null, so if you need to present this value you'll have to separately retrieve it (for example using stored fields, FieldCache, etc.). //Another collector is the TermAllGroupHeadsCollector that can be used to retrieve all most relevant documents per group. Also known as group heads. This can be useful in situations when one wants to compute group based facets / statistics on the complete query result. The collector can be executed during the first or second phase. This collector can also be used with the GroupingSearch convenience utility, but when if one only wants to compute the most relevant documents per group it is better to just use the collector as done here below. AbstractAllGroupHeadsCollector collector = TermAllGroupHeadsCollector.create(groupField, sortWithinGroup); searcher.search(new TermQuery(new Term("title", "Lucene")), collector); // Return all group heads as int array int[] groupHeadsArray = collector.retrieveGroupHeads(); // Return all group heads as FixedBitSet. int maxDoc = searcher.getIndexReader().maxDoc(); FixedBitSet groupHeadsBitSet = collector.retrieveGroupHeads(maxDoc); }
private DocSet computeGroupedDocSet(Query query, ProcessedFilter filter, List<Collector> collectors) throws IOException { Command firstCommand = commands.get(0); AbstractAllGroupHeadsCollector termAllGroupHeadsCollector = TermAllGroupHeadsCollector.create(firstCommand.getKey(), firstCommand.getSortWithinGroup()); if (collectors.isEmpty()) { searchWithTimeLimiter(query, filter, termAllGroupHeadsCollector); } else { collectors.add(termAllGroupHeadsCollector); searchWithTimeLimiter(query, filter, MultiCollector.wrap(collectors.toArray(new Collector[collectors.size()]))); } return new BitDocSet(termAllGroupHeadsCollector.retrieveGroupHeads(searcher.maxDoc())); }
private DocSet computeGroupedDocSet(Query query, ProcessedFilter filter, List<Collector> collectors) throws IOException { Command firstCommand = commands.get(0); AbstractAllGroupHeadsCollector termAllGroupHeadsCollector = TermAllGroupHeadsCollector.create(firstCommand.getKey(), firstCommand.getSortWithinGroup()); if (collectors.isEmpty()) { searchWithTimeLimiter(query, filter, termAllGroupHeadsCollector); } else { collectors.add(termAllGroupHeadsCollector); searchWithTimeLimiter(query, filter, MultiCollector.wrap(collectors.toArray(new Collector[collectors.size()]))); } int maxDoc = searcher.maxDoc(); long[] bits = termAllGroupHeadsCollector.retrieveGroupHeads(maxDoc).getBits(); return new BitDocSet(new OpenBitSet(bits, bits.length)); }
/** * {@inheritDoc} */ @Override public AbstractAllGroupHeadsCollector<?> createAllGroupCollector() throws IOException { Sort sortWithinGroup = groupSort != null ? groupSort : new Sort(); return TermAllGroupHeadsCollector.create(groupBy, sortWithinGroup); }