Skip to content

Commit

Permalink
[7.x] Fix bug with significant terms background count (#76730) (#76744)
Browse files Browse the repository at this point in the history
* Fix bug with significant terms background count (#76730)

When building empty responses for shards that don't have the term field in question,
significant terms ignored the background filter.

This commit fixes this bug by respecting the background filter count, even
when building empty results.

closes #76729

* fixing bwc yaml test version
  • Loading branch information
benwtrent authored Aug 20, 2021
1 parent 03e5c55 commit 433eab7
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ SignificantStringTerms buildResult(long owningBucketOrd, long otherDocCount, Sig

@Override
SignificantStringTerms buildEmptyResult() {
return buildEmptySignificantTermsAggregation(0, significanceHeuristic);
return buildEmptySignificantTermsAggregation(0, supersetSize, significanceHeuristic);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 433eab7

Please sign in to comment.