use of org.opensearch.search.aggregations.bucket.terms.LongTerms in project OpenSearch by opensearch-project.
the class NestedIT method testNestedAsSubAggregation.
public void testNestedAsSubAggregation() throws Exception {
SearchResponse response = client().prepareSearch("idx").addAggregation(terms("top_values").field("value").size(100).collectMode(aggCollectionMode).subAggregation(nested("nested", "nested").subAggregation(max("max_value").field("nested.value")))).get();
assertSearchResponse(response);
LongTerms values = response.getAggregations().get("top_values");
assertThat(values, notNullValue());
assertThat(values.getName(), equalTo("top_values"));
assertThat(values.getBuckets(), notNullValue());
assertThat(values.getBuckets().size(), equalTo(numParents));
for (int i = 0; i < numParents; i++) {
String topValue = "" + (i + 1);
assertThat(values.getBucketByKey(topValue), notNullValue());
Nested nested = values.getBucketByKey(topValue).getAggregations().get("nested");
assertThat(nested, notNullValue());
Max max = nested.getAggregations().get("max_value");
assertThat(max, notNullValue());
assertThat(max.getValue(), equalTo(numChildren[i] == 0 ? Double.NEGATIVE_INFINITY : (double) i + numChildren[i]));
}
}
use of org.opensearch.search.aggregations.bucket.terms.LongTerms in project OpenSearch by opensearch-project.
the class ChildrenToParentAggregatorTests method testTermsParentChildTerms.
public void testTermsParentChildTerms() throws IOException {
Directory directory = newDirectory();
RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
final Map<String, Tuple<Integer, Integer>> expectedParentChildRelations = setupIndex(indexWriter);
indexWriter.close();
SortedMap<Integer, Long> sortedValues = new TreeMap<>();
for (Tuple<Integer, Integer> value : expectedParentChildRelations.values()) {
Long l = sortedValues.computeIfAbsent(value.v2(), integer -> 0L);
sortedValues.put(value.v2(), l + 1);
}
IndexReader indexReader = OpenSearchDirectoryReader.wrap(DirectoryReader.open(directory), new ShardId(new Index("foo", "_na_"), 1));
// TODO set "maybeWrap" to true for IndexSearcher once #23338 is resolved
IndexSearcher indexSearcher = newSearcher(indexReader, false, true);
// verify a terms-aggregation inside the parent-aggregation which itself is inside a
// terms-aggregation on the child-documents
testCaseTermsParentTerms(new MatchAllDocsQuery(), indexSearcher, longTerms -> {
assertNotNull(longTerms);
for (LongTerms.Bucket bucket : longTerms.getBuckets()) {
assertNotNull(bucket);
assertNotNull(bucket.getKeyAsString());
}
});
indexReader.close();
directory.close();
}
use of org.opensearch.search.aggregations.bucket.terms.LongTerms in project OpenSearch by opensearch-project.
the class VariableWidthHistogramAggregatorTests method testSubAggregationReduction.
public void testSubAggregationReduction() throws IOException {
final List<Number> dataset = Arrays.asList(1L, 1L, 1L, 2L, 2L);
testSearchCase(DEFAULT_QUERY, dataset, false, aggregation -> aggregation.field(NUMERIC_FIELD).setNumBuckets(3).setInitialBuffer(12).setShardSize(4).subAggregation(new TermsAggregationBuilder("terms").field(NUMERIC_FIELD).shardSize(2).size(1)), histogram -> {
double deltaError = 1d / 10000d;
// This is a test to make sure that the sub aggregations get reduced
// This terms sub aggregation has shardSize (2) != size (1), so we will get 1 bucket only if
// InternalVariableWidthHistogram reduces the sub aggregations.
LongTerms terms = histogram.getBuckets().get(0).getAggregations().get("terms");
assertEquals(1L, terms.getBuckets().size(), deltaError);
assertEquals(1L, terms.getBuckets().get(0).getKey());
});
}
use of org.opensearch.search.aggregations.bucket.terms.LongTerms in project OpenSearch by opensearch-project.
the class ChildrenToParentAggregatorTests method testCaseTermsParentTerms.
// run a terms aggregation on the number in child-documents, then a parent aggregation and then terms on the parent-number
private void testCaseTermsParentTerms(Query query, IndexSearcher indexSearcher, Consumer<LongTerms> verify) throws IOException {
AggregationBuilder aggregationBuilder = new TermsAggregationBuilder("subvalue_terms").field("subNumber").subAggregation(new ParentAggregationBuilder("to_parent", CHILD_TYPE).subAggregation(new TermsAggregationBuilder("value_terms").field("number")));
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.LONG);
MappedFieldType subFieldType = new NumberFieldMapper.NumberFieldType("subNumber", NumberFieldMapper.NumberType.LONG);
LongTerms result = searchAndReduce(indexSearcher, query, aggregationBuilder, fieldType, subFieldType);
verify.accept(result);
}
use of org.opensearch.search.aggregations.bucket.terms.LongTerms in project OpenSearch by opensearch-project.
the class ChildrenToParentAggregatorTests method testParentChildTerms.
public void testParentChildTerms() throws IOException {
Directory directory = newDirectory();
RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
final Map<String, Tuple<Integer, Integer>> expectedParentChildRelations = setupIndex(indexWriter);
indexWriter.close();
SortedMap<Integer, Long> entries = new TreeMap<>();
for (Tuple<Integer, Integer> value : expectedParentChildRelations.values()) {
Long l = entries.computeIfAbsent(value.v2(), integer -> 0L);
entries.put(value.v2(), l + 1);
}
List<Map.Entry<Integer, Long>> sortedValues = new ArrayList<>(entries.entrySet());
sortedValues.sort((o1, o2) -> {
// sort larger values first
int ret = o2.getValue().compareTo(o1.getValue());
if (ret != 0) {
return ret;
}
// on equal value, sort by key
return o1.getKey().compareTo(o2.getKey());
});
IndexReader indexReader = OpenSearchDirectoryReader.wrap(DirectoryReader.open(directory), new ShardId(new Index("foo", "_na_"), 1));
// TODO set "maybeWrap" to true for IndexSearcher once #23338 is resolved
IndexSearcher indexSearcher = newSearcher(indexReader, false, true);
// verify a terms-aggregation inside the parent-aggregation
testCaseTerms(new MatchAllDocsQuery(), indexSearcher, parent -> {
assertNotNull(parent);
assertTrue(JoinAggregationInspectionHelper.hasValue(parent));
LongTerms valueTerms = parent.getAggregations().get("value_terms");
assertNotNull(valueTerms);
List<LongTerms.Bucket> valueTermsBuckets = valueTerms.getBuckets();
assertNotNull(valueTermsBuckets);
assertEquals("Had: " + parent, sortedValues.size(), valueTermsBuckets.size());
int i = 0;
for (Map.Entry<Integer, Long> entry : sortedValues) {
LongTerms.Bucket bucket = valueTermsBuckets.get(i);
assertEquals(entry.getKey().longValue(), bucket.getKeyAsNumber());
assertEquals(entry.getValue(), (Long) bucket.getDocCount());
i++;
}
});
indexReader.close();
directory.close();
}
Aggregations