Search in sources :

Example 1 with InternalTopHits

use of org.opensearch.search.aggregations.metrics.InternalTopHits in project OpenSearch by opensearch-project.

the class TermsAggregatorTests method assertNestedTopHitsScore.

private void assertNestedTopHitsScore(InternalMultiBucketAggregation<?, ?> terms, boolean withScore) {
    assertThat(terms.getBuckets().size(), equalTo(9));
    int ptr = 9;
    for (MultiBucketsAggregation.Bucket bucket : terms.getBuckets()) {
        InternalTopHits topHits = bucket.getAggregations().get("top_hits");
        assertThat(topHits.getHits().getTotalHits().value, equalTo((long) ptr));
        assertEquals(TotalHits.Relation.EQUAL_TO, topHits.getHits().getTotalHits().relation);
        if (withScore) {
            assertThat(topHits.getHits().getMaxScore(), equalTo(1f));
        } else {
            assertThat(topHits.getHits().getMaxScore(), equalTo(Float.NaN));
        }
        --ptr;
    }
}
Also used : InternalTopHits(org.opensearch.search.aggregations.metrics.InternalTopHits) MultiBucketsAggregation(org.opensearch.search.aggregations.bucket.MultiBucketsAggregation) InetAddressPoint(org.apache.lucene.document.InetAddressPoint) GeoPoint(org.opensearch.common.geo.GeoPoint)

Example 2 with InternalTopHits

use of org.opensearch.search.aggregations.metrics.InternalTopHits in project OpenSearch by opensearch-project.

the class TermsAggregatorTests method testGlobalAggregationWithScore.

public void testGlobalAggregationWithScore() throws IOException {
    try (Directory directory = newDirectory()) {
        try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
            Document document = new Document();
            document.add(new SortedDocValuesField("keyword", new BytesRef("a")));
            indexWriter.addDocument(document);
            document = new Document();
            document.add(new SortedDocValuesField("keyword", new BytesRef("c")));
            indexWriter.addDocument(document);
            document = new Document();
            document.add(new SortedDocValuesField("keyword", new BytesRef("e")));
            indexWriter.addDocument(document);
            try (IndexReader indexReader = maybeWrapReaderEs(indexWriter.getReader())) {
                IndexSearcher indexSearcher = newIndexSearcher(indexReader);
                String executionHint = randomFrom(TermsAggregatorFactory.ExecutionMode.values()).toString();
                Aggregator.SubAggCollectionMode collectionMode = randomFrom(Aggregator.SubAggCollectionMode.values());
                GlobalAggregationBuilder globalBuilder = new GlobalAggregationBuilder("global").subAggregation(new TermsAggregationBuilder("terms").userValueTypeHint(ValueType.STRING).executionHint(executionHint).collectMode(collectionMode).field("keyword").order(BucketOrder.key(true)).subAggregation(new TermsAggregationBuilder("sub_terms").userValueTypeHint(ValueType.STRING).executionHint(executionHint).collectMode(collectionMode).field("keyword").order(BucketOrder.key(true)).subAggregation(new TopHitsAggregationBuilder("top_hits").storedField("_none_"))));
                MappedFieldType fieldType = new KeywordFieldMapper.KeywordFieldType("keyword");
                InternalGlobal result = searchAndReduce(indexSearcher, new MatchAllDocsQuery(), globalBuilder, fieldType);
                InternalMultiBucketAggregation<?, ?> terms = result.getAggregations().get("terms");
                assertThat(terms.getBuckets().size(), equalTo(3));
                for (MultiBucketsAggregation.Bucket bucket : terms.getBuckets()) {
                    InternalMultiBucketAggregation<?, ?> subTerms = bucket.getAggregations().get("sub_terms");
                    assertThat(subTerms.getBuckets().size(), equalTo(1));
                    MultiBucketsAggregation.Bucket subBucket = subTerms.getBuckets().get(0);
                    InternalTopHits topHits = subBucket.getAggregations().get("top_hits");
                    assertThat(topHits.getHits().getHits().length, equalTo(1));
                    for (SearchHit hit : topHits.getHits()) {
                        assertThat(hit.getScore(), greaterThan(0f));
                    }
                }
            }
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) InternalGlobal(org.opensearch.search.aggregations.bucket.global.InternalGlobal) SearchHit(org.opensearch.search.SearchHit) TopHitsAggregationBuilder(org.opensearch.search.aggregations.metrics.TopHitsAggregationBuilder) Aggregator(org.opensearch.search.aggregations.Aggregator) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) GlobalAggregationBuilder(org.opensearch.search.aggregations.bucket.global.GlobalAggregationBuilder) InternalTopHits(org.opensearch.search.aggregations.metrics.InternalTopHits) MultiBucketsAggregation(org.opensearch.search.aggregations.bucket.MultiBucketsAggregation) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) IndexReader(org.apache.lucene.index.IndexReader) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory)

Example 3 with InternalTopHits

use of org.opensearch.search.aggregations.metrics.InternalTopHits in project OpenSearch by opensearch-project.

the class RareTermsAggregatorTests method testWithNestedScoringAggregations.

public void testWithNestedScoringAggregations() throws IOException {
    try (Directory directory = newDirectory()) {
        try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
            for (int i = 0; i < 10; i++) {
                int[] nestedValues = new int[i];
                for (int j = 0; j < i; j++) {
                    nestedValues[j] = j;
                }
                indexWriter.addDocuments(generateDocsWithNested(Integer.toString(i), i, nestedValues));
            }
            indexWriter.commit();
            for (boolean withScore : new boolean[] { true, false }) {
                NestedAggregationBuilder nested = new NestedAggregationBuilder("nested", "nested_object").subAggregation(new RareTermsAggregationBuilder("terms").field("nested_value").maxDocCount(2).subAggregation(new TopHitsAggregationBuilder("top_hits").sort(withScore ? new ScoreSortBuilder() : new FieldSortBuilder("_doc")).storedField("_none_")));
                MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("nested_value", NumberFieldMapper.NumberType.LONG);
                try (IndexReader indexReader = wrapInMockESDirectoryReader(DirectoryReader.open(directory))) {
                    if (withScore) {
                        IllegalStateException e = expectThrows(IllegalStateException.class, () -> searchAndReduce(newIndexSearcher(indexReader), // match root document only
                        new DocValuesFieldExistsQuery(PRIMARY_TERM_NAME), nested, fieldType));
                        assertThat(e.getMessage(), equalTo("RareTerms agg [terms] is the child of the nested agg [nested], " + "and also has a scoring child agg [top_hits].  This combination is not supported because it requires " + "executing in [depth_first] mode, which the RareTerms agg cannot do."));
                    } else {
                        InternalNested result = searchAndReduce(newIndexSearcher(indexReader), // match root document only
                        new DocValuesFieldExistsQuery(PRIMARY_TERM_NAME), nested, fieldType);
                        InternalMultiBucketAggregation<?, ?> terms = result.getAggregations().get("terms");
                        assertThat(terms.getBuckets().size(), equalTo(2));
                        long counter = 1;
                        for (MultiBucketsAggregation.Bucket bucket : terms.getBuckets()) {
                            InternalTopHits topHits = bucket.getAggregations().get("top_hits");
                            TotalHits hits = topHits.getHits().getTotalHits();
                            assertNotNull(hits);
                            assertThat(hits.value, equalTo(counter));
                            assertThat(topHits.getHits().getMaxScore(), equalTo(Float.NaN));
                            counter += 1;
                        }
                    }
                }
            }
        }
    }
}
Also used : TotalHits(org.apache.lucene.search.TotalHits) TopHitsAggregationBuilder(org.opensearch.search.aggregations.metrics.TopHitsAggregationBuilder) InternalNested(org.opensearch.search.aggregations.bucket.nested.InternalNested) FieldSortBuilder(org.opensearch.search.sort.FieldSortBuilder) DocValuesFieldExistsQuery(org.apache.lucene.search.DocValuesFieldExistsQuery) LongPoint(org.apache.lucene.document.LongPoint) InternalTopHits(org.opensearch.search.aggregations.metrics.InternalTopHits) ScoreSortBuilder(org.opensearch.search.sort.ScoreSortBuilder) MultiBucketsAggregation(org.opensearch.search.aggregations.bucket.MultiBucketsAggregation) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) IndexReader(org.apache.lucene.index.IndexReader) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory) NestedAggregationBuilder(org.opensearch.search.aggregations.bucket.nested.NestedAggregationBuilder)

Example 4 with InternalTopHits

use of org.opensearch.search.aggregations.metrics.InternalTopHits in project OpenSearch by opensearch-project.

the class RareTermsAggregatorTests method testGlobalAggregationWithScore.

public void testGlobalAggregationWithScore() throws IOException {
    try (Directory directory = newDirectory()) {
        try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
            Document document = new Document();
            document.add(new SortedDocValuesField("keyword", new BytesRef("a")));
            indexWriter.addDocument(document);
            document = new Document();
            document.add(new SortedDocValuesField("keyword", new BytesRef("c")));
            indexWriter.addDocument(document);
            document = new Document();
            document.add(new SortedDocValuesField("keyword", new BytesRef("e")));
            indexWriter.addDocument(document);
            try (IndexReader indexReader = maybeWrapReaderEs(indexWriter.getReader())) {
                IndexSearcher indexSearcher = newIndexSearcher(indexReader);
                GlobalAggregationBuilder globalBuilder = new GlobalAggregationBuilder("global").subAggregation(new RareTermsAggregationBuilder("terms").field("keyword").subAggregation(new RareTermsAggregationBuilder("sub_terms").field("keyword").subAggregation(new TopHitsAggregationBuilder("top_hits").storedField("_none_"))));
                MappedFieldType fieldType = new KeywordFieldMapper.KeywordFieldType("keyword");
                InternalGlobal result = searchAndReduce(indexSearcher, new MatchAllDocsQuery(), globalBuilder, fieldType);
                InternalMultiBucketAggregation<?, ?> terms = result.getAggregations().get("terms");
                assertThat(terms.getBuckets().size(), equalTo(3));
                for (MultiBucketsAggregation.Bucket bucket : terms.getBuckets()) {
                    InternalMultiBucketAggregation<?, ?> subTerms = bucket.getAggregations().get("sub_terms");
                    assertThat(subTerms.getBuckets().size(), equalTo(1));
                    MultiBucketsAggregation.Bucket subBucket = subTerms.getBuckets().get(0);
                    InternalTopHits topHits = subBucket.getAggregations().get("top_hits");
                    assertThat(topHits.getHits().getHits().length, equalTo(1));
                    for (SearchHit hit : topHits.getHits()) {
                        assertThat(hit.getScore(), greaterThan(0f));
                    }
                }
            }
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) InternalGlobal(org.opensearch.search.aggregations.bucket.global.InternalGlobal) SearchHit(org.opensearch.search.SearchHit) TopHitsAggregationBuilder(org.opensearch.search.aggregations.metrics.TopHitsAggregationBuilder) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) GlobalAggregationBuilder(org.opensearch.search.aggregations.bucket.global.GlobalAggregationBuilder) InternalTopHits(org.opensearch.search.aggregations.metrics.InternalTopHits) MultiBucketsAggregation(org.opensearch.search.aggregations.bucket.MultiBucketsAggregation) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) IndexReader(org.apache.lucene.index.IndexReader) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory)

Aggregations

MultiBucketsAggregation (org.opensearch.search.aggregations.bucket.MultiBucketsAggregation)4 InternalTopHits (org.opensearch.search.aggregations.metrics.InternalTopHits)4 IndexReader (org.apache.lucene.index.IndexReader)3 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)3 Directory (org.apache.lucene.store.Directory)3 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)3 TopHitsAggregationBuilder (org.opensearch.search.aggregations.metrics.TopHitsAggregationBuilder)3 Document (org.apache.lucene.document.Document)2 SortedDocValuesField (org.apache.lucene.document.SortedDocValuesField)2 IndexSearcher (org.apache.lucene.search.IndexSearcher)2 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)2 BytesRef (org.apache.lucene.util.BytesRef)2 SearchHit (org.opensearch.search.SearchHit)2 GlobalAggregationBuilder (org.opensearch.search.aggregations.bucket.global.GlobalAggregationBuilder)2 InternalGlobal (org.opensearch.search.aggregations.bucket.global.InternalGlobal)2 InetAddressPoint (org.apache.lucene.document.InetAddressPoint)1 LongPoint (org.apache.lucene.document.LongPoint)1 DocValuesFieldExistsQuery (org.apache.lucene.search.DocValuesFieldExistsQuery)1 TotalHits (org.apache.lucene.search.TotalHits)1 GeoPoint (org.opensearch.common.geo.GeoPoint)1