Skip to content

Commit b09af6a

Browse files
committed
Unit test with scores
1 parent 9891403 commit b09af6a

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

server/src/test/java/org/elasticsearch/search/aggregations/bucket/sampler/random/RandomSamplerAggregatorTests.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,29 @@
1111

1212
import org.apache.lucene.document.LongPoint;
1313
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;
1418
import org.apache.lucene.tests.index.RandomIndexWriter;
1519
import org.apache.lucene.util.BytesRef;
1620
import org.elasticsearch.common.Strings;
1721
import org.elasticsearch.index.mapper.KeywordFieldMapper;
1822
import org.elasticsearch.index.query.QueryBuilders;
23+
import org.elasticsearch.search.SearchHit;
1924
import org.elasticsearch.search.aggregations.AggregationBuilders;
2025
import org.elasticsearch.search.aggregations.AggregatorTestCase;
2126
import org.elasticsearch.search.aggregations.bucket.filter.Filter;
2227
import org.elasticsearch.search.aggregations.metrics.Avg;
2328
import org.elasticsearch.search.aggregations.metrics.Max;
2429
import org.elasticsearch.search.aggregations.metrics.Min;
30+
import org.elasticsearch.search.aggregations.metrics.TopHits;
2531
import org.hamcrest.Description;
2632
import org.hamcrest.Matcher;
2733
import org.hamcrest.TypeSafeMatcher;
2834

2935
import java.io.IOException;
36+
import java.util.Arrays;
3037
import java.util.List;
3138
import java.util.concurrent.atomic.AtomicInteger;
3239
import java.util.stream.DoubleStream;
@@ -37,6 +44,8 @@
3744
import static org.hamcrest.Matchers.equalTo;
3845
import static org.hamcrest.Matchers.greaterThan;
3946
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
47+
import static org.hamcrest.Matchers.hasSize;
48+
import static org.hamcrest.Matchers.lessThan;
4049
import static org.hamcrest.Matchers.lessThanOrEqualTo;
4150
import static org.hamcrest.Matchers.not;
4251
import static org.hamcrest.Matchers.notANumber;
@@ -76,6 +85,35 @@ public void testAggregationSampling() throws IOException {
7685
assertThat(avgAvg, closeTo(1.5, 0.5));
7786
}
7887

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+
79117
public void testAggregationSamplingNestedAggsScaled() throws IOException {
80118
// in case 0 docs get sampled, which can rarely happen
81119
// in case the test index has many segments.

0 commit comments

Comments
 (0)