use of org.opensearch.search.aggregations.metrics.InternalMin in project OpenSearch by opensearch-project.
the class RangeHistogramAggregatorTests method testAsSubAgg.
public void testAsSubAgg() throws IOException {
AggregationBuilder request = new HistogramAggregationBuilder("outer").field("outer").interval(5).subAggregation(new HistogramAggregationBuilder("inner").field("inner").interval(5).subAggregation(new MinAggregationBuilder("min").field("n")));
CheckedConsumer<RandomIndexWriter, IOException> buildIndex = iw -> {
List<List<IndexableField>> docs = new ArrayList<>();
for (int n = 0; n < 10000; n++) {
BytesRef outerRange = RangeType.LONG.encodeRanges(org.opensearch.common.collect.Set.of(new RangeFieldMapper.Range(RangeType.LONG, n % 100, n % 100 + 10, true, true)));
BytesRef innerRange = RangeType.LONG.encodeRanges(org.opensearch.common.collect.Set.of(new RangeFieldMapper.Range(RangeType.LONG, n / 100, n / 100 + 10, true, true)));
docs.add(org.opensearch.common.collect.List.of(new BinaryDocValuesField("outer", outerRange), new BinaryDocValuesField("inner", innerRange), new SortedNumericDocValuesField("n", n)));
}
iw.addDocuments(docs);
};
Consumer<InternalHistogram> verify = outer -> {
assertThat(outer.getBuckets(), hasSize(22));
for (int outerIdx = 0; outerIdx < 22; outerIdx++) {
InternalHistogram.Bucket outerBucket = outer.getBuckets().get(outerIdx);
assertThat(outerBucket.getKey(), equalTo(5.0 * outerIdx));
InternalHistogram inner = outerBucket.getAggregations().get("inner");
assertThat(inner.getBuckets(), hasSize(22));
for (int innerIdx = 0; innerIdx < 22; innerIdx++) {
InternalHistogram.Bucket innerBucket = inner.getBuckets().get(innerIdx);
assertThat(innerBucket.getKey(), equalTo(5.0 * innerIdx));
InternalMin min = innerBucket.getAggregations().get("min");
int minOuterIdxWithOverlappingRange = Math.max(0, outerIdx - 2);
int minInnerIdxWithOverlappingRange = Math.max(0, innerIdx - 2);
assertThat(min.getValue(), equalTo(minOuterIdxWithOverlappingRange * 5.0 + minInnerIdxWithOverlappingRange * 500.0));
}
}
};
testCase(request, new MatchAllDocsQuery(), buildIndex, verify, rangeField("outer", RangeType.LONG), rangeField("inner", RangeType.LONG), longField("n"));
}
use of org.opensearch.search.aggregations.metrics.InternalMin in project OpenSearch by opensearch-project.
the class NumericHistogramAggregatorTests method testAsSubAgg.
public void testAsSubAgg() throws IOException {
AggregationBuilder request = new HistogramAggregationBuilder("outer").field("outer").interval(5).subAggregation(new HistogramAggregationBuilder("inner").field("inner").interval(5).subAggregation(new MinAggregationBuilder("min").field("n")));
CheckedConsumer<RandomIndexWriter, IOException> buildIndex = iw -> {
List<List<IndexableField>> docs = new ArrayList<>();
for (int n = 0; n < 10000; n++) {
docs.add(org.opensearch.common.collect.List.of(new SortedNumericDocValuesField("outer", n % 100), new SortedNumericDocValuesField("inner", n / 100), new SortedNumericDocValuesField("n", n)));
}
iw.addDocuments(docs);
};
Consumer<InternalHistogram> verify = outer -> {
assertThat(outer.getBuckets(), hasSize(20));
for (int outerIdx = 0; outerIdx < 20; outerIdx++) {
InternalHistogram.Bucket outerBucket = outer.getBuckets().get(outerIdx);
assertThat(outerBucket.getKey(), equalTo(5.0 * outerIdx));
InternalHistogram inner = outerBucket.getAggregations().get("inner");
assertThat(inner.getBuckets(), hasSize(20));
for (int innerIdx = 0; innerIdx < 20; innerIdx++) {
InternalHistogram.Bucket innerBucket = inner.getBuckets().get(innerIdx);
assertThat(innerBucket.getKey(), equalTo(5.0 * innerIdx));
InternalMin min = innerBucket.getAggregations().get("min");
assertThat(min.getValue(), equalTo(outerIdx * 5.0 + innerIdx * 500.0));
}
}
};
testCase(request, new MatchAllDocsQuery(), buildIndex, verify, longField("outer"), longField("inner"), longField("n"));
}
use of org.opensearch.search.aggregations.metrics.InternalMin in project OpenSearch by opensearch-project.
the class InternalSingleBucketAggregationTestCase method mutateInstance.
@Override
protected T mutateInstance(T instance) {
String name = instance.getName();
long docCount = instance.getDocCount();
InternalAggregations aggregations = instance.getAggregations();
Map<String, Object> metadata = instance.getMetadata();
switch(between(0, 3)) {
case 0:
name += randomAlphaOfLength(5);
break;
case 1:
docCount += between(1, 2000);
break;
case 2:
List<InternalAggregation> aggs = new ArrayList<>();
aggs.add(new InternalMax("new_max", randomDouble(), randomNumericDocValueFormat(), emptyMap()));
aggs.add(new InternalMin("new_min", randomDouble(), randomNumericDocValueFormat(), emptyMap()));
aggregations = InternalAggregations.from(aggs);
break;
case 3:
default:
if (metadata == null) {
metadata = new HashMap<>(1);
} else {
metadata = new HashMap<>(instance.getMetadata());
}
metadata.put(randomAlphaOfLength(15), randomInt());
break;
}
return createTestInstance(name, docCount, aggregations, metadata);
}
use of org.opensearch.search.aggregations.metrics.InternalMin in project OpenSearch by opensearch-project.
the class InternalSingleBucketAggregationTestCase method assertReduced.
@Override
protected final void assertReduced(T reduced, List<T> inputs) {
assertEquals(inputs.stream().mapToLong(InternalSingleBucketAggregation::getDocCount).sum(), reduced.getDocCount());
if (hasInternalMax) {
double expected = inputs.stream().mapToDouble(i -> {
InternalMax max = i.getAggregations().get("max");
return max.getValue();
}).max().getAsDouble();
InternalMax reducedMax = reduced.getAggregations().get("max");
assertEquals(expected, reducedMax.getValue(), 0);
}
if (hasInternalMin) {
double expected = inputs.stream().mapToDouble(i -> {
InternalMin min = i.getAggregations().get("min");
return min.getValue();
}).min().getAsDouble();
InternalMin reducedMin = reduced.getAggregations().get("min");
assertEquals(expected, reducedMin.getValue(), 0);
}
extraAssertReduced(reduced, inputs);
}
use of org.opensearch.search.aggregations.metrics.InternalMin in project OpenSearch by opensearch-project.
the class ParentToChildrenAggregatorTests method testParentChildAsSubAgg.
public void testParentChildAsSubAgg() throws IOException {
try (Directory directory = newDirectory()) {
RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
final Map<String, Tuple<Integer, Integer>> expectedParentChildRelations = setupIndex(indexWriter);
indexWriter.close();
try (IndexReader indexReader = OpenSearchDirectoryReader.wrap(DirectoryReader.open(directory), new ShardId(new Index("foo", "_na_"), 1))) {
IndexSearcher indexSearcher = newSearcher(indexReader, false, true);
AggregationBuilder request = new TermsAggregationBuilder("t").field("kwd").subAggregation(new ChildrenAggregationBuilder("children", CHILD_TYPE).subAggregation(new MinAggregationBuilder("min").field("number")));
long expectedEvenChildCount = 0;
double expectedEvenMin = Double.MAX_VALUE;
long expectedOddChildCount = 0;
double expectedOddMin = Double.MAX_VALUE;
for (Map.Entry<String, Tuple<Integer, Integer>> e : expectedParentChildRelations.entrySet()) {
if (Integer.valueOf(e.getKey().substring("parent".length())) % 2 == 0) {
expectedEvenChildCount += e.getValue().v1();
expectedEvenMin = Math.min(expectedEvenMin, e.getValue().v2());
} else {
expectedOddChildCount += e.getValue().v1();
expectedOddMin = Math.min(expectedOddMin, e.getValue().v2());
}
}
StringTerms result = searchAndReduce(indexSearcher, new MatchAllDocsQuery(), request, longField("number"), keywordField("kwd"));
StringTerms.Bucket evenBucket = result.getBucketByKey("even");
InternalChildren evenChildren = evenBucket.getAggregations().get("children");
InternalMin evenMin = evenChildren.getAggregations().get("min");
assertThat(evenChildren.getDocCount(), equalTo(expectedEvenChildCount));
assertThat(evenMin.getValue(), equalTo(expectedEvenMin));
if (expectedOddChildCount > 0) {
StringTerms.Bucket oddBucket = result.getBucketByKey("odd");
InternalChildren oddChildren = oddBucket.getAggregations().get("children");
InternalMin oddMin = oddChildren.getAggregations().get("min");
assertThat(oddChildren.getDocCount(), equalTo(expectedOddChildCount));
assertThat(oddMin.getValue(), equalTo(expectedOddMin));
} else {
assertNull(result.getBucketByKey("odd"));
}
}
}
}
Aggregations