use of org.opensearch.search.aggregations.Aggregation in project OpenSearch by opensearch-project.
the class MatrixStatsNamedXContentProvider method getNamedXContentParsers.
@Override
public List<NamedXContentRegistry.Entry> getNamedXContentParsers() {
ParseField parseField = new ParseField(MatrixStatsAggregationBuilder.NAME);
ContextParser<Object, Aggregation> contextParser = (p, name) -> ParsedMatrixStats.fromXContent(p, (String) name);
return singletonList(new NamedXContentRegistry.Entry(Aggregation.class, parseField, contextParser));
}
use of org.opensearch.search.aggregations.Aggregation in project OpenSearch by opensearch-project.
the class NaNSortingIT method assertCorrectlySorted.
private void assertCorrectlySorted(Histogram histo, boolean asc, SubAggregation agg) {
assertThat(histo, notNullValue());
double previousValue = asc ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY;
for (Histogram.Bucket bucket : histo.getBuckets()) {
Aggregation sub = bucket.getAggregations().get(agg.name);
double value = agg.getValue(sub);
assertTrue(Comparators.compareDiscardNaN(previousValue, value, asc) <= 0);
previousValue = value;
}
}
use of org.opensearch.search.aggregations.Aggregation in project OpenSearch by opensearch-project.
the class ParentIT method testSimpleParentAgg.
public void testSimpleParentAgg() throws Exception {
final SearchRequestBuilder searchRequest = client().prepareSearch("test").setSize(10000).setQuery(matchQuery("randomized", true)).addAggregation(parent("to_article", "comment").subAggregation(terms("category").field("category").size(10000)));
SearchResponse searchResponse = searchRequest.get();
assertSearchResponse(searchResponse);
long articlesWithComment = articleToControl.values().stream().filter(parentControl -> !parentControl.commentIds.isEmpty()).count();
Parent parentAgg = searchResponse.getAggregations().get("to_article");
assertThat("Request: " + searchRequest + "\nResponse: " + searchResponse + "\n", parentAgg.getDocCount(), equalTo(articlesWithComment));
Terms categoryTerms = parentAgg.getAggregations().get("category");
long categoriesWithComments = categoryToControl.values().stream().filter(control -> !control.commentIds.isEmpty()).count();
assertThat("Buckets: " + categoryTerms.getBuckets().stream().map((Function<Terms.Bucket, String>) MultiBucketsAggregation.Bucket::getKeyAsString).collect(Collectors.toList()) + "\nCategories: " + categoryToControl.keySet(), (long) categoryTerms.getBuckets().size(), equalTo(categoriesWithComments));
for (Map.Entry<String, Control> entry : categoryToControl.entrySet()) {
// no children for this category -> no entry in the child to parent-aggregation
if (entry.getValue().commentIds.isEmpty()) {
assertNull(categoryTerms.getBucketByKey(entry.getKey()));
continue;
}
final Terms.Bucket categoryBucket = categoryTerms.getBucketByKey(entry.getKey());
assertNotNull("Failed for category " + entry.getKey(), categoryBucket);
assertThat("Failed for category " + entry.getKey(), categoryBucket.getKeyAsString(), equalTo(entry.getKey()));
// count all articles in this category which have at least one comment
long articlesForCategory = articleToControl.values().stream().filter(parentControl -> parentControl.category.equals(entry.getKey())).filter(parentControl -> !parentControl.commentIds.isEmpty()).count();
assertThat("Failed for category " + entry.getKey(), categoryBucket.getDocCount(), equalTo(articlesForCategory));
}
}
use of org.opensearch.search.aggregations.Aggregation in project OpenSearch by opensearch-project.
the class ChildrenToParentAggregatorTests method testNoDocs.
public void testNoDocs() throws IOException {
Directory directory = newDirectory();
RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
// intentionally not writing any docs
indexWriter.close();
IndexReader indexReader = DirectoryReader.open(directory);
testCase(new MatchAllDocsQuery(), newSearcher(indexReader, false, true), childrenToParent -> {
assertEquals(0, childrenToParent.getDocCount());
Aggregation parentAggregation = childrenToParent.getAggregations().get("in_parent");
assertEquals(0, childrenToParent.getDocCount());
assertNotNull("Aggregations: " + childrenToParent.getAggregations().asMap(), parentAggregation);
assertEquals(Double.POSITIVE_INFINITY, ((InternalMin) parentAggregation).getValue(), Double.MIN_VALUE);
assertFalse(JoinAggregationInspectionHelper.hasValue(childrenToParent));
});
indexReader.close();
directory.close();
}
use of org.opensearch.search.aggregations.Aggregation in project OpenSearch by opensearch-project.
the class ParentJoinNamedXContentProvider method getNamedXContentParsers.
@Override
public List<NamedXContentRegistry.Entry> getNamedXContentParsers() {
ParseField parseFieldChildren = new ParseField(ChildrenAggregationBuilder.NAME);
ParseField parseFieldParent = new ParseField(ParentAggregationBuilder.NAME);
ContextParser<Object, Aggregation> contextParserChildren = (p, name) -> ParsedChildren.fromXContent(p, (String) name);
ContextParser<Object, Aggregation> contextParserParent = (p, name) -> ParsedParent.fromXContent(p, (String) name);
return Arrays.asList(new NamedXContentRegistry.Entry(Aggregation.class, parseFieldChildren, contextParserChildren), new NamedXContentRegistry.Entry(Aggregation.class, parseFieldParent, contextParserParent));
}
Aggregations