Skip to content

Commit

Permalink
Terms aggs that run as filters ignore shard_min_doc_count (#69323)
Browse files Browse the repository at this point in the history
This change handles `shard_min_doc_count` for terms aggregation that run as filters.

Closes #69312
  • Loading branch information
jimczi authored Feb 22, 2021
1 parent 914a85a commit 27c31fa
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ protected boolean lessThan(OrdBucket a, OrdBucket b) {
};
OrdBucket spare = null;
for (InternalFilters.InternalBucket b : filters.getBuckets()) {
if (b.getDocCount() < bucketCountThresholds.getShardMinDocCount()) {
continue;
}
if (spare == null) {
spare = new OrdBucket(showTermDocCountError, format);
} else {
Expand Down Expand Up @@ -194,6 +197,9 @@ protected boolean lessThan(OrdBucket a, OrdBucket b) {
} else {
buckets = new ArrayList<>(filters.getBuckets().size());
for (InternalFilters.InternalBucket b : filters.getBuckets()) {
if (b.getDocCount() < bucketCountThresholds.getShardMinDocCount()) {
continue;
}
buckets.add(buildBucket(b));
}
Collections.sort(buckets, reduceOrder.comparator());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
import java.io.IOException;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
Expand Down Expand Up @@ -258,6 +259,33 @@ public void testSimple() throws Exception {
}, fieldType);
}

public void testStringShardMinDocCount() throws IOException {
MappedFieldType fieldType = new KeywordFieldMapper.KeywordFieldType("string", true, true, null);
for (TermsAggregatorFactory.ExecutionMode executionMode : TermsAggregatorFactory.ExecutionMode.values()) {
TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("_name")
.field("string")
.executionHint(executionMode.toString())
.size(2)
.minDocCount(2)
.shardMinDocCount(2)
.order(BucketOrder.key(true));
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
// force single shard/segment
iw.addDocuments(Arrays.asList(
doc(fieldType, "a", "b"),
doc(fieldType, "", "c", "d"),
doc(fieldType, "b", "d"),
doc(fieldType, "b")));
}, (InternalTerms<?, ?> result) -> {
assertEquals(2, result.getBuckets().size());
assertEquals("b", result.getBuckets().get(0).getKeyAsString());
assertEquals(3L, result.getBuckets().get(0).getDocCount());
assertEquals("d", result.getBuckets().get(1).getKeyAsString());
assertEquals(2L, result.getBuckets().get(1).getDocCount());
}, fieldType);
}
}

public void testManyUniqueTerms() throws Exception {
MappedFieldType fieldType = new KeywordFieldMapper.KeywordFieldType("string", randomBoolean(), true, null);
TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("_name")
Expand Down

0 comments on commit 27c31fa

Please sign in to comment.