Search in sources :

Example 1 with TopHitsAggregationBuilder

use of org.opensearch.search.aggregations.metrics.TopHitsAggregationBuilder in project fess by codelibs.

the class EsAbstractConditionAggregation method regTopHitsA.

protected TopHitsAggregationBuilder regTopHitsA(String name) {
    TopHitsAggregationBuilder builder = AggregationBuilders.topHits(name);
    regA(builder);
    return builder;
}
Also used : TopHitsAggregationBuilder(org.opensearch.search.aggregations.metrics.TopHitsAggregationBuilder)

Example 2 with TopHitsAggregationBuilder

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

the class CompositeAggregatorTests method testWithKeywordAndTopHits.

public void testWithKeywordAndTopHits() throws Exception {
    final List<Map<String, List<Object>>> dataset = new ArrayList<>();
    dataset.addAll(Arrays.asList(createDocument("keyword", "a"), createDocument("keyword", "c"), createDocument("keyword", "a"), createDocument("keyword", "d"), createDocument("keyword", "c")));
    testSearchCase(Arrays.asList(new MatchAllDocsQuery(), new DocValuesFieldExistsQuery("keyword")), dataset, () -> {
        TermsValuesSourceBuilder terms = new TermsValuesSourceBuilder("keyword").field("keyword");
        return new CompositeAggregationBuilder("name", Collections.singletonList(terms)).subAggregation(new TopHitsAggregationBuilder("top_hits").storedField("_none_"));
    }, (result) -> {
        assertEquals(3, result.getBuckets().size());
        assertEquals("{keyword=a}", result.getBuckets().get(0).getKeyAsString());
        assertEquals(2L, result.getBuckets().get(0).getDocCount());
        TopHits topHits = result.getBuckets().get(0).getAggregations().get("top_hits");
        assertNotNull(topHits);
        assertEquals(topHits.getHits().getHits().length, 2);
        assertEquals(topHits.getHits().getTotalHits().value, 2L);
        assertEquals("{keyword=c}", result.getBuckets().get(1).getKeyAsString());
        assertEquals(2L, result.getBuckets().get(1).getDocCount());
        topHits = result.getBuckets().get(1).getAggregations().get("top_hits");
        assertNotNull(topHits);
        assertEquals(topHits.getHits().getHits().length, 2);
        assertEquals(topHits.getHits().getTotalHits().value, 2L);
        assertEquals("{keyword=d}", result.getBuckets().get(2).getKeyAsString());
        assertEquals(1L, result.getBuckets().get(2).getDocCount());
        topHits = result.getBuckets().get(2).getAggregations().get("top_hits");
        assertNotNull(topHits);
        assertEquals(topHits.getHits().getHits().length, 1);
        assertEquals(topHits.getHits().getTotalHits().value, 1L);
    });
    testSearchCase(Arrays.asList(new MatchAllDocsQuery(), new DocValuesFieldExistsQuery("keyword")), dataset, () -> {
        TermsValuesSourceBuilder terms = new TermsValuesSourceBuilder("keyword").field("keyword");
        return new CompositeAggregationBuilder("name", Collections.singletonList(terms)).aggregateAfter(Collections.singletonMap("keyword", "a")).subAggregation(new TopHitsAggregationBuilder("top_hits").storedField("_none_"));
    }, (result) -> {
        assertEquals(2, result.getBuckets().size());
        assertEquals("{keyword=c}", result.getBuckets().get(0).getKeyAsString());
        assertEquals(2L, result.getBuckets().get(0).getDocCount());
        TopHits topHits = result.getBuckets().get(0).getAggregations().get("top_hits");
        assertNotNull(topHits);
        assertEquals(topHits.getHits().getHits().length, 2);
        assertEquals(topHits.getHits().getTotalHits().value, 2L);
        assertEquals("{keyword=d}", result.getBuckets().get(1).getKeyAsString());
        assertEquals(1L, result.getBuckets().get(1).getDocCount());
        topHits = result.getBuckets().get(1).getAggregations().get("top_hits");
        assertNotNull(topHits);
        assertEquals(topHits.getHits().getHits().length, 1);
        assertEquals(topHits.getHits().getTotalHits().value, 1L);
    });
}
Also used : TopHits(org.opensearch.search.aggregations.metrics.TopHits) TopHitsAggregationBuilder(org.opensearch.search.aggregations.metrics.TopHitsAggregationBuilder) ArrayList(java.util.ArrayList) DocValuesFieldExistsQuery(org.apache.lucene.search.DocValuesFieldExistsQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with TopHitsAggregationBuilder

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

the class TermsAggregatorTests method testWithNestedAggregations.

public void testWithNestedAggregations() 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 (Aggregator.SubAggCollectionMode mode : Aggregator.SubAggCollectionMode.values()) {
                for (boolean withScore : new boolean[] { true, false }) {
                    NestedAggregationBuilder nested = new NestedAggregationBuilder("nested", "nested_object").subAggregation(new TermsAggregationBuilder("terms").userValueTypeHint(ValueType.LONG).field("nested_value").collectMode(mode).order(BucketOrder.key(true)).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))) {
                        {
                            InternalNested result = searchAndReduce(newSearcher(indexReader, false, true), // match root document only
                            new DocValuesFieldExistsQuery(PRIMARY_TERM_NAME), nested, fieldType);
                            InternalMultiBucketAggregation<?, ?> terms = result.getAggregations().get("terms");
                            assertNestedTopHitsScore(terms, withScore);
                        }
                        {
                            FilterAggregationBuilder filter = new FilterAggregationBuilder("filter", new MatchAllQueryBuilder()).subAggregation(nested);
                            InternalFilter result = searchAndReduce(newSearcher(indexReader, false, true), // match root document only
                            new DocValuesFieldExistsQuery(PRIMARY_TERM_NAME), filter, fieldType);
                            InternalNested nestedResult = result.getAggregations().get("nested");
                            InternalMultiBucketAggregation<?, ?> terms = nestedResult.getAggregations().get("terms");
                            assertNestedTopHitsScore(terms, withScore);
                        }
                    }
                }
            }
        }
    }
}
Also used : FilterAggregationBuilder(org.opensearch.search.aggregations.bucket.filter.FilterAggregationBuilder) TopHitsAggregationBuilder(org.opensearch.search.aggregations.metrics.TopHitsAggregationBuilder) InternalNested(org.opensearch.search.aggregations.bucket.nested.InternalNested) Aggregator(org.opensearch.search.aggregations.Aggregator) FieldSortBuilder(org.opensearch.search.sort.FieldSortBuilder) DocValuesFieldExistsQuery(org.apache.lucene.search.DocValuesFieldExistsQuery) InetAddressPoint(org.apache.lucene.document.InetAddressPoint) GeoPoint(org.opensearch.common.geo.GeoPoint) ScoreSortBuilder(org.opensearch.search.sort.ScoreSortBuilder) InternalFilter(org.opensearch.search.aggregations.bucket.filter.InternalFilter) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) IndexReader(org.apache.lucene.index.IndexReader) InternalMultiBucketAggregation(org.opensearch.search.aggregations.InternalMultiBucketAggregation) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory) NestedAggregationBuilder(org.opensearch.search.aggregations.bucket.nested.NestedAggregationBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder)

Example 4 with TopHitsAggregationBuilder

use of org.opensearch.search.aggregations.metrics.TopHitsAggregationBuilder in project fess by codelibs.

the class EsAbstractConditionAggregation method regTopHitsA.

protected TopHitsAggregationBuilder regTopHitsA(String name) {
    TopHitsAggregationBuilder builder = AggregationBuilders.topHits(name);
    regA(builder);
    return builder;
}
Also used : TopHitsAggregationBuilder(org.opensearch.search.aggregations.metrics.TopHitsAggregationBuilder)

Example 5 with TopHitsAggregationBuilder

use of org.opensearch.search.aggregations.metrics.TopHitsAggregationBuilder in project fess by codelibs.

the class EsAbstractConditionAggregation method regTopHitsA.

protected TopHitsAggregationBuilder regTopHitsA(String name) {
    TopHitsAggregationBuilder builder = AggregationBuilders.topHits(name);
    regA(builder);
    return builder;
}
Also used : TopHitsAggregationBuilder(org.opensearch.search.aggregations.metrics.TopHitsAggregationBuilder)

Aggregations

TopHitsAggregationBuilder (org.opensearch.search.aggregations.metrics.TopHitsAggregationBuilder)11 IndexReader (org.apache.lucene.index.IndexReader)4 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)4 DocValuesFieldExistsQuery (org.apache.lucene.search.DocValuesFieldExistsQuery)4 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)4 Directory (org.apache.lucene.store.Directory)4 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)4 MultiBucketsAggregation (org.opensearch.search.aggregations.bucket.MultiBucketsAggregation)3 GlobalAggregationBuilder (org.opensearch.search.aggregations.bucket.global.GlobalAggregationBuilder)3 NestedAggregationBuilder (org.opensearch.search.aggregations.bucket.nested.NestedAggregationBuilder)3 InternalTopHits (org.opensearch.search.aggregations.metrics.InternalTopHits)3 FieldSortBuilder (org.opensearch.search.sort.FieldSortBuilder)3 ScoreSortBuilder (org.opensearch.search.sort.ScoreSortBuilder)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Document (org.apache.lucene.document.Document)2 InetAddressPoint (org.apache.lucene.document.InetAddressPoint)2 LongPoint (org.apache.lucene.document.LongPoint)2 SortedDocValuesField (org.apache.lucene.document.SortedDocValuesField)2