use of org.opensearch.search.aggregations.Aggregation in project OpenSearch by opensearch-project.
the class InternalMultiBucketAggregationTestCase method assertBucket.
protected void assertBucket(MultiBucketsAggregation.Bucket expected, MultiBucketsAggregation.Bucket actual, boolean checkOrder) {
assertTrue(expected instanceof InternalMultiBucketAggregation.InternalBucket);
assertTrue(actual instanceof ParsedMultiBucketAggregation.ParsedBucket);
assertEquals(expected.getKey(), actual.getKey());
assertEquals(expected.getKeyAsString(), actual.getKeyAsString());
assertEquals(expected.getDocCount(), actual.getDocCount());
Aggregations expectedAggregations = expected.getAggregations();
Aggregations actualAggregations = actual.getAggregations();
assertEquals(expectedAggregations.asList().size(), actualAggregations.asList().size());
if (checkOrder) {
Iterator<Aggregation> expectedIt = expectedAggregations.iterator();
Iterator<Aggregation> actualIt = actualAggregations.iterator();
while (expectedIt.hasNext()) {
Aggregation expectedAggregation = expectedIt.next();
Aggregation actualAggregation = actualIt.next();
assertMultiBucketsAggregations(expectedAggregation, actualAggregation, true);
}
} else {
for (Aggregation expectedAggregation : expectedAggregations) {
Aggregation actualAggregation = actualAggregations.get(expectedAggregation.getName());
assertNotNull(actualAggregation);
assertMultiBucketsAggregations(expectedAggregation, actualAggregation, false);
}
}
}
use of org.opensearch.search.aggregations.Aggregation in project OpenSearch by opensearch-project.
the class AutoDateHistogramAggregatorTests method testRandomDayIntervals.
public void testRandomDayIntervals() throws IOException {
final int length = 140;
final List<ZonedDateTime> dataset = new ArrayList<>(length);
final ZonedDateTime startDate = ZonedDateTime.of(2017, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
for (int i = 0; i < length; i++) {
final ZonedDateTime date = startDate.plusDays(i);
dataset.add(date);
}
final int randomChoice = randomIntBetween(1, 3);
if (randomChoice == 1) {
testSearchCase(DEFAULT_QUERY, dataset, aggregation -> aggregation.setNumBuckets(length).field(DATE_FIELD), histogram -> {
final List<? extends Histogram.Bucket> buckets = histogram.getBuckets();
assertEquals(length, buckets.size());
final int randomIndex = randomInt(length - 1);
final Histogram.Bucket bucket = buckets.get(randomIndex);
assertEquals(startDate.plusDays(randomIndex), bucket.getKey());
assertEquals(1, bucket.getDocCount());
});
} else if (randomChoice == 2) {
testSearchCase(DEFAULT_QUERY, dataset, aggregation -> aggregation.setNumBuckets(60).field(DATE_FIELD), histogram -> {
final List<? extends Histogram.Bucket> buckets = histogram.getBuckets();
final int expectedDocCount = 7;
assertEquals(20, buckets.size());
final int randomIndex = randomInt(19);
final Histogram.Bucket bucket = buckets.get(randomIndex);
assertEquals(startDate.plusDays(randomIndex * expectedDocCount), bucket.getKey());
assertEquals(expectedDocCount, bucket.getDocCount());
});
} else if (randomChoice == 3) {
testSearchCase(DEFAULT_QUERY, dataset, aggregation -> aggregation.setNumBuckets(6).field(DATE_FIELD), histogram -> {
final List<? extends Histogram.Bucket> buckets = histogram.getBuckets();
assertEquals(5, buckets.size());
final int randomIndex = randomInt(2);
final Histogram.Bucket bucket = buckets.get(randomIndex);
assertEquals(startDate.plusMonths(randomIndex), bucket.getKey());
assertEquals(YearMonth.from(startDate.plusMonths(randomIndex)).lengthOfMonth(), bucket.getDocCount());
});
}
}
use of org.opensearch.search.aggregations.Aggregation in project OpenSearch by opensearch-project.
the class AvgBucketAggregatorTests method testSameAggNames.
/**
* Test for issue #30608. Under the following circumstances:
*
* A. Multi-bucket agg in the first entry of our internal list
* B. Regular agg as the immediate child of the multi-bucket in A
* C. Regular agg with the same name as B at the top level, listed as the second entry in our internal list
* D. Finally, a pipeline agg with the path down to B
*
* BucketMetrics reduction would throw a class cast exception due to bad subpathing. This test ensures
* it is fixed.
*
* Note: we have this test inside of the `avg_bucket` package so that we can get access to the package-private
* `reduce()` needed for testing this
*/
public void testSameAggNames() throws IOException {
Query query = new MatchAllDocsQuery();
AvgAggregationBuilder avgBuilder = new AvgAggregationBuilder("foo").field(VALUE_FIELD);
DateHistogramAggregationBuilder histo = new DateHistogramAggregationBuilder("histo").calendarInterval(DateHistogramInterval.YEAR).field(DATE_FIELD).subAggregation(new AvgAggregationBuilder("foo").field(VALUE_FIELD));
AvgBucketPipelineAggregationBuilder avgBucketBuilder = new AvgBucketPipelineAggregationBuilder("the_avg_bucket", "histo>foo");
try (Directory directory = newDirectory()) {
try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
Document document = new Document();
for (String date : dataset) {
if (frequently()) {
indexWriter.commit();
}
document.add(new SortedNumericDocValuesField(DATE_FIELD, asLong(date)));
document.add(new SortedNumericDocValuesField(VALUE_FIELD, randomInt()));
indexWriter.addDocument(document);
document.clear();
}
}
InternalAvg avgResult;
InternalDateHistogram histogramResult;
try (IndexReader indexReader = DirectoryReader.open(directory)) {
IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
DateFieldMapper.DateFieldType fieldType = new DateFieldMapper.DateFieldType(DATE_FIELD);
MappedFieldType valueFieldType = new NumberFieldMapper.NumberFieldType(VALUE_FIELD, NumberFieldMapper.NumberType.LONG);
avgResult = searchAndReduce(indexSearcher, query, avgBuilder, 10000, new MappedFieldType[] { fieldType, valueFieldType });
histogramResult = searchAndReduce(indexSearcher, query, histo, 10000, new MappedFieldType[] { fieldType, valueFieldType });
}
// Finally, reduce the pipeline agg
PipelineAggregator avgBucketAgg = avgBucketBuilder.createInternal(Collections.emptyMap());
List<Aggregation> reducedAggs = new ArrayList<>(2);
// Histo has to go first to exercise the bug
reducedAggs.add(histogramResult);
reducedAggs.add(avgResult);
Aggregations aggregations = new Aggregations(reducedAggs);
InternalAggregation pipelineResult = ((AvgBucketPipelineAggregator) avgBucketAgg).doReduce(aggregations, null);
assertNotNull(pipelineResult);
}
}
use of org.opensearch.search.aggregations.Aggregation in project OpenSearch by opensearch-project.
the class TopHitsAggregatorTests method testTopLevel.
public void testTopLevel() throws Exception {
Aggregation result;
if (randomBoolean()) {
result = testCase(new MatchAllDocsQuery(), topHits("_name").sort("string", SortOrder.DESC));
} else {
Query query = new QueryParser("string", new KeywordAnalyzer()).parse("d^1000 c^100 b^10 a^1");
result = testCase(query, topHits("_name"));
}
SearchHits searchHits = ((TopHits) result).getHits();
assertEquals(3L, searchHits.getTotalHits().value);
assertEquals("3", searchHits.getAt(0).getId());
assertEquals("2", searchHits.getAt(1).getId());
assertEquals("1", searchHits.getAt(2).getId());
assertTrue(AggregationInspectionHelper.hasValue(((InternalTopHits) result)));
}
use of org.opensearch.search.aggregations.Aggregation in project OpenSearch by opensearch-project.
the class SharedSignificantTermsTestMethods method checkSignificantTermsAggregationCorrect.
private static void checkSignificantTermsAggregationCorrect(OpenSearchIntegTestCase testCase) {
SearchResponse response = client().prepareSearch(INDEX_NAME).addAggregation(terms("class").field(CLASS_FIELD).subAggregation(significantTerms("sig_terms").field(TEXT_FIELD))).execute().actionGet();
assertSearchResponse(response);
StringTerms classes = response.getAggregations().get("class");
Assert.assertThat(classes.getBuckets().size(), equalTo(2));
for (Terms.Bucket classBucket : classes.getBuckets()) {
Map<String, Aggregation> aggs = classBucket.getAggregations().asMap();
Assert.assertTrue(aggs.containsKey("sig_terms"));
SignificantTerms agg = (SignificantTerms) aggs.get("sig_terms");
Assert.assertThat(agg.getBuckets().size(), equalTo(1));
SignificantTerms.Bucket sigBucket = agg.iterator().next();
String term = sigBucket.getKeyAsString();
String classTerm = classBucket.getKeyAsString();
Assert.assertTrue(term.equals(classTerm));
}
}
Aggregations