|
11 | 11 |
|
12 | 12 | import org.apache.lucene.document.LongPoint; |
13 | 13 | import org.apache.lucene.document.SortedNumericDocValuesField; |
| 14 | +import org.apache.lucene.index.Term; |
| 15 | +import org.apache.lucene.search.BooleanClause; |
| 16 | +import org.apache.lucene.search.BooleanQuery; |
| 17 | +import org.apache.lucene.search.TermQuery; |
14 | 18 | import org.apache.lucene.tests.index.RandomIndexWriter; |
15 | 19 | import org.apache.lucene.util.BytesRef; |
16 | 20 | import org.elasticsearch.common.Strings; |
17 | 21 | import org.elasticsearch.index.mapper.KeywordFieldMapper; |
18 | 22 | import org.elasticsearch.index.query.QueryBuilders; |
| 23 | +import org.elasticsearch.search.SearchHit; |
19 | 24 | import org.elasticsearch.search.aggregations.AggregationBuilders; |
20 | 25 | import org.elasticsearch.search.aggregations.AggregatorTestCase; |
21 | 26 | import org.elasticsearch.search.aggregations.bucket.filter.Filter; |
22 | 27 | import org.elasticsearch.search.aggregations.metrics.Avg; |
23 | 28 | import org.elasticsearch.search.aggregations.metrics.Max; |
24 | 29 | import org.elasticsearch.search.aggregations.metrics.Min; |
| 30 | +import org.elasticsearch.search.aggregations.metrics.TopHits; |
25 | 31 | import org.hamcrest.Description; |
26 | 32 | import org.hamcrest.Matcher; |
27 | 33 | import org.hamcrest.TypeSafeMatcher; |
28 | 34 |
|
29 | 35 | import java.io.IOException; |
| 36 | +import java.util.Arrays; |
30 | 37 | import java.util.List; |
31 | 38 | import java.util.concurrent.atomic.AtomicInteger; |
32 | 39 | import java.util.stream.DoubleStream; |
|
37 | 44 | import static org.hamcrest.Matchers.equalTo; |
38 | 45 | import static org.hamcrest.Matchers.greaterThan; |
39 | 46 | import static org.hamcrest.Matchers.greaterThanOrEqualTo; |
| 47 | +import static org.hamcrest.Matchers.hasSize; |
| 48 | +import static org.hamcrest.Matchers.lessThan; |
40 | 49 | import static org.hamcrest.Matchers.lessThanOrEqualTo; |
41 | 50 | import static org.hamcrest.Matchers.not; |
42 | 51 | import static org.hamcrest.Matchers.notANumber; |
@@ -76,6 +85,35 @@ public void testAggregationSampling() throws IOException { |
76 | 85 | assertThat(avgAvg, closeTo(1.5, 0.5)); |
77 | 86 | } |
78 | 87 |
|
| 88 | + public void testAggregationSampling_withScores() throws IOException { |
| 89 | + long[] counts = new long[5]; |
| 90 | + AtomicInteger integer = new AtomicInteger(); |
| 91 | + do { |
| 92 | + testCase(RandomSamplerAggregatorTests::writeTestDocs, (InternalRandomSampler result) -> { |
| 93 | + counts[integer.get()] = result.getDocCount(); |
| 94 | + if (result.getDocCount() > 0) { |
| 95 | + TopHits agg = result.getAggregations().get("top"); |
| 96 | + List<SearchHit> hits = Arrays.asList(agg.getHits().getHits()); |
| 97 | + assertThat(Strings.toString(result), hits, hasSize(1)); |
| 98 | + assertThat(Strings.toString(result), hits.get(0).getScore(), allOf(greaterThan(0.0f), lessThan(1.0f))); |
| 99 | + } |
| 100 | + }, |
| 101 | + new AggTestConfig( |
| 102 | + new RandomSamplerAggregationBuilder("my_agg").subAggregation(AggregationBuilders.topHits("top").size(1)) |
| 103 | + .setProbability(0.25), |
| 104 | + longField(NUMERIC_FIELD_NAME) |
| 105 | + ).withQuery( |
| 106 | + new BooleanQuery.Builder().add( |
| 107 | + new TermQuery(new Term(KEYWORD_FIELD_NAME, KEYWORD_FIELD_VALUE)), |
| 108 | + BooleanClause.Occur.SHOULD |
| 109 | + ).build() |
| 110 | + ) |
| 111 | + ); |
| 112 | + } while (integer.incrementAndGet() < 5); |
| 113 | + long avgCount = LongStream.of(counts).sum() / integer.get(); |
| 114 | + assertThat(avgCount, allOf(greaterThanOrEqualTo(20L), lessThanOrEqualTo(70L))); |
| 115 | + } |
| 116 | + |
79 | 117 | public void testAggregationSamplingNestedAggsScaled() throws IOException { |
80 | 118 | // in case 0 docs get sampled, which can rarely happen |
81 | 119 | // in case the test index has many segments. |
|
0 commit comments