-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Introduce support for pruning and skipping to FirstPassGroupingCollector #15210
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
Open
alexmm-amzn
wants to merge
4
commits into
apache:main
Choose a base branch
from
alexmm-amzn:pr/15136-2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+99
−29
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3cfd7aa
Add pruning support for FirstPassGroupingCollector (#15136)
alexmm-amzn e618ab3
Add support for skipping over non-competitive documents in FirstPassG…
alexmm-amzn 6c29e6f
Fix TestGrouping unit tests to use ScoreMode that is aligned with the…
alexmm-amzn 74417a2
Update CHANGES.txt (#15136)
alexmm-amzn 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
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
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 |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ | |
| import java.util.HashMap; | ||
| import java.util.TreeSet; | ||
| import org.apache.lucene.index.LeafReaderContext; | ||
| import org.apache.lucene.search.DocIdSetIterator; | ||
| import org.apache.lucene.search.FieldComparator; | ||
| import org.apache.lucene.search.LeafFieldComparator; | ||
| import org.apache.lucene.search.Pruning; | ||
|
|
@@ -49,9 +50,10 @@ public class FirstPassGroupingCollector<T> extends SimpleCollector { | |
| private final LeafFieldComparator[] leafComparators; | ||
| private final int[] reversed; | ||
| private final int topNGroups; | ||
| private final boolean needsScores; | ||
| private final HashMap<T, CollectedSearchGroup<T>> groupMap; | ||
| private final int compIDXEnd; | ||
| private final ScoreMode scoreMode; | ||
| private final boolean canSetMinScore; | ||
|
|
||
| // Set once we reach topNGroups unique groups: | ||
| /** | ||
|
|
@@ -61,6 +63,9 @@ public class FirstPassGroupingCollector<T> extends SimpleCollector { | |
|
|
||
| private int docBase; | ||
| private int spareSlot; | ||
| private Scorable scorer; | ||
| private int bottomSlot; | ||
| private float minCompetitiveScore; | ||
|
|
||
| /** | ||
| * Create the first pass collector. | ||
|
|
@@ -82,7 +87,6 @@ public FirstPassGroupingCollector( | |
| // and specialize it? | ||
|
|
||
| this.topNGroups = topNGroups; | ||
| this.needsScores = groupSort.needsScores(); | ||
| final SortField[] sortFields = groupSort.getSort(); | ||
| comparators = new FieldComparator<?>[sortFields.length]; | ||
| leafComparators = new LeafFieldComparator[sortFields.length]; | ||
|
|
@@ -91,19 +95,34 @@ public FirstPassGroupingCollector( | |
| for (int i = 0; i < sortFields.length; i++) { | ||
| final SortField sortField = sortFields[i]; | ||
|
|
||
| final Pruning pruning; | ||
| if (i == 0) { | ||
| pruning = compIDXEnd >= 0 ? Pruning.GREATER_THAN : Pruning.GREATER_THAN_OR_EQUAL_TO; | ||
| } else { | ||
| pruning = Pruning.NONE; | ||
| } | ||
|
|
||
| // use topNGroups + 1 so we have a spare slot to use for comparing (tracked by | ||
| // this.spareSlot): | ||
| comparators[i] = sortField.getComparator(topNGroups + 1, Pruning.NONE); | ||
| comparators[i] = sortField.getComparator(topNGroups + 1, pruning); | ||
| reversed[i] = sortField.getReverse() ? -1 : 1; | ||
| } | ||
|
|
||
| if (SortField.FIELD_SCORE.equals(sortFields[0]) == true) { | ||
| scoreMode = ScoreMode.TOP_SCORES; | ||
| canSetMinScore = true; | ||
| } else { | ||
| scoreMode = groupSort.needsScores() ? ScoreMode.TOP_DOCS_WITH_SCORES : ScoreMode.TOP_DOCS; | ||
| canSetMinScore = false; | ||
|
Comment on lines
+115
to
+116
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need |
||
| } | ||
|
|
||
| spareSlot = topNGroups; | ||
| groupMap = CollectionUtil.newHashMap(topNGroups); | ||
| } | ||
|
|
||
| @Override | ||
| public ScoreMode scoreMode() { | ||
| return needsScores ? ScoreMode.COMPLETE : ScoreMode.COMPLETE_NO_SCORES; | ||
| return scoreMode; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -154,10 +173,12 @@ public Collection<SearchGroup<T>> getTopGroups(int groupOffset) throws IOExcepti | |
|
|
||
| @Override | ||
| public void setScorer(Scorable scorer) throws IOException { | ||
| this.scorer = scorer; | ||
| groupSelector.setScorer(scorer); | ||
| for (LeafFieldComparator comparator : leafComparators) { | ||
| comparator.setScorer(scorer); | ||
| } | ||
| setMinCompetitiveScore(scorer); | ||
| } | ||
|
|
||
| private boolean isCompetitive(int doc) throws IOException { | ||
|
|
@@ -238,6 +259,9 @@ private void collectNewGroup(final int doc) throws IOException { | |
| // number of groups; from here on we will drop | ||
| // bottom group when we insert new one: | ||
| buildSortedSet(); | ||
|
|
||
| // Allow pruning for compatible leaf comparators. | ||
| leafComparators[0].setHitsThresholdReached(); | ||
| } | ||
|
|
||
| } else { | ||
|
|
@@ -262,9 +286,7 @@ private void collectNewGroup(final int doc) throws IOException { | |
| assert orderedGroups.size() == topNGroups; | ||
|
|
||
| final int lastComparatorSlot = orderedGroups.last().comparatorSlot; | ||
| for (LeafFieldComparator fc : leafComparators) { | ||
| fc.setBottom(lastComparatorSlot); | ||
| } | ||
| setBottomSlot(lastComparatorSlot); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -320,13 +342,16 @@ private void collectExistingGroup(final int doc, final CollectedSearchGroup<T> g | |
| // If we changed the value of the last group, or changed which group was last, then update | ||
| // bottom: | ||
| if (group == newLast || prevLast != newLast) { | ||
| for (LeafFieldComparator fc : leafComparators) { | ||
| fc.setBottom(newLast.comparatorSlot); | ||
| } | ||
| setBottomSlot(newLast.comparatorSlot); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public DocIdSetIterator competitiveIterator() throws IOException { | ||
| return leafComparators[0].competitiveIterator(); | ||
| } | ||
|
|
||
| private void buildSortedSet() throws IOException { | ||
| final Comparator<CollectedSearchGroup<?>> comparator = | ||
| new Comparator<>() { | ||
|
|
@@ -348,13 +373,12 @@ public int compare(CollectedSearchGroup<?> o1, CollectedSearchGroup<?> o2) { | |
| orderedGroups.addAll(groupMap.values()); | ||
| assert orderedGroups.size() > 0; | ||
|
|
||
| for (LeafFieldComparator fc : leafComparators) { | ||
| fc.setBottom(orderedGroups.last().comparatorSlot); | ||
| } | ||
| setBottomSlot(orderedGroups.last().comparatorSlot); | ||
| } | ||
|
|
||
| @Override | ||
| protected void doSetNextReader(LeafReaderContext readerContext) throws IOException { | ||
| minCompetitiveScore = 0f; | ||
| docBase = readerContext.docBase; | ||
| for (int i = 0; i < comparators.length; i++) { | ||
| leafComparators[i] = comparators[i].getLeafComparator(readerContext); | ||
|
|
@@ -372,4 +396,25 @@ public GroupSelector<T> getGroupSelector() { | |
| private boolean isGroupMapFull() { | ||
| return groupMap.size() >= topNGroups; | ||
| } | ||
|
|
||
| private void setBottomSlot(final int bottomSlot) throws IOException { | ||
| for (LeafFieldComparator fc : leafComparators) { | ||
| fc.setBottom(bottomSlot); | ||
| } | ||
|
|
||
| this.bottomSlot = bottomSlot; | ||
| setMinCompetitiveScore(scorer); | ||
| } | ||
|
|
||
| private void setMinCompetitiveScore(final Scorable scorer) throws IOException { | ||
| if (canSetMinScore == false || isGroupMapFull() == false) { | ||
| return; | ||
| } | ||
|
|
||
| final float minScore = (float) comparators[0].value(bottomSlot); | ||
| if (minScore > minCompetitiveScore) { | ||
| scorer.setMinCompetitiveScore(minScore); | ||
| minCompetitiveScore = minScore; | ||
| } | ||
| } | ||
| } | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't pruning always equal to
Pruning.GREATER_THANsincecompIDXEnd = sortFields.length - 1?