use of org.opensearch.search.aggregations.metrics.MaxAggregationBuilder in project OpenSearch by opensearch-project.
the class NestedAggregatorTests method testNestedWithPipeline.
/**
* This tests to make sure pipeline aggs embedded under a SingleBucket agg (like nested)
* are properly reduced
*/
public void testNestedWithPipeline() throws IOException {
int numRootDocs = randomIntBetween(1, 20);
int expectedNestedDocs = 0;
double expectedMaxValue = Double.NEGATIVE_INFINITY;
try (Directory directory = newDirectory()) {
try (RandomIndexWriter iw = new RandomIndexWriter(random(), directory)) {
for (int i = 0; i < numRootDocs; i++) {
List<Document> documents = new ArrayList<>();
expectedMaxValue = Math.max(expectedMaxValue, generateMaxDocs(documents, 1, i, NESTED_OBJECT, VALUE_FIELD_NAME));
expectedNestedDocs += 1;
Document document = new Document();
document.add(new Field(IdFieldMapper.NAME, Uid.encodeId(Integer.toString(i)), IdFieldMapper.Defaults.FIELD_TYPE));
document.add(sequenceIDFields.primaryTerm);
documents.add(document);
iw.addDocuments(documents);
}
iw.commit();
}
try (IndexReader indexReader = wrapInMockESDirectoryReader(DirectoryReader.open(directory))) {
NestedAggregationBuilder nestedBuilder = new NestedAggregationBuilder(NESTED_AGG, NESTED_OBJECT).subAggregation(new TermsAggregationBuilder("terms").field(VALUE_FIELD_NAME).userValueTypeHint(ValueType.NUMERIC).subAggregation(new MaxAggregationBuilder(MAX_AGG_NAME).field(VALUE_FIELD_NAME)).subAggregation(new BucketScriptPipelineAggregationBuilder("bucketscript", Collections.singletonMap("_value", MAX_AGG_NAME), new Script(ScriptType.INLINE, MockScriptEngine.NAME, INVERSE_SCRIPT, Collections.emptyMap()))));
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(VALUE_FIELD_NAME, NumberFieldMapper.NumberType.LONG);
InternalNested nested = searchAndReduce(newSearcher(indexReader, false, true), new MatchAllDocsQuery(), nestedBuilder, fieldType);
assertEquals(expectedNestedDocs, nested.getDocCount());
assertEquals(NESTED_AGG, nested.getName());
assertEquals(expectedNestedDocs, nested.getDocCount());
InternalTerms<?, LongTerms.Bucket> terms = (InternalTerms) nested.getProperty("terms");
assertNotNull(terms);
for (LongTerms.Bucket bucket : terms.getBuckets()) {
InternalMax max = (InternalMax) bucket.getAggregations().asMap().get(MAX_AGG_NAME);
InternalSimpleValue bucketScript = (InternalSimpleValue) bucket.getAggregations().asMap().get("bucketscript");
assertNotNull(max);
assertNotNull(bucketScript);
assertEquals(max.getValue(), -bucketScript.getValue(), Double.MIN_VALUE);
}
assertTrue(AggregationInspectionHelper.hasValue(nested));
}
}
}
use of org.opensearch.search.aggregations.metrics.MaxAggregationBuilder in project OpenSearch by opensearch-project.
the class NestedAggregatorTests method testNoDocs.
public void testNoDocs() throws IOException {
try (Directory directory = newDirectory()) {
try (RandomIndexWriter iw = new RandomIndexWriter(random(), directory)) {
// intentionally not writing any docs
}
try (IndexReader indexReader = wrapInMockESDirectoryReader(DirectoryReader.open(directory))) {
NestedAggregationBuilder nestedBuilder = new NestedAggregationBuilder(NESTED_AGG, NESTED_OBJECT);
MaxAggregationBuilder maxAgg = new MaxAggregationBuilder(MAX_AGG_NAME).field(VALUE_FIELD_NAME);
nestedBuilder.subAggregation(maxAgg);
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(VALUE_FIELD_NAME, NumberFieldMapper.NumberType.LONG);
InternalNested nested = searchAndReduce(newSearcher(indexReader, false, true), new MatchAllDocsQuery(), nestedBuilder, fieldType);
assertEquals(NESTED_AGG, nested.getName());
assertEquals(0, nested.getDocCount());
InternalMax max = (InternalMax) nested.getProperty(MAX_AGG_NAME);
assertEquals(MAX_AGG_NAME, max.getName());
assertEquals(Double.NEGATIVE_INFINITY, max.getValue(), Double.MIN_VALUE);
assertFalse(AggregationInspectionHelper.hasValue(nested));
}
}
}
use of org.opensearch.search.aggregations.metrics.MaxAggregationBuilder in project OpenSearch by opensearch-project.
the class ReverseNestedAggregatorTests method testNoDocs.
public void testNoDocs() throws IOException {
try (Directory directory = newDirectory()) {
try (RandomIndexWriter iw = new RandomIndexWriter(random(), directory)) {
// intentionally not writing any docs
}
try (IndexReader indexReader = wrapInMockESDirectoryReader(DirectoryReader.open(directory))) {
NestedAggregationBuilder nestedBuilder = new NestedAggregationBuilder(NESTED_AGG, NESTED_OBJECT);
ReverseNestedAggregationBuilder reverseNestedBuilder = new ReverseNestedAggregationBuilder(REVERSE_AGG_NAME);
nestedBuilder.subAggregation(reverseNestedBuilder);
MaxAggregationBuilder maxAgg = new MaxAggregationBuilder(MAX_AGG_NAME).field(VALUE_FIELD_NAME);
reverseNestedBuilder.subAggregation(maxAgg);
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(VALUE_FIELD_NAME, NumberFieldMapper.NumberType.LONG);
Nested nested = searchAndReduce(newSearcher(indexReader, false, true), new MatchAllDocsQuery(), nestedBuilder, fieldType);
ReverseNested reverseNested = (ReverseNested) ((InternalAggregation) nested).getProperty(REVERSE_AGG_NAME);
assertEquals(REVERSE_AGG_NAME, reverseNested.getName());
assertEquals(0, reverseNested.getDocCount());
InternalMax max = (InternalMax) ((InternalAggregation) reverseNested).getProperty(MAX_AGG_NAME);
assertEquals(MAX_AGG_NAME, max.getName());
assertEquals(Double.NEGATIVE_INFINITY, max.getValue(), Double.MIN_VALUE);
}
}
}
use of org.opensearch.search.aggregations.metrics.MaxAggregationBuilder in project OpenSearch by opensearch-project.
the class ReverseNestedAggregatorTests method testFieldAlias.
public void testFieldAlias() throws IOException {
int numParentDocs = randomIntBetween(1, 20);
int expectedParentDocs = 0;
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(VALUE_FIELD_NAME, NumberFieldMapper.NumberType.LONG);
try (Directory directory = newDirectory()) {
try (RandomIndexWriter iw = new RandomIndexWriter(random(), directory)) {
for (int i = 0; i < numParentDocs; i++) {
List<Document> documents = new ArrayList<>();
int numNestedDocs = randomIntBetween(0, 20);
if (numNestedDocs > 0) {
expectedParentDocs++;
}
for (int nested = 0; nested < numNestedDocs; nested++) {
Document document = new Document();
document.add(new Field(IdFieldMapper.NAME, Uid.encodeId(Integer.toString(i)), IdFieldMapper.Defaults.NESTED_FIELD_TYPE));
document.add(new Field(TypeFieldMapper.NAME, "__" + NESTED_OBJECT, TypeFieldMapper.Defaults.NESTED_FIELD_TYPE));
documents.add(document);
}
Document document = new Document();
document.add(new Field(IdFieldMapper.NAME, Uid.encodeId(Integer.toString(i)), IdFieldMapper.Defaults.FIELD_TYPE));
long value = randomNonNegativeLong() % 10000;
document.add(new SortedNumericDocValuesField(VALUE_FIELD_NAME, value));
document.add(SeqNoFieldMapper.SequenceIDFields.emptySeqID().primaryTerm);
documents.add(document);
iw.addDocuments(documents);
}
iw.commit();
}
try (IndexReader indexReader = wrapInMockESDirectoryReader(DirectoryReader.open(directory))) {
MaxAggregationBuilder maxAgg = max(MAX_AGG_NAME).field(VALUE_FIELD_NAME);
MaxAggregationBuilder aliasMaxAgg = max(MAX_AGG_NAME).field(VALUE_FIELD_NAME + "-alias");
NestedAggregationBuilder agg = nested(NESTED_AGG, NESTED_OBJECT).subAggregation(reverseNested(REVERSE_AGG_NAME).subAggregation(maxAgg));
NestedAggregationBuilder aliasAgg = nested(NESTED_AGG, NESTED_OBJECT).subAggregation(reverseNested(REVERSE_AGG_NAME).subAggregation(aliasMaxAgg));
Nested nested = searchAndReduce(newSearcher(indexReader, false, true), new MatchAllDocsQuery(), agg, fieldType);
Nested aliasNested = searchAndReduce(newSearcher(indexReader, false, true), new MatchAllDocsQuery(), aliasAgg, fieldType);
ReverseNested reverseNested = nested.getAggregations().get(REVERSE_AGG_NAME);
ReverseNested aliasReverseNested = aliasNested.getAggregations().get(REVERSE_AGG_NAME);
assertEquals(reverseNested, aliasReverseNested);
assertEquals(expectedParentDocs, reverseNested.getDocCount());
}
}
}
use of org.opensearch.search.aggregations.metrics.MaxAggregationBuilder in project OpenSearch by opensearch-project.
the class RareTermsAggregatorTests method testEmbeddedMaxAgg.
public void testEmbeddedMaxAgg() throws IOException {
Query query = new MatchAllDocsQuery();
testSearchCase(query, dataset, aggregation -> {
MaxAggregationBuilder max = new MaxAggregationBuilder("the_max").field(LONG_FIELD);
aggregation.field(LONG_FIELD).maxDocCount(1).subAggregation(max);
}, agg -> {
assertEquals(1, agg.getBuckets().size());
LongRareTerms.Bucket bucket = (LongRareTerms.Bucket) agg.getBuckets().get(0);
assertThat(bucket.getKey(), equalTo(1L));
assertThat(bucket.getDocCount(), equalTo(1L));
Aggregations children = bucket.getAggregations();
assertThat(children.asList().size(), equalTo(1));
assertThat(children.asList().get(0).getName(), equalTo("the_max"));
assertThat(((Max) (children.asList().get(0))).getValue(), equalTo(1.0));
});
testSearchCase(query, dataset, aggregation -> {
MaxAggregationBuilder max = new MaxAggregationBuilder("the_max").field(LONG_FIELD);
aggregation.field(KEYWORD_FIELD).maxDocCount(1).subAggregation(max);
}, agg -> {
assertEquals(1, agg.getBuckets().size());
StringRareTerms.Bucket bucket = (StringRareTerms.Bucket) agg.getBuckets().get(0);
assertThat(bucket.getKey(), equalTo("1"));
assertThat(bucket.getDocCount(), equalTo(1L));
Aggregations children = bucket.getAggregations();
assertThat(children.asList().size(), equalTo(1));
assertThat(children.asList().get(0).getName(), equalTo("the_max"));
assertThat(((Max) (children.asList().get(0))).getValue(), equalTo(1.0));
});
}
Aggregations