/** * Creates an <code>AbstractAllGroupHeadsCollector</code> instance based on the supplied arguments. * This factory method decides with implementation is best suited. * * @param groupField The field to group by * @param sortWithinGroup The sort within each group * @param initialSize The initial allocation size of the internal int set and group list which should roughly match * the total number of expected unique groups. Be aware that the heap usage is * 4 bytes * initialSize. * @return an <code>AbstractAllGroupHeadsCollector</code> instance based on the supplied arguments */ public static AbstractAllGroupHeadsCollector<?> create(String groupField, Sort sortWithinGroup, int initialSize) { boolean sortAllScore = true; boolean sortAllFieldValue = true; for (SortField sortField : sortWithinGroup.getSort()) { if (sortField.getType() == SortField.Type.SCORE) { sortAllFieldValue = false; } else if (needGeneralImpl(sortField)) { return new GeneralAllGroupHeadsCollector(groupField, sortWithinGroup); } else { sortAllScore = false; } } if (sortAllScore) { return new ScoreAllGroupHeadsCollector(groupField, sortWithinGroup, initialSize); } else if (sortAllFieldValue) { return new OrdAllGroupHeadsCollector(groupField, sortWithinGroup, initialSize); } else { return new OrdScoreAllGroupHeadsCollector(groupField, sortWithinGroup, initialSize); } }
@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); }
@Override public AbstractAllGroupHeadsCollector<?> createAllGroupCollector() throws IOException { Sort sortWithinGroup = groupSort != null ? groupSort : new Sort(); return new FunctionAllGroupHeadsCollector(groupBy, context, sortWithinGroup); }
/** * Creates an <code>AbstractAllGroupHeadsCollector</code> instance based on the supplied arguments. * This factory method decides with implementation is best suited. * * Delegates to {@link #create(String, org.apache.lucene.search.Sort, int)} with an initialSize of 128. * * @param groupField The field to group by * @param sortWithinGroup The sort within each group * @return an <code>AbstractAllGroupHeadsCollector</code> instance based on the supplied arguments */ public static AbstractAllGroupHeadsCollector<?> create(String groupField, Sort sortWithinGroup) { return create(groupField, sortWithinGroup, DEFAULT_INITIAL_SIZE); }
/** * Returns a collector that is able to return the most relevant document of all groups. * Returns <code>null</code> if the command doesn't support this type of collector. * * @return a collector that is able to return the most relevant document of all groups. * @throws IOException If I/O related errors occur */ public AbstractAllGroupHeadsCollector<?> createAllGroupCollector() throws IOException { return null; }