use of org.opensearch.search.aggregations.AggregationBuilder in project OpenSearch by opensearch-project.
the class SignificantTermsSignificanceScoreIT method testSubAggregations.
/**
* A simple test that adds a sub-aggregation to a significant terms aggregation,
* to help check that sub-aggregation collection is handled correctly.
*/
public void testSubAggregations() throws Exception {
indexEqualTestData();
QueryBuilder query = QueryBuilders.termsQuery(TEXT_FIELD, "a", "b");
AggregationBuilder subAgg = terms("class").field(CLASS_FIELD);
AggregationBuilder agg = significantTerms("significant_terms").field(TEXT_FIELD).executionHint(randomExecutionHint()).significanceHeuristic(new ChiSquare(true, true)).minDocCount(1).shardSize(1000).size(1000).subAggregation(subAgg);
SearchResponse response = client().prepareSearch("test").setQuery(query).addAggregation(agg).get();
assertSearchResponse(response);
SignificantTerms sigTerms = response.getAggregations().get("significant_terms");
assertThat(sigTerms.getBuckets().size(), equalTo(2));
for (SignificantTerms.Bucket bucket : sigTerms) {
StringTerms terms = bucket.getAggregations().get("class");
assertThat(terms.getBuckets().size(), equalTo(2));
}
}
use of org.opensearch.search.aggregations.AggregationBuilder in project OpenSearch by opensearch-project.
the class FiltersTests method testRewrite.
public void testRewrite() throws IOException {
// test non-keyed filter that doesn't rewrite
AggregationBuilder original = new FiltersAggregationBuilder("my-agg", new MatchAllQueryBuilder());
original.setMetadata(Collections.singletonMap(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20)));
AggregationBuilder rewritten = original.rewrite(new QueryRewriteContext(xContentRegistry(), null, null, () -> 0L));
assertSame(original, rewritten);
// test non-keyed filter that does rewrite
original = new FiltersAggregationBuilder("my-agg", new BoolQueryBuilder());
rewritten = original.rewrite(new QueryRewriteContext(xContentRegistry(), null, null, () -> 0L));
assertNotSame(original, rewritten);
assertThat(rewritten, instanceOf(FiltersAggregationBuilder.class));
assertEquals("my-agg", ((FiltersAggregationBuilder) rewritten).getName());
assertEquals(1, ((FiltersAggregationBuilder) rewritten).filters().size());
assertEquals("0", ((FiltersAggregationBuilder) rewritten).filters().get(0).key());
assertThat(((FiltersAggregationBuilder) rewritten).filters().get(0).filter(), instanceOf(MatchAllQueryBuilder.class));
assertFalse(((FiltersAggregationBuilder) rewritten).isKeyed());
// test keyed filter that doesn't rewrite
original = new FiltersAggregationBuilder("my-agg", new KeyedFilter("my-filter", new MatchAllQueryBuilder()));
rewritten = original.rewrite(new QueryRewriteContext(xContentRegistry(), null, null, () -> 0L));
assertSame(original, rewritten);
// test non-keyed filter that does rewrite
original = new FiltersAggregationBuilder("my-agg", new KeyedFilter("my-filter", new BoolQueryBuilder()));
rewritten = original.rewrite(new QueryRewriteContext(xContentRegistry(), null, null, () -> 0L));
assertNotSame(original, rewritten);
assertThat(rewritten, instanceOf(FiltersAggregationBuilder.class));
assertEquals("my-agg", ((FiltersAggregationBuilder) rewritten).getName());
assertEquals(1, ((FiltersAggregationBuilder) rewritten).filters().size());
assertEquals("my-filter", ((FiltersAggregationBuilder) rewritten).filters().get(0).key());
assertThat(((FiltersAggregationBuilder) rewritten).filters().get(0).filter(), instanceOf(MatchAllQueryBuilder.class));
assertTrue(((FiltersAggregationBuilder) rewritten).isKeyed());
// test sub-agg filter that does rewrite
original = new TermsAggregationBuilder("terms").userValueTypeHint(ValueType.BOOLEAN).subAggregation(new FiltersAggregationBuilder("my-agg", new KeyedFilter("my-filter", new BoolQueryBuilder())));
rewritten = original.rewrite(new QueryRewriteContext(xContentRegistry(), null, null, () -> 0L));
assertNotSame(original, rewritten);
assertNotEquals(original, rewritten);
assertThat(rewritten, instanceOf(TermsAggregationBuilder.class));
assertThat(rewritten.getSubAggregations().size(), equalTo(1));
AggregationBuilder subAgg = rewritten.getSubAggregations().iterator().next();
assertThat(subAgg, instanceOf(FiltersAggregationBuilder.class));
assertNotSame(original.getSubAggregations().iterator().next(), subAgg);
assertEquals("my-agg", subAgg.getName());
assertSame(rewritten, rewritten.rewrite(new QueryRewriteContext(xContentRegistry(), null, null, () -> 0L)));
}
use of org.opensearch.search.aggregations.AggregationBuilder in project OpenSearch by opensearch-project.
the class FiltersTests method testRewritePreservesOtherBucket.
public void testRewritePreservesOtherBucket() throws IOException {
FiltersAggregationBuilder originalFilters = new FiltersAggregationBuilder("my-agg", new BoolQueryBuilder());
originalFilters.otherBucket(randomBoolean());
originalFilters.otherBucketKey(randomAlphaOfLength(10));
AggregationBuilder rewritten = originalFilters.rewrite(new QueryRewriteContext(xContentRegistry(), null, null, () -> 0L));
assertThat(rewritten, instanceOf(FiltersAggregationBuilder.class));
FiltersAggregationBuilder rewrittenFilters = (FiltersAggregationBuilder) rewritten;
assertEquals(originalFilters.otherBucket(), rewrittenFilters.otherBucket());
assertEquals(originalFilters.otherBucketKey(), rewrittenFilters.otherBucketKey());
}
use of org.opensearch.search.aggregations.AggregationBuilder in project OpenSearch by opensearch-project.
the class AutoDateHistogramAggregatorTests method testAsSubAggInManyBuckets.
public void testAsSubAggInManyBuckets() throws IOException {
CheckedBiConsumer<RandomIndexWriter, DateFieldMapper.DateFieldType, IOException> buildIndex = (iw, dft) -> {
long start = dft.parse("2020-01-01T00:00:00Z");
long end = dft.parse("2021-01-01T00:00:00Z");
long anHour = dft.resolution().convert(Instant.ofEpochSecond(TimeUnit.HOURS.toSeconds(1)));
List<List<IndexableField>> docs = new ArrayList<>();
int n = 0;
for (long d = start; d < end; d += anHour) {
docs.add(org.opensearch.common.collect.List.of(new SortedNumericDocValuesField(AGGREGABLE_DATE, d), new SortedNumericDocValuesField("n", n % 100)));
n++;
}
/*
* Intentionally add all documents at once to put them on the
* same shard to make the reduce behavior consistent.
*/
iw.addDocuments(docs);
};
AggregationBuilder builder = new HistogramAggregationBuilder("n").field("n").interval(1).subAggregation(new AutoDateHistogramAggregationBuilder("dh").field(AGGREGABLE_DATE).setNumBuckets(4).subAggregation(new MaxAggregationBuilder("max").field("n")));
asSubAggTestCase(builder, buildIndex, (InternalHistogram histo) -> {
assertThat(histo.getBuckets(), hasSize(100));
for (int n = 0; n < 100; n++) {
InternalHistogram.Bucket b = histo.getBuckets().get(n);
InternalAutoDateHistogram dh = b.getAggregations().get("dh");
assertThat(bucketCountsAsMap(dh), hasEntry(equalTo("2020-01-01T00:00:00.000Z"), either(equalTo(21)).or(equalTo(22))));
assertThat(bucketCountsAsMap(dh), hasEntry(equalTo("2020-04-01T00:00:00.000Z"), either(equalTo(21)).or(equalTo(22))));
assertThat(bucketCountsAsMap(dh), hasEntry(equalTo("2020-07-01T00:00:00.000Z"), either(equalTo(22)).or(equalTo(23))));
assertThat(bucketCountsAsMap(dh), hasEntry(equalTo("2020-10-01T00:00:00.000Z"), either(equalTo(22)).or(equalTo(23))));
Map<String, Double> expectedMax = new TreeMap<>();
expectedMax.put("2020-01-01T00:00:00.000Z", (double) n);
expectedMax.put("2020-04-01T00:00:00.000Z", (double) n);
expectedMax.put("2020-07-01T00:00:00.000Z", (double) n);
expectedMax.put("2020-10-01T00:00:00.000Z", (double) n);
assertThat(maxAsMap(dh), equalTo(expectedMax));
}
});
}
use of org.opensearch.search.aggregations.AggregationBuilder in project OpenSearch by opensearch-project.
the class AutoDateHistogramAggregatorTests method testAsSubAgg.
public void testAsSubAgg() throws IOException {
AggregationBuilder builder = new TermsAggregationBuilder("k1").field("k1").subAggregation(new AutoDateHistogramAggregationBuilder("dh").field(AGGREGABLE_DATE).setNumBuckets(3).subAggregation(new MaxAggregationBuilder("max").field("n")));
asSubAggTestCase(builder, (StringTerms terms) -> {
StringTerms.Bucket a = terms.getBucketByKey("a");
InternalAutoDateHistogram adh = a.getAggregations().get("dh");
Map<String, Integer> expectedDocCount = new TreeMap<>();
expectedDocCount.put("2020-01-01T00:00:00.000Z", 2);
expectedDocCount.put("2021-01-01T00:00:00.000Z", 2);
assertThat(bucketCountsAsMap(adh), equalTo(expectedDocCount));
Map<String, Double> expectedMax = new TreeMap<>();
expectedMax.put("2020-01-01T00:00:00.000Z", 2.0);
expectedMax.put("2021-01-01T00:00:00.000Z", 4.0);
assertThat(maxAsMap(adh), equalTo(expectedMax));
StringTerms.Bucket b = terms.getBucketByKey("b");
InternalAutoDateHistogram bdh = b.getAggregations().get("dh");
expectedDocCount.clear();
expectedDocCount.put("2020-02-01T00:00:00.000Z", 1);
assertThat(bucketCountsAsMap(bdh), equalTo(expectedDocCount));
expectedMax.clear();
expectedMax.put("2020-02-01T00:00:00.000Z", 5.0);
assertThat(maxAsMap(bdh), equalTo(expectedMax));
});
builder = new TermsAggregationBuilder("k2").field("k2").subAggregation(builder);
asSubAggTestCase(builder, (StringTerms terms) -> {
StringTerms.Bucket a = terms.getBucketByKey("a");
StringTerms ak1 = a.getAggregations().get("k1");
StringTerms.Bucket ak1a = ak1.getBucketByKey("a");
InternalAutoDateHistogram ak1adh = ak1a.getAggregations().get("dh");
Map<String, Integer> expectedDocCount = new TreeMap<>();
expectedDocCount.put("2020-01-01T00:00:00.000Z", 2);
expectedDocCount.put("2021-01-01T00:00:00.000Z", 1);
assertThat(bucketCountsAsMap(ak1adh), equalTo(expectedDocCount));
Map<String, Double> expectedMax = new TreeMap<>();
expectedMax.put("2020-01-01T00:00:00.000Z", 2.0);
expectedMax.put("2021-01-01T00:00:00.000Z", 3.0);
assertThat(maxAsMap(ak1adh), equalTo(expectedMax));
StringTerms.Bucket b = terms.getBucketByKey("b");
StringTerms bk1 = b.getAggregations().get("k1");
StringTerms.Bucket bk1a = bk1.getBucketByKey("a");
InternalAutoDateHistogram bk1adh = bk1a.getAggregations().get("dh");
expectedDocCount.clear();
expectedDocCount.put("2021-03-01T00:00:00.000Z", 1);
assertThat(bucketCountsAsMap(bk1adh), equalTo(expectedDocCount));
expectedMax.clear();
expectedMax.put("2021-03-01T00:00:00.000Z", 4.0);
assertThat(maxAsMap(bk1adh), equalTo(expectedMax));
StringTerms.Bucket bk1b = bk1.getBucketByKey("b");
InternalAutoDateHistogram bk1bdh = bk1b.getAggregations().get("dh");
expectedDocCount.clear();
expectedDocCount.put("2020-02-01T00:00:00.000Z", 1);
assertThat(bucketCountsAsMap(bk1bdh), equalTo(expectedDocCount));
expectedMax.clear();
expectedMax.put("2020-02-01T00:00:00.000Z", 5.0);
assertThat(maxAsMap(bk1bdh), equalTo(expectedMax));
});
}
Aggregations