-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Introduce AllGroupHeadsCollectorManager #15565
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
1df552e
Introduce AllGroupHeadsCollectorManager
gaobinlong 75314b4
Modify change log
gaobinlong b60e77c
Remove unused field in GroupHeadWithValue
gaobinlong 8134a06
merge main
gaobinlong 65a0478
x
gaobinlong 0ff1320
merge main
gaobinlong 17cc637
Optimize some code
gaobinlong bb3a689
Modify change log
gaobinlong 12bb09d
Merge remote-tracking branch 'upstream/main' into allGroupHeads
gaobinlong d71d4f3
Optimize some code
gaobinlong 04ef4cf
Fix class modifier
gaobinlong c319a8d
Optimize some code
gaobinlong 96bbade
merge main
gaobinlong 2622584
Cover custom FieldComparator case when merging group heads
gaobinlong c9a6275
Merge remote-tracking branch 'upstream/main' into allGroupHeads
gaobinlong 5c22124
Do not share context across collectors for ValueSourceGroupSelector
gaobinlong 9174c26
Add more test
gaobinlong c96767a
Merge remote-tracking branch 'upstream/main' into allGroupHeads
gaobinlong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
...ne/grouping/src/java/org/apache/lucene/search/grouping/AllGroupHeadsCollectorManager.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.lucene.search.grouping; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Collection; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.function.Supplier; | ||
| import org.apache.lucene.index.IndexReader; | ||
| import org.apache.lucene.search.CollectorManager; | ||
| import org.apache.lucene.search.FieldComparator; | ||
| import org.apache.lucene.search.Sort; | ||
| import org.apache.lucene.search.SortField; | ||
| import org.apache.lucene.util.Bits; | ||
| import org.apache.lucene.util.FixedBitSet; | ||
|
|
||
| /** | ||
| * A {@link CollectorManager} implementation for {@link AllGroupHeadsCollector} that collects the | ||
| * most relevant document (group head) for each group across multiple segments and merges the | ||
| * per-segment results into a single {@link GroupHeadsResult}. | ||
| * | ||
| * <p>Example usage: | ||
| * | ||
| * <pre class="prettyprint"> | ||
| * IndexSearcher searcher = ...; // your IndexSearcher | ||
| * AllGroupHeadsCollectorManager<BytesRef> manager = | ||
| * new AllGroupHeadsCollectorManager<>( | ||
| * () -> new TermGroupSelector("category"), Sort.RELEVANCE); | ||
| * GroupHeadsResult result = searcher.search(new MatchAllDocsQuery(), manager); | ||
| * Bits groupHeadsBits = result.retrieveGroupHeads(searcher.getIndexReader().maxDoc()); | ||
| * </pre> | ||
| * | ||
| * @param <T> the type of the group value | ||
| * @lucene.experimental | ||
| */ | ||
| public class AllGroupHeadsCollectorManager<T> | ||
| implements CollectorManager< | ||
| AllGroupHeadsCollector<T>, AllGroupHeadsCollectorManager.GroupHeadsResult> { | ||
|
|
||
| /** Holds the merged group heads and provides access as an {@code int[]} or {@link Bits}. */ | ||
| public static class GroupHeadsResult { | ||
| private final int[] groupHeads; | ||
|
|
||
| private GroupHeadsResult(int[] groupHeads) { | ||
| this.groupHeads = groupHeads; | ||
| } | ||
|
|
||
| /** Returns the group head document IDs as an array. */ | ||
| public int[] retrieveGroupHeads() { | ||
| return groupHeads; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the group head document IDs as a {@link Bits} set of size {@code maxDoc}, suitable | ||
| * for use as a filter. | ||
| * | ||
| * @param maxDoc The maxDoc of the top level {@link IndexReader}. | ||
| */ | ||
| public Bits retrieveGroupHeads(int maxDoc) { | ||
| FixedBitSet result = new FixedBitSet(maxDoc); | ||
| for (int docId : groupHeads) { | ||
| result.set(docId); | ||
| } | ||
| return result; | ||
| } | ||
| } | ||
|
|
||
| private static final class GroupHeadWithValues { | ||
| int doc; | ||
| final Object[] sortValues; | ||
|
|
||
| GroupHeadWithValues(int doc, Object[] sortValues) { | ||
| this.doc = doc; | ||
| this.sortValues = sortValues; | ||
| } | ||
| } | ||
|
|
||
| private final Supplier<GroupSelector<T>> groupSelectorFactory; | ||
| private final Sort sortWithinGroup; | ||
|
|
||
| /** | ||
| * Creates a new AllGroupHeadsCollectorManager. | ||
| * | ||
| * @param groupSelectorFactory factory to create group selectors for each collector | ||
| * @param sortWithinGroup the sort to use within each group to determine the group head | ||
| */ | ||
| public AllGroupHeadsCollectorManager( | ||
| Supplier<GroupSelector<T>> groupSelectorFactory, Sort sortWithinGroup) { | ||
| this.groupSelectorFactory = groupSelectorFactory; | ||
| this.sortWithinGroup = sortWithinGroup; | ||
| } | ||
|
|
||
| @Override | ||
| public AllGroupHeadsCollector<T> newCollector() throws IOException { | ||
| return AllGroupHeadsCollector.newCollector(groupSelectorFactory.get(), sortWithinGroup); | ||
| } | ||
|
|
||
| @Override | ||
| public GroupHeadsResult reduce(Collection<AllGroupHeadsCollector<T>> collectors) { | ||
| Map<T, GroupHeadWithValues> mergedHeads = new HashMap<>(); | ||
| SortField[] sortFields = sortWithinGroup.getSort(); | ||
|
|
||
| for (AllGroupHeadsCollector<T> collector : collectors) { | ||
| mergeCollectorHeads(collector, mergedHeads, sortFields); | ||
| } | ||
|
|
||
| return new GroupHeadsResult(mergedHeads.values().stream().mapToInt(h -> h.doc).toArray()); | ||
| } | ||
|
|
||
| private void mergeCollectorHeads( | ||
| AllGroupHeadsCollector<T> collector, | ||
| Map<T, GroupHeadWithValues> mergedHeads, | ||
| SortField[] sortFields) { | ||
| for (AllGroupHeadsCollector.GroupHead<T> head : collector.getCollectedGroupHeads()) { | ||
| Object[] sortValues = head.getSortValues(); | ||
| GroupHeadWithValues existing = mergedHeads.get(head.groupValue); | ||
| if (existing == null || isCompetitive(head, sortValues, existing, sortFields)) { | ||
| mergedHeads.put(head.groupValue, new GroupHeadWithValues(head.doc, sortValues)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @SuppressWarnings({"rawtypes"}) | ||
| private boolean isCompetitive( | ||
| AllGroupHeadsCollector.GroupHead<T> head, | ||
| Object[] sortValues, | ||
| GroupHeadWithValues existing, | ||
| SortField[] sortFields) { | ||
| FieldComparator[] comparators = head.getComparators(); | ||
| int cmp; | ||
| if (sortWithinGroup.equals(Sort.RELEVANCE)) { | ||
| cmp = Float.compare((float) sortValues[0], (float) existing.sortValues[0]); | ||
| return cmp > 0 || (cmp == 0 && head.doc < existing.doc); | ||
| } else { | ||
| cmp = 0; | ||
| for (int i = 0; i < sortFields.length; i++) { | ||
| @SuppressWarnings({"unchecked"}) | ||
| int c = comparators[i].compareValues(sortValues[i], existing.sortValues[i]); | ||
| c = sortFields[i].getReverse() ? -c : c; | ||
| if (c != 0) { | ||
| cmp = c; | ||
| break; | ||
| } | ||
| } | ||
| return cmp < 0 || (cmp == 0 && head.doc < existing.doc); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.