use of org.opensearch.search.aggregations.metrics.MaxAggregationBuilder 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.metrics.MaxAggregationBuilder 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));
});
}
use of org.opensearch.search.aggregations.metrics.MaxAggregationBuilder in project OpenSearch by opensearch-project.
the class AutoDateHistogramAggregatorTests method testAsSubAggWithIncreasedRounding.
public void testAsSubAggWithIncreasedRounding() 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 useC = dft.parse("2020-07-01T00:00Z");
long anHour = dft.resolution().convert(Instant.ofEpochSecond(TimeUnit.HOURS.toSeconds(1)));
List<List<IndexableField>> docs = new ArrayList<>();
BytesRef aBytes = new BytesRef("a");
BytesRef bBytes = new BytesRef("b");
BytesRef cBytes = new BytesRef("c");
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 SortedSetDocValuesField("k1", aBytes), new SortedSetDocValuesField("k1", d < useC ? bBytes : cBytes), new SortedNumericDocValuesField("n", 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 TermsAggregationBuilder("k1").field("k1").subAggregation(new AutoDateHistogramAggregationBuilder("dh").field(AGGREGABLE_DATE).setNumBuckets(4).subAggregation(new MaxAggregationBuilder("max").field("n")));
asSubAggTestCase(builder, buildIndex, (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", 2184);
expectedDocCount.put("2020-04-01T00:00:00.000Z", 2184);
expectedDocCount.put("2020-07-01T00:00:00.000Z", 2208);
expectedDocCount.put("2020-10-01T00:00:00.000Z", 2208);
assertThat(bucketCountsAsMap(adh), equalTo(expectedDocCount));
Map<String, Double> expectedMax = new TreeMap<>();
expectedMax.put("2020-01-01T00:00:00.000Z", 2183.0);
expectedMax.put("2020-04-01T00:00:00.000Z", 4367.0);
expectedMax.put("2020-07-01T00:00:00.000Z", 6575.0);
expectedMax.put("2020-10-01T00:00:00.000Z", 8783.0);
assertThat(maxAsMap(adh), equalTo(expectedMax));
StringTerms.Bucket b = terms.getBucketByKey("b");
InternalAutoDateHistogram bdh = b.getAggregations().get("dh");
expectedDocCount.clear();
expectedDocCount.put("2020-01-01T00:00:00.000Z", 2184);
expectedDocCount.put("2020-04-01T00:00:00.000Z", 2184);
assertThat(bucketCountsAsMap(bdh), equalTo(expectedDocCount));
expectedMax.clear();
expectedMax.put("2020-01-01T00:00:00.000Z", 2183.0);
expectedMax.put("2020-04-01T00:00:00.000Z", 4367.0);
assertThat(maxAsMap(bdh), equalTo(expectedMax));
StringTerms.Bucket c = terms.getBucketByKey("c");
InternalAutoDateHistogram cdh = c.getAggregations().get("dh");
expectedDocCount.clear();
expectedDocCount.put("2020-07-01T00:00:00.000Z", 2208);
expectedDocCount.put("2020-10-01T00:00:00.000Z", 2208);
assertThat(bucketCountsAsMap(cdh), equalTo(expectedDocCount));
expectedMax.clear();
expectedMax.put("2020-07-01T00:00:00.000Z", 6575.0);
expectedMax.put("2020-10-01T00:00:00.000Z", 8783.0);
assertThat(maxAsMap(cdh), equalTo(expectedMax));
});
}
use of org.opensearch.search.aggregations.metrics.MaxAggregationBuilder in project OpenSearch by opensearch-project.
the class CompositeAggregatorTests method testWithTermsSubAggExecutionMode.
public void testWithTermsSubAggExecutionMode() throws Exception {
// test with no bucket
for (Aggregator.SubAggCollectionMode mode : Aggregator.SubAggCollectionMode.values()) {
testSearchCase(Arrays.asList(new MatchAllDocsQuery(), new DocValuesFieldExistsQuery("keyword")), Collections.singletonList(createDocument()), () -> {
TermsValuesSourceBuilder terms = new TermsValuesSourceBuilder("keyword").field("keyword");
return new CompositeAggregationBuilder("name", Collections.singletonList(terms)).subAggregation(new TermsAggregationBuilder("terms").userValueTypeHint(ValueType.STRING).field("terms").collectMode(mode).subAggregation(new MaxAggregationBuilder("max").field("long")));
}, (result) -> {
assertEquals(0, result.getBuckets().size());
});
}
final List<Map<String, List<Object>>> dataset = new ArrayList<>();
dataset.addAll(Arrays.asList(createDocument("keyword", "a", "terms", "a", "long", 50L), createDocument("keyword", "c", "terms", "d", "long", 78L), createDocument("keyword", "a", "terms", "w", "long", 78L), createDocument("keyword", "d", "terms", "y", "long", 76L), createDocument("keyword", "c", "terms", "y", "long", 70L)));
for (Aggregator.SubAggCollectionMode mode : Aggregator.SubAggCollectionMode.values()) {
testSearchCase(Arrays.asList(new MatchAllDocsQuery(), new DocValuesFieldExistsQuery("keyword")), dataset, () -> {
TermsValuesSourceBuilder terms = new TermsValuesSourceBuilder("keyword").field("keyword");
return new CompositeAggregationBuilder("name", Collections.singletonList(terms)).subAggregation(new TermsAggregationBuilder("terms").userValueTypeHint(ValueType.STRING).field("terms").collectMode(mode).subAggregation(new MaxAggregationBuilder("max").field("long")));
}, (result) -> {
assertEquals(3, result.getBuckets().size());
assertEquals("{keyword=a}", result.getBuckets().get(0).getKeyAsString());
assertEquals(2L, result.getBuckets().get(0).getDocCount());
StringTerms subTerms = result.getBuckets().get(0).getAggregations().get("terms");
assertEquals(2, subTerms.getBuckets().size());
assertEquals("a", subTerms.getBuckets().get(0).getKeyAsString());
assertEquals("w", subTerms.getBuckets().get(1).getKeyAsString());
InternalMax max = subTerms.getBuckets().get(0).getAggregations().get("max");
assertEquals(50L, (long) max.getValue());
max = subTerms.getBuckets().get(1).getAggregations().get("max");
assertEquals(78L, (long) max.getValue());
assertEquals("{keyword=c}", result.getBuckets().get(1).getKeyAsString());
assertEquals(2L, result.getBuckets().get(1).getDocCount());
subTerms = result.getBuckets().get(1).getAggregations().get("terms");
assertEquals(2, subTerms.getBuckets().size());
assertEquals("d", subTerms.getBuckets().get(0).getKeyAsString());
assertEquals("y", subTerms.getBuckets().get(1).getKeyAsString());
max = subTerms.getBuckets().get(0).getAggregations().get("max");
assertEquals(78L, (long) max.getValue());
max = subTerms.getBuckets().get(1).getAggregations().get("max");
assertEquals(70L, (long) max.getValue());
assertEquals("{keyword=d}", result.getBuckets().get(2).getKeyAsString());
assertEquals(1L, result.getBuckets().get(2).getDocCount());
subTerms = result.getBuckets().get(2).getAggregations().get("terms");
assertEquals(1, subTerms.getBuckets().size());
assertEquals("y", subTerms.getBuckets().get(0).getKeyAsString());
max = subTerms.getBuckets().get(0).getAggregations().get("max");
assertEquals(76L, (long) max.getValue());
});
}
}
use of org.opensearch.search.aggregations.metrics.MaxAggregationBuilder in project OpenSearch by opensearch-project.
the class NestedAggregatorTests method testNestedOrdering.
public void testNestedOrdering() throws IOException {
try (Directory directory = newDirectory()) {
try (RandomIndexWriter iw = new RandomIndexWriter(random(), directory)) {
iw.addDocuments(generateBook("1", new String[] { "a" }, new int[] { 12, 13, 14 }));
iw.addDocuments(generateBook("2", new String[] { "b" }, new int[] { 5, 50 }));
iw.addDocuments(generateBook("3", new String[] { "c" }, new int[] { 39, 19 }));
iw.addDocuments(generateBook("4", new String[] { "d" }, new int[] { 2, 1, 3 }));
iw.addDocuments(generateBook("5", new String[] { "a" }, new int[] { 70, 10 }));
iw.addDocuments(generateBook("6", new String[] { "e" }, new int[] { 23, 21 }));
iw.addDocuments(generateBook("7", new String[] { "e", "a" }, new int[] { 8, 8 }));
iw.addDocuments(generateBook("8", new String[] { "f" }, new int[] { 12, 14 }));
iw.addDocuments(generateBook("9", new String[] { "g", "c", "e" }, new int[] { 18, 8 }));
}
try (IndexReader indexReader = wrapInMockESDirectoryReader(DirectoryReader.open(directory))) {
MappedFieldType fieldType1 = new NumberFieldMapper.NumberFieldType("num_pages", NumberFieldMapper.NumberType.LONG);
MappedFieldType fieldType2 = new KeywordFieldMapper.KeywordFieldType("author");
TermsAggregationBuilder termsBuilder = new TermsAggregationBuilder("authors").userValueTypeHint(ValueType.STRING).field("author").order(BucketOrder.aggregation("chapters>num_pages.value", true));
NestedAggregationBuilder nestedBuilder = new NestedAggregationBuilder("chapters", "nested_chapters");
MaxAggregationBuilder maxAgg = new MaxAggregationBuilder("num_pages").field("num_pages");
nestedBuilder.subAggregation(maxAgg);
termsBuilder.subAggregation(nestedBuilder);
Terms terms = searchAndReduce(newSearcher(indexReader, false, true), new MatchAllDocsQuery(), termsBuilder, fieldType1, fieldType2);
assertEquals(7, terms.getBuckets().size());
assertEquals("authors", terms.getName());
Terms.Bucket bucket = terms.getBuckets().get(0);
assertEquals("d", bucket.getKeyAsString());
Max numPages = ((Nested) bucket.getAggregations().get("chapters")).getAggregations().get("num_pages");
assertEquals(3, (int) numPages.getValue());
bucket = terms.getBuckets().get(1);
assertEquals("f", bucket.getKeyAsString());
numPages = ((Nested) bucket.getAggregations().get("chapters")).getAggregations().get("num_pages");
assertEquals(14, (int) numPages.getValue());
bucket = terms.getBuckets().get(2);
assertEquals("g", bucket.getKeyAsString());
numPages = ((Nested) bucket.getAggregations().get("chapters")).getAggregations().get("num_pages");
assertEquals(18, (int) numPages.getValue());
bucket = terms.getBuckets().get(3);
assertEquals("e", bucket.getKeyAsString());
numPages = ((Nested) bucket.getAggregations().get("chapters")).getAggregations().get("num_pages");
assertEquals(23, (int) numPages.getValue());
bucket = terms.getBuckets().get(4);
assertEquals("c", bucket.getKeyAsString());
numPages = ((Nested) bucket.getAggregations().get("chapters")).getAggregations().get("num_pages");
assertEquals(39, (int) numPages.getValue());
bucket = terms.getBuckets().get(5);
assertEquals("b", bucket.getKeyAsString());
numPages = ((Nested) bucket.getAggregations().get("chapters")).getAggregations().get("num_pages");
assertEquals(50, (int) numPages.getValue());
bucket = terms.getBuckets().get(6);
assertEquals("a", bucket.getKeyAsString());
numPages = ((Nested) bucket.getAggregations().get("chapters")).getAggregations().get("num_pages");
assertEquals(70, (int) numPages.getValue());
// reverse order:
termsBuilder = new TermsAggregationBuilder("authors").userValueTypeHint(ValueType.STRING).field("author").order(BucketOrder.aggregation("chapters>num_pages.value", false));
nestedBuilder = new NestedAggregationBuilder("chapters", "nested_chapters");
maxAgg = new MaxAggregationBuilder("num_pages").field("num_pages");
nestedBuilder.subAggregation(maxAgg);
termsBuilder.subAggregation(nestedBuilder);
terms = searchAndReduce(newSearcher(indexReader, false, true), new MatchAllDocsQuery(), termsBuilder, fieldType1, fieldType2);
assertEquals(7, terms.getBuckets().size());
assertEquals("authors", terms.getName());
bucket = terms.getBuckets().get(0);
assertEquals("a", bucket.getKeyAsString());
numPages = ((Nested) bucket.getAggregations().get("chapters")).getAggregations().get("num_pages");
assertEquals(70, (int) numPages.getValue());
bucket = terms.getBuckets().get(1);
assertEquals("b", bucket.getKeyAsString());
numPages = ((Nested) bucket.getAggregations().get("chapters")).getAggregations().get("num_pages");
assertEquals(50, (int) numPages.getValue());
bucket = terms.getBuckets().get(2);
assertEquals("c", bucket.getKeyAsString());
numPages = ((Nested) bucket.getAggregations().get("chapters")).getAggregations().get("num_pages");
assertEquals(39, (int) numPages.getValue());
bucket = terms.getBuckets().get(3);
assertEquals("e", bucket.getKeyAsString());
numPages = ((Nested) bucket.getAggregations().get("chapters")).getAggregations().get("num_pages");
assertEquals(23, (int) numPages.getValue());
bucket = terms.getBuckets().get(4);
assertEquals("g", bucket.getKeyAsString());
numPages = ((Nested) bucket.getAggregations().get("chapters")).getAggregations().get("num_pages");
assertEquals(18, (int) numPages.getValue());
bucket = terms.getBuckets().get(5);
assertEquals("f", bucket.getKeyAsString());
numPages = ((Nested) bucket.getAggregations().get("chapters")).getAggregations().get("num_pages");
assertEquals(14, (int) numPages.getValue());
bucket = terms.getBuckets().get(6);
assertEquals("d", bucket.getKeyAsString());
numPages = ((Nested) bucket.getAggregations().get("chapters")).getAggregations().get("num_pages");
assertEquals(3, (int) numPages.getValue());
}
}
}
Aggregations