use of org.opensearch.search.aggregations.metrics.MinAggregationBuilder in project fess by codelibs.
the class EsAbstractConditionAggregation method regMinA.
protected MinAggregationBuilder regMinA(String name, String field) {
MinAggregationBuilder builder = AggregationBuilders.min(name).field(field);
regA(builder);
return builder;
}
use of org.opensearch.search.aggregations.metrics.MinAggregationBuilder in project fess by codelibs.
the class EsAbstractConditionAggregation method regMinA.
protected MinAggregationBuilder regMinA(String name, String field) {
MinAggregationBuilder builder = AggregationBuilders.min(name).field(field);
regA(builder);
return builder;
}
use of org.opensearch.search.aggregations.metrics.MinAggregationBuilder in project fess by codelibs.
the class EsAbstractConditionAggregation method regMinA.
protected MinAggregationBuilder regMinA(String name, String field) {
MinAggregationBuilder builder = AggregationBuilders.min(name).field(field);
regA(builder);
return builder;
}
use of org.opensearch.search.aggregations.metrics.MinAggregationBuilder 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.MinAggregationBuilder in project OpenSearch by opensearch-project.
the class NestedAggregatorTests method testNestedOrdering_random.
public void testNestedOrdering_random() throws IOException {
int numBooks = randomIntBetween(32, 512);
List<Tuple<String, int[]>> books = new ArrayList<>();
for (int i = 0; i < numBooks; i++) {
int numChapters = randomIntBetween(1, 8);
int[] chapters = new int[numChapters];
for (int j = 0; j < numChapters; j++) {
chapters[j] = randomIntBetween(2, 64);
}
books.add(Tuple.tuple(String.format(Locale.ROOT, "%03d", i), chapters));
}
try (Directory directory = newDirectory()) {
try (RandomIndexWriter iw = new RandomIndexWriter(random(), directory)) {
int id = 0;
for (Tuple<String, int[]> book : books) {
iw.addDocuments(generateBook(String.format(Locale.ROOT, "%03d", id), new String[] { book.v1() }, book.v2()));
id++;
}
}
for (Tuple<String, int[]> book : books) {
Arrays.sort(book.v2());
}
books.sort((o1, o2) -> {
int cmp = Integer.compare(o1.v2()[0], o2.v2()[0]);
if (cmp == 0) {
return o1.v1().compareTo(o2.v1());
} else {
return cmp;
}
});
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).size(books.size()).field("author").order(BucketOrder.compound(BucketOrder.aggregation("chapters>num_pages.value", true), BucketOrder.key(true)));
NestedAggregationBuilder nestedBuilder = new NestedAggregationBuilder("chapters", "nested_chapters");
MinAggregationBuilder minAgg = new MinAggregationBuilder("num_pages").field("num_pages");
nestedBuilder.subAggregation(minAgg);
termsBuilder.subAggregation(nestedBuilder);
Terms terms = searchAndReduce(newSearcher(indexReader, false, true), new MatchAllDocsQuery(), termsBuilder, fieldType1, fieldType2);
assertEquals(books.size(), terms.getBuckets().size());
assertEquals("authors", terms.getName());
for (int i = 0; i < books.size(); i++) {
Tuple<String, int[]> book = books.get(i);
Terms.Bucket bucket = terms.getBuckets().get(i);
assertEquals(book.v1(), bucket.getKeyAsString());
Min numPages = ((Nested) bucket.getAggregations().get("chapters")).getAggregations().get("num_pages");
assertEquals(book.v2()[0], (int) numPages.getValue());
}
}
}
}
Aggregations