Skip to content

Fix max score tracking with field collapsing #27122

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 1 commit into from
Oct 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,10 @@ static TopDocsCollectorContext createTopDocsCollectorContext(SearchContext searc
return new ScrollingTopDocsCollectorContext(searchContext.scrollContext(),
searchContext.sort(), numDocs, searchContext.trackScores(), searchContext.numberOfShards());
} else if (searchContext.collapse() != null) {
boolean trackScores = searchContext.sort() == null ? true : searchContext.trackScores();
int numDocs = Math.min(searchContext.from() + searchContext.size(), totalNumDocs);
return new CollapsingTopDocsCollectorContext(searchContext.collapse(),
searchContext.sort(), numDocs, searchContext.trackScores());
searchContext.sort(), numDocs, trackScores);
} else {
int numDocs = Math.min(searchContext.from() + searchContext.size(), totalNumDocs);
final boolean rescore = searchContext.rescore().isEmpty() == false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
import java.util.List;
import java.util.Set;

import static org.hamcrest.core.IsEqual.equalTo;

public class CollapsingTopDocsCollectorTests extends ESTestCase {
private static class SegmentSearcher extends IndexSearcher {
private final List<LeafReaderContext> ctx;
Expand Down Expand Up @@ -82,12 +84,15 @@ interface CollapsingDocValuesProducer<T extends Comparable> {
}

<T extends Comparable> void assertSearchCollapse(CollapsingDocValuesProducer<T> dvProducers, boolean numeric) throws IOException {
assertSearchCollapse(dvProducers, numeric, true);
assertSearchCollapse(dvProducers, numeric, false);
assertSearchCollapse(dvProducers, numeric, true, true);
assertSearchCollapse(dvProducers, numeric, true, false);
assertSearchCollapse(dvProducers, numeric, false, true);
assertSearchCollapse(dvProducers, numeric, false, false);
}

private <T extends Comparable> void assertSearchCollapse(CollapsingDocValuesProducer<T> dvProducers,
boolean numeric, boolean multivalued) throws IOException {
boolean numeric, boolean multivalued,
boolean trackMaxScores) throws IOException {
final int numDocs = randomIntBetween(1000, 2000);
int maxGroup = randomIntBetween(2, 500);
final Directory dir = newDirectory();
Expand Down Expand Up @@ -118,14 +123,14 @@ private <T extends Comparable> void assertSearchCollapse(CollapsingDocValuesProd
final CollapsingTopDocsCollector collapsingCollector;
if (numeric) {
collapsingCollector =
CollapsingTopDocsCollector.createNumeric(collapseField.getField(), sort, expectedNumGroups, false);
CollapsingTopDocsCollector.createNumeric(collapseField.getField(), sort, expectedNumGroups, trackMaxScores);
} else {
collapsingCollector =
CollapsingTopDocsCollector.createKeyword(collapseField.getField(), sort, expectedNumGroups, false);
CollapsingTopDocsCollector.createKeyword(collapseField.getField(), sort, expectedNumGroups, trackMaxScores);
}

TopFieldCollector topFieldCollector =
TopFieldCollector.create(sort, totalHits, true, false, false);
TopFieldCollector.create(sort, totalHits, true, trackMaxScores, trackMaxScores);

searcher.search(new MatchAllDocsQuery(), collapsingCollector);
searcher.search(new MatchAllDocsQuery(), topFieldCollector);
Expand All @@ -136,6 +141,11 @@ private <T extends Comparable> void assertSearchCollapse(CollapsingDocValuesProd
assertEquals(totalHits, collapseTopFieldDocs.totalHits);
assertEquals(totalHits, topDocs.scoreDocs.length);
assertEquals(totalHits, topDocs.totalHits);
if (trackMaxScores) {
assertThat(collapseTopFieldDocs.getMaxScore(), equalTo(topDocs.getMaxScore()));
} else {
assertThat(collapseTopFieldDocs.getMaxScore(), equalTo(Float.NaN));
}

Set<Object> seen = new HashSet<>();
// collapse field is the last sort
Expand Down Expand Up @@ -186,14 +196,14 @@ private <T extends Comparable> void assertSearchCollapse(CollapsingDocValuesProd
}

final CollapseTopFieldDocs[] shardHits = new CollapseTopFieldDocs[subSearchers.length];
final Weight weight = searcher.createNormalizedWeight(new MatchAllDocsQuery(), false);
final Weight weight = searcher.createNormalizedWeight(new MatchAllDocsQuery(), true);
for (int shardIDX = 0; shardIDX < subSearchers.length; shardIDX++) {
final SegmentSearcher subSearcher = subSearchers[shardIDX];
final CollapsingTopDocsCollector c;
if (numeric) {
c = CollapsingTopDocsCollector.createNumeric(collapseField.getField(), sort, expectedNumGroups, false);
c = CollapsingTopDocsCollector.createNumeric(collapseField.getField(), sort, expectedNumGroups, trackMaxScores);
} else {
c = CollapsingTopDocsCollector.createKeyword(collapseField.getField(), sort, expectedNumGroups, false);
c = CollapsingTopDocsCollector.createKeyword(collapseField.getField(), sort, expectedNumGroups, trackMaxScores);
}
subSearcher.search(weight, c);
shardHits[shardIDX] = c.getTopDocs();
Expand Down