Search in sources :

Example 16 with InternalAggregations

use of org.elasticsearch.search.aggregations.InternalAggregations in project elasticsearch by elastic.

the class SearchPhaseControllerTests method testConsumer.

public void testConsumer() {
    int bufferSize = randomIntBetween(2, 3);
    SearchRequest request = new SearchRequest();
    request.source(new SearchSourceBuilder().aggregation(AggregationBuilders.avg("foo")));
    request.setBatchedReduceSize(bufferSize);
    InitialSearchPhase.SearchPhaseResults<QuerySearchResultProvider> consumer = searchPhaseController.newSearchPhaseResults(request, 3);
    QuerySearchResult result = new QuerySearchResult(0, new SearchShardTarget("node", new Index("a", "b"), 0));
    result.topDocs(new TopDocs(0, new ScoreDoc[0], 0.0F), new DocValueFormat[0]);
    InternalAggregations aggs = new InternalAggregations(Arrays.asList(new InternalMax("test", 1.0D, DocValueFormat.RAW, Collections.emptyList(), Collections.emptyMap())));
    result.aggregations(aggs);
    consumer.consumeResult(0, result);
    result = new QuerySearchResult(1, new SearchShardTarget("node", new Index("a", "b"), 0));
    result.topDocs(new TopDocs(0, new ScoreDoc[0], 0.0F), new DocValueFormat[0]);
    aggs = new InternalAggregations(Arrays.asList(new InternalMax("test", 3.0D, DocValueFormat.RAW, Collections.emptyList(), Collections.emptyMap())));
    result.aggregations(aggs);
    consumer.consumeResult(2, result);
    result = new QuerySearchResult(1, new SearchShardTarget("node", new Index("a", "b"), 0));
    result.topDocs(new TopDocs(0, new ScoreDoc[0], 0.0F), new DocValueFormat[0]);
    aggs = new InternalAggregations(Arrays.asList(new InternalMax("test", 2.0D, DocValueFormat.RAW, Collections.emptyList(), Collections.emptyMap())));
    result.aggregations(aggs);
    consumer.consumeResult(1, result);
    int numTotalReducePhases = 1;
    if (bufferSize == 2) {
        assertThat(consumer, instanceOf(SearchPhaseController.QueryPhaseResultConsumer.class));
        assertEquals(1, ((SearchPhaseController.QueryPhaseResultConsumer) consumer).getNumReducePhases());
        assertEquals(2, ((SearchPhaseController.QueryPhaseResultConsumer) consumer).getNumBuffered());
        numTotalReducePhases++;
    } else {
        assertThat(consumer, not(instanceOf(SearchPhaseController.QueryPhaseResultConsumer.class)));
    }
    SearchPhaseController.ReducedQueryPhase reduce = consumer.reduce();
    assertEquals(numTotalReducePhases, reduce.numReducePhases);
    InternalMax max = (InternalMax) reduce.aggregations.asList().get(0);
    assertEquals(3.0D, max.getValue(), 0.0D);
}
Also used : QuerySearchResultProvider(org.elasticsearch.search.query.QuerySearchResultProvider) InternalMax(org.elasticsearch.search.aggregations.metrics.max.InternalMax) Index(org.elasticsearch.index.Index) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) InternalAggregations(org.elasticsearch.search.aggregations.InternalAggregations) QuerySearchResult(org.elasticsearch.search.query.QuerySearchResult) SearchShardTarget(org.elasticsearch.search.SearchShardTarget)

Aggregations

InternalAggregations (org.elasticsearch.search.aggregations.InternalAggregations)16 ArrayList (java.util.ArrayList)12 InternalAggregation (org.elasticsearch.search.aggregations.InternalAggregation)9 List (java.util.List)8 ReduceContext (org.elasticsearch.search.aggregations.InternalAggregation.ReduceContext)8 IOException (java.io.IOException)7 Map (java.util.Map)7 Collectors (java.util.stream.Collectors)7 StreamSupport (java.util.stream.StreamSupport)7 StreamInput (org.elasticsearch.common.io.stream.StreamInput)6 Bucket (org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket)6 StreamOutput (org.elasticsearch.common.io.stream.StreamOutput)4 DocValueFormat (org.elasticsearch.search.DocValueFormat)4 GapPolicy (org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy)4 BucketHelpers.resolveBucketValue (org.elasticsearch.search.aggregations.pipeline.BucketHelpers.resolveBucketValue)4 PipelineAggregator (org.elasticsearch.search.aggregations.pipeline.PipelineAggregator)4 QuerySearchResult (org.elasticsearch.search.query.QuerySearchResult)4 ScoreDoc (org.apache.lucene.search.ScoreDoc)3 TopDocs (org.apache.lucene.search.TopDocs)3 MultiBucketsAggregation (org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation)3