Search in sources :

Example 1 with InternalMax

use of org.elasticsearch.search.aggregations.metrics.max.InternalMax in project elasticsearch by elastic.

the class InternalSingleBucketAggregationTestCase method createTestInstance.

@Override
protected final T createTestInstance(String name, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) {
    List<InternalAggregation> internal = new ArrayList<>();
    if (hasInternalMax) {
        internal.add(new InternalMax("max", randomDouble(), randomFrom(DocValueFormat.BOOLEAN, DocValueFormat.GEOHASH, DocValueFormat.IP, DocValueFormat.RAW), emptyList(), emptyMap()));
    }
    if (hasInternalMin) {
        internal.add(new InternalMin("min", randomDouble(), randomFrom(DocValueFormat.BOOLEAN, DocValueFormat.GEOHASH, DocValueFormat.IP, DocValueFormat.RAW), emptyList(), emptyMap()));
    }
    // we shouldn't use the full long range here since we sum doc count on reduce, and don't want to overflow the long range there
    long docCount = between(0, Integer.MAX_VALUE);
    return createTestInstance(name, docCount, new InternalAggregations(internal), pipelineAggregators, metaData);
}
Also used : InternalAggregation(org.elasticsearch.search.aggregations.InternalAggregation) InternalAggregations(org.elasticsearch.search.aggregations.InternalAggregations) InternalMax(org.elasticsearch.search.aggregations.metrics.max.InternalMax) InternalMin(org.elasticsearch.search.aggregations.metrics.min.InternalMin) ArrayList(java.util.ArrayList)

Example 2 with InternalMax

use of org.elasticsearch.search.aggregations.metrics.max.InternalMax in project elasticsearch by elastic.

the class SearchPhaseControllerTests method testConsumerConcurrently.

public void testConsumerConcurrently() throws InterruptedException {
    int expectedNumResults = randomIntBetween(1, 100);
    int bufferSize = randomIntBetween(2, 200);
    SearchRequest request = new SearchRequest();
    request.source(new SearchSourceBuilder().aggregation(AggregationBuilders.avg("foo")));
    request.setBatchedReduceSize(bufferSize);
    InitialSearchPhase.SearchPhaseResults<QuerySearchResultProvider> consumer = searchPhaseController.newSearchPhaseResults(request, expectedNumResults);
    AtomicInteger max = new AtomicInteger();
    CountDownLatch latch = new CountDownLatch(expectedNumResults);
    for (int i = 0; i < expectedNumResults; i++) {
        int id = i;
        Thread t = new Thread(() -> {
            int number = randomIntBetween(1, 1000);
            max.updateAndGet(prev -> Math.max(prev, number));
            QuerySearchResult result = new QuerySearchResult(id, new SearchShardTarget("node", new Index("a", "b"), id));
            result.topDocs(new TopDocs(id, new ScoreDoc[0], 0.0F), new DocValueFormat[0]);
            InternalAggregations aggs = new InternalAggregations(Arrays.asList(new InternalMax("test", (double) number, DocValueFormat.RAW, Collections.emptyList(), Collections.emptyMap())));
            result.aggregations(aggs);
            consumer.consumeResult(id, result);
            latch.countDown();
        });
        t.start();
    }
    latch.await();
    SearchPhaseController.ReducedQueryPhase reduce = consumer.reduce();
    InternalMax internalMax = (InternalMax) reduce.aggregations.asList().get(0);
    assertEquals(max.get(), internalMax.getValue(), 0.0D);
}
Also used : QuerySearchResultProvider(org.elasticsearch.search.query.QuerySearchResultProvider) InternalMax(org.elasticsearch.search.aggregations.metrics.max.InternalMax) Index(org.elasticsearch.index.Index) CountDownLatch(java.util.concurrent.CountDownLatch) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) InternalAggregations(org.elasticsearch.search.aggregations.InternalAggregations) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) QuerySearchResult(org.elasticsearch.search.query.QuerySearchResult) SearchShardTarget(org.elasticsearch.search.SearchShardTarget)

Example 3 with InternalMax

use of org.elasticsearch.search.aggregations.metrics.max.InternalMax 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)

Example 4 with InternalMax

use of org.elasticsearch.search.aggregations.metrics.max.InternalMax in project elasticsearch by elastic.

the class InternalSingleBucketAggregationTestCase method assertReduced.

@Override
protected final void assertReduced(T reduced, List<T> inputs) {
    assertEquals(inputs.stream().mapToLong(InternalSingleBucketAggregation::getDocCount).sum(), reduced.getDocCount());
    if (hasInternalMax) {
        double expected = inputs.stream().mapToDouble(i -> {
            InternalMax max = i.getAggregations().get("max");
            return max.getValue();
        }).max().getAsDouble();
        InternalMax reducedMax = reduced.getAggregations().get("max");
        assertEquals(expected, reducedMax.getValue(), 0);
    }
    if (hasInternalMin) {
        double expected = inputs.stream().mapToDouble(i -> {
            InternalMin min = i.getAggregations().get("min");
            return min.getValue();
        }).min().getAsDouble();
        InternalMin reducedMin = reduced.getAggregations().get("min");
        assertEquals(expected, reducedMin.getValue(), 0);
    }
    extraAssertReduced(reduced, inputs);
}
Also used : InternalMax(org.elasticsearch.search.aggregations.metrics.max.InternalMax) InternalMin(org.elasticsearch.search.aggregations.metrics.min.InternalMin)

Aggregations

InternalMax (org.elasticsearch.search.aggregations.metrics.max.InternalMax)4 InternalAggregations (org.elasticsearch.search.aggregations.InternalAggregations)3 ScoreDoc (org.apache.lucene.search.ScoreDoc)2 TopDocs (org.apache.lucene.search.TopDocs)2 Index (org.elasticsearch.index.Index)2 SearchShardTarget (org.elasticsearch.search.SearchShardTarget)2 InternalMin (org.elasticsearch.search.aggregations.metrics.min.InternalMin)2 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)2 QuerySearchResult (org.elasticsearch.search.query.QuerySearchResult)2 QuerySearchResultProvider (org.elasticsearch.search.query.QuerySearchResultProvider)2 ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 InternalAggregation (org.elasticsearch.search.aggregations.InternalAggregation)1