Search in sources :

Example 21 with Aggregation

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);
        }
    }
}
Also used : Aggregation(org.opensearch.search.aggregations.Aggregation) InternalMultiBucketAggregation(org.opensearch.search.aggregations.InternalMultiBucketAggregation) MultiBucketsAggregation(org.opensearch.search.aggregations.bucket.MultiBucketsAggregation) ParsedMultiBucketAggregation(org.opensearch.search.aggregations.ParsedMultiBucketAggregation) ParsedAggregation(org.opensearch.search.aggregations.ParsedAggregation) InternalAggregation(org.opensearch.search.aggregations.InternalAggregation) Aggregations(org.opensearch.search.aggregations.Aggregations) InternalAggregations(org.opensearch.search.aggregations.InternalAggregations) ParsedMultiBucketAggregation(org.opensearch.search.aggregations.ParsedMultiBucketAggregation) InternalMultiBucketAggregation(org.opensearch.search.aggregations.InternalMultiBucketAggregation)

Example 22 with Aggregation

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());
        });
    }
}
Also used : Query(org.apache.lucene.search.Query) Arrays(java.util.Arrays) Aggregation(org.opensearch.search.aggregations.Aggregation) Matchers.either(org.hamcrest.Matchers.either) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) IndexableField(org.apache.lucene.index.IndexableField) ZonedDateTime(java.time.ZonedDateTime) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) Version(org.opensearch.Version) Document(org.apache.lucene.document.Document) AggregationBuilder(org.opensearch.search.aggregations.AggregationBuilder) InternalMax(org.opensearch.search.aggregations.metrics.InternalMax) Locale(java.util.Locale) Directory(org.apache.lucene.store.Directory) Map(java.util.Map) ZoneOffset(java.time.ZoneOffset) AggregationInspectionHelper(org.opensearch.search.aggregations.support.AggregationInspectionHelper) SortedSetDocValuesField(org.apache.lucene.document.SortedSetDocValuesField) BytesRef(org.apache.lucene.util.BytesRef) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) DirectoryReader(org.apache.lucene.index.DirectoryReader) DerivativePipelineAggregationBuilder(org.opensearch.search.aggregations.pipeline.DerivativePipelineAggregationBuilder) Settings(org.opensearch.common.settings.Settings) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Instant(java.time.Instant) List(java.util.List) RandomIndexWriter(org.apache.lucene.tests.index.RandomIndexWriter) LocalDate(java.time.LocalDate) Matchers.equalTo(org.hamcrest.Matchers.equalTo) IndexSettings(org.opensearch.index.IndexSettings) InternalStats(org.opensearch.search.aggregations.metrics.InternalStats) MultiBucketConsumerService(org.opensearch.search.aggregations.MultiBucketConsumerService) IndexReader(org.apache.lucene.index.IndexReader) IndexSearcher(org.apache.lucene.search.IndexSearcher) InternalSimpleValue(org.opensearch.search.aggregations.pipeline.InternalSimpleValue) LongPoint(org.apache.lucene.document.LongPoint) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) StringTerms(org.opensearch.search.aggregations.bucket.terms.StringTerms) CheckedBiConsumer(org.opensearch.common.CheckedBiConsumer) HashMap(java.util.HashMap) NumberFieldMapper(org.opensearch.index.mapper.NumberFieldMapper) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Matchers.hasSize(org.hamcrest.Matchers.hasSize) Matchers.hasEntry(org.hamcrest.Matchers.hasEntry) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) MaxAggregationBuilder(org.opensearch.search.aggregations.metrics.MaxAggregationBuilder) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) AggregationBuilders(org.opensearch.search.aggregations.AggregationBuilders) TreeMap(java.util.TreeMap) DateFieldMapper(org.opensearch.index.mapper.DateFieldMapper) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) YearMonth(java.time.YearMonth) Collections(java.util.Collections) ZonedDateTime(java.time.ZonedDateTime) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) LongPoint(org.apache.lucene.document.LongPoint)

Example 23 with Aggregation

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);
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Aggregations(org.opensearch.search.aggregations.Aggregations) ArrayList(java.util.ArrayList) Document(org.apache.lucene.document.Document) Aggregation(org.opensearch.search.aggregations.Aggregation) InternalAggregation(org.opensearch.search.aggregations.InternalAggregation) InternalAvg(org.opensearch.search.aggregations.metrics.InternalAvg) InternalDateHistogram(org.opensearch.search.aggregations.bucket.histogram.InternalDateHistogram) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) AvgAggregationBuilder(org.opensearch.search.aggregations.metrics.AvgAggregationBuilder) Directory(org.apache.lucene.store.Directory) DateFieldMapper(org.opensearch.index.mapper.DateFieldMapper) DateHistogramAggregationBuilder(org.opensearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) InternalAggregation(org.opensearch.search.aggregations.InternalAggregation) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) IndexReader(org.apache.lucene.index.IndexReader) RandomIndexWriter(org.apache.lucene.tests.index.RandomIndexWriter)

Example 24 with Aggregation

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)));
}
Also used : Aggregation(org.opensearch.search.aggregations.Aggregation) KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) SearchHits(org.opensearch.search.SearchHits) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Example 25 with Aggregation

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));
    }
}
Also used : Aggregation(org.opensearch.search.aggregations.Aggregation) SignificantTerms(org.opensearch.search.aggregations.bucket.terms.SignificantTerms) StringTerms(org.opensearch.search.aggregations.bucket.terms.StringTerms) AggregationBuilders.significantTerms(org.opensearch.search.aggregations.AggregationBuilders.significantTerms) StringTerms(org.opensearch.search.aggregations.bucket.terms.StringTerms) SignificantTerms(org.opensearch.search.aggregations.bucket.terms.SignificantTerms) Terms(org.opensearch.search.aggregations.bucket.terms.Terms) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Aggregations

Aggregation (org.opensearch.search.aggregations.Aggregation)54 SearchResponse (org.opensearch.action.search.SearchResponse)31 HashMap (java.util.HashMap)30 List (java.util.List)30 ArrayList (java.util.ArrayList)29 Map (java.util.Map)20 InternalAggregation (org.opensearch.search.aggregations.InternalAggregation)19 Aggregations (org.opensearch.search.aggregations.Aggregations)18 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)16 Matchers.containsString (org.hamcrest.Matchers.containsString)14 IOException (java.io.IOException)13 Collections (java.util.Collections)13 ActionListener (org.opensearch.action.ActionListener)13 NamedXContentRegistry (org.opensearch.common.xcontent.NamedXContentRegistry)13 MultiBucketsAggregation (org.opensearch.search.aggregations.bucket.MultiBucketsAggregation)13 Terms (org.opensearch.search.aggregations.bucket.terms.Terms)13 Collectors (java.util.stream.Collectors)12 Script (org.opensearch.script.Script)12 Arrays (java.util.Arrays)11 SearchRequest (org.opensearch.action.search.SearchRequest)11