diff --git a/rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/search.aggregation/30_sig_terms.yml b/rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/search.aggregation/30_sig_terms.yml index d7759afe4a907..05fdb6a80b856 100644 --- a/rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/search.aggregation/30_sig_terms.yml +++ b/rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/search.aggregation/30_sig_terms.yml @@ -73,6 +73,87 @@ - match: {aggregations.class.buckets.1.sig_terms.buckets.0.key: "good"} --- +"Test background filter count ": + - skip: + version: " - 7.14.99" + reason: bugfix introduced in 7.15.0 + + - do: + indices.create: + index: goodbad + body: + settings: + number_of_shards: "1" + mappings: + properties: + text: + type: text + fielddata: true + class: + type: keyword + - do: + indices.create: + index: goodbad-2 + body: + settings: + number_of_shards: "1" + mappings: + properties: + text: + type: text + fielddata: true + class: + type: keyword + + - do: + index: + index: goodbad-2 + id: 1 + body: { class: "bad" } + - do: + index: + index: goodbad-2 + id: 2 + body: { class: "bad" } + + - do: + index: + index: goodbad + id: 1 + body: { text: "good", class: "good" } + - do: + index: + index: goodbad + id: 2 + body: { text: "good", class: "good" } + - do: + index: + index: goodbad + id: 3 + body: { text: "bad", class: "bad" } + - do: + index: + index: goodbad + id: 4 + body: { text: "bad", class: "bad" } + + - do: + indices.refresh: + index: [goodbad, goodbad-2] + + - do: + search: + rest_total_hits_as_int: true + index: goodbad* + + - match: {hits.total: 6} + + - do: + search: + index: goodbad* + body: {"aggs": {"sig_terms": {"significant_terms": {"field": "text", "background_filter": {"bool": {"filter": [{"term": {"class": "good" }}]}}}}}} + - match: { aggregations.sig_terms.bg_count: 2 } +--- "IP test": - do: indices.create: diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/AbstractStringTermsAggregator.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/AbstractStringTermsAggregator.java index aadec9b36e330..263f6752c31e1 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/AbstractStringTermsAggregator.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/AbstractStringTermsAggregator.java @@ -36,9 +36,12 @@ protected StringTerms buildEmptyTermsAggregation() { metadata(), format, bucketCountThresholds.getShardSize(), showTermDocCountError, 0, emptyList(), 0L); } - protected SignificantStringTerms buildEmptySignificantTermsAggregation(long subsetSize, SignificanceHeuristic significanceHeuristic) { + protected SignificantStringTerms buildEmptySignificantTermsAggregation( + long subsetSize, + long supersetSize, + SignificanceHeuristic significanceHeuristic + ) { // We need to account for the significance of a miss in our global stats - provide corpus size as context - int supersetSize = searcher().getIndexReader().numDocs(); return new SignificantStringTerms(name, bucketCountThresholds.getRequiredSize(), bucketCountThresholds.getMinDocCount(), metadata(), format, subsetSize, supersetSize, significanceHeuristic, emptyList()); } diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/GlobalOrdinalsStringTermsAggregator.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/GlobalOrdinalsStringTermsAggregator.java index e4d44114166ca..7c9947c0d780a 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/GlobalOrdinalsStringTermsAggregator.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/GlobalOrdinalsStringTermsAggregator.java @@ -894,12 +894,12 @@ SignificantStringTerms buildResult(long owningBucketOrd, long otherDocCount, Sig @Override SignificantStringTerms buildEmptyResult() { - return buildEmptySignificantTermsAggregation(0, significanceHeuristic); + return buildEmptySignificantTermsAggregation(0, supersetSize, significanceHeuristic); } @Override SignificantStringTerms buildNoValuesResult(long owningBucketOrdinal) { - return buildEmptySignificantTermsAggregation(subsetSizes.get(owningBucketOrdinal), significanceHeuristic); + return buildEmptySignificantTermsAggregation(subsetSizes.get(owningBucketOrdinal), supersetSize, significanceHeuristic); } @Override diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/MapStringTermsAggregator.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/MapStringTermsAggregator.java index a7911bcdc3590..e61cad1458915 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/MapStringTermsAggregator.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/MapStringTermsAggregator.java @@ -568,7 +568,7 @@ SignificantStringTerms buildResult(long owningBucketOrd, long otherDocCount, Sig @Override SignificantStringTerms buildEmptyResult() { - return buildEmptySignificantTermsAggregation(0, significanceHeuristic); + return buildEmptySignificantTermsAggregation(0, supersetSize, significanceHeuristic); } @Override diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/NumericTermsAggregator.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/NumericTermsAggregator.java index 38338bc8e5668..51c73c12b683e 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/NumericTermsAggregator.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/NumericTermsAggregator.java @@ -573,8 +573,6 @@ SignificantLongTerms buildResult(long owningBucketOrd, long otherDocCoun, Signif @Override SignificantLongTerms buildEmptyResult() { - // We need to account for the significance of a miss in our global stats - provide corpus size as context - int supersetSize = searcher().getIndexReader().numDocs(); return new SignificantLongTerms( name, bucketCountThresholds.getRequiredSize(),