diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/search.aggregation/280_rare_terms.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/search.aggregation/280_rare_terms.yml index a82caddd9cfd4..04d76a987809d 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/search.aggregation/280_rare_terms.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/search.aggregation/280_rare_terms.yml @@ -313,4 +313,50 @@ setup: - match: { hits.total.value: 1 } - length: { aggregations.long_terms.buckets: 0 } +--- +"sub aggs": + - skip: + version: " - 7.99.99" + reason: Sub aggs fixed in 8.0 (to be backported to 7.6.1) + + - do: + index: + refresh: true + index: test_1 + id: 1 + body: { "str" : "abc", "number": 1 } + - do: + index: + refresh: true + index: test_1 + id: 2 + body: { "str": "abc", "number": 2 } + + - do: + index: + refresh: true + index: test_1 + id: 3 + body: { "str": "bcd", "number": 3 } + + - do: + search: + body: + size: 0 + aggs: + str_terms: + rare_terms: + field: str + max_doc_count: 1 + aggs: + max_n: + max: + field: number + + - match: { hits.total.value: 3 } + - length: { aggregations.str_terms.buckets: 1 } + - match: { aggregations.str_terms.buckets.0.key: "bcd" } + - is_false: aggregations.str_terms.buckets.0.key_as_string + - match: { aggregations.str_terms.buckets.0.doc_count: 1 } + - match: { aggregations.str_terms.buckets.0.max_n.value: 3.0 } diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/AbstractRareTermsAggregator.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/AbstractRareTermsAggregator.java index 2bbe3c01988df..bacc2ef217004 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/AbstractRareTermsAggregator.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/AbstractRareTermsAggregator.java @@ -50,7 +50,6 @@ public abstract class AbstractRareTermsAggregator dataset, try (Directory directory = newDirectory()) { try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) { Document document = new Document(); - for (Long value : dataset) { + List shuffledDataset = new ArrayList<>(dataset); + Collections.shuffle(shuffledDataset, random()); + for (Long value : shuffledDataset) { if (frequently()) { indexWriter.commit(); }