Search in sources :

Example 1 with InternalAggregation

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

the class InternalMatrixStatsTests method testReduceRandom.

@Override
public void testReduceRandom() {
    int numValues = 10000;
    int numShards = randomIntBetween(1, 20);
    int valuesPerShard = (int) Math.floor(numValues / numShards);
    List<Double> aValues = new ArrayList<>();
    List<Double> bValues = new ArrayList<>();
    RunningStats runningStats = new RunningStats();
    List<InternalAggregation> shardResults = new ArrayList<>();
    int valuePerShardCounter = 0;
    for (int i = 0; i < numValues; i++) {
        double valueA = randomDouble();
        aValues.add(valueA);
        double valueB = randomDouble();
        bValues.add(valueB);
        runningStats.add(new String[] { "a", "b" }, new double[] { valueA, valueB });
        if (++valuePerShardCounter == valuesPerShard) {
            shardResults.add(new InternalMatrixStats("_name", 1L, runningStats, null, Collections.emptyMap()));
            runningStats = new RunningStats();
            valuePerShardCounter = 0;
        }
    }
    if (valuePerShardCounter != 0) {
        shardResults.add(new InternalMatrixStats("_name", 1L, runningStats, null, Collections.emptyMap()));
    }
    MultiPassStats multiPassStats = new MultiPassStats("a", "b");
    multiPassStats.computeStats(aValues, bValues);
    ScriptService mockScriptService = mockScriptService();
    MockBigArrays bigArrays = new MockBigArrays(new MockPageCacheRecycler(Settings.EMPTY), new NoneCircuitBreakerService());
    InternalAggregation.ReduceContext context = InternalAggregation.ReduceContext.forFinalReduction(bigArrays, mockScriptService, b -> {
    }, PipelineTree.EMPTY);
    InternalMatrixStats reduced = (InternalMatrixStats) shardResults.get(0).reduce(shardResults, context);
    multiPassStats.assertNearlyEqual(reduced.getResults());
}
Also used : MockPageCacheRecycler(org.opensearch.common.util.MockPageCacheRecycler) ArrayList(java.util.ArrayList) MockBigArrays(org.opensearch.common.util.MockBigArrays) InternalAggregation(org.opensearch.search.aggregations.InternalAggregation) ScriptService(org.opensearch.script.ScriptService) NoneCircuitBreakerService(org.opensearch.indices.breaker.NoneCircuitBreakerService)

Example 2 with InternalAggregation

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

the class GlobalIT method testWithStatsSubAggregator.

public void testWithStatsSubAggregator() throws Exception {
    SearchResponse response = client().prepareSearch("idx").setQuery(QueryBuilders.termQuery("tag", "tag1")).addAggregation(global("global").subAggregation(stats("value_stats").field("value"))).get();
    assertSearchResponse(response);
    Global global = response.getAggregations().get("global");
    assertThat(global, notNullValue());
    assertThat(global.getName(), equalTo("global"));
    assertThat(global.getDocCount(), equalTo((long) numDocs));
    assertThat((long) ((InternalAggregation) global).getProperty("_count"), equalTo((long) numDocs));
    assertThat(global.getAggregations().asList().isEmpty(), is(false));
    Stats stats = global.getAggregations().get("value_stats");
    assertThat((Stats) ((InternalAggregation) global).getProperty("value_stats"), sameInstance(stats));
    assertThat(stats, notNullValue());
    assertThat(stats.getName(), equalTo("value_stats"));
    long sum = 0;
    for (int i = 0; i < numDocs; ++i) {
        sum += i + 1;
    }
    assertThat(stats.getAvg(), equalTo((double) sum / numDocs));
    assertThat(stats.getMin(), equalTo(1.0));
    assertThat(stats.getMax(), equalTo((double) numDocs));
    assertThat(stats.getCount(), equalTo((long) numDocs));
    assertThat(stats.getSum(), equalTo((double) sum));
}
Also used : InternalAggregation(org.opensearch.search.aggregations.InternalAggregation) Stats(org.opensearch.search.aggregations.metrics.Stats) Global(org.opensearch.search.aggregations.bucket.global.Global) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 3 with InternalAggregation

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

the class FilterIT method testWithSubAggregation.

public void testWithSubAggregation() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(filter("tag1", termQuery("tag", "tag1")).subAggregation(avg("avg_value").field("value"))).get();
    assertSearchResponse(response);
    Filter filter = response.getAggregations().get("tag1");
    assertThat(filter, notNullValue());
    assertThat(filter.getName(), equalTo("tag1"));
    assertThat(filter.getDocCount(), equalTo((long) numTag1Docs));
    assertThat((long) ((InternalAggregation) filter).getProperty("_count"), equalTo((long) numTag1Docs));
    long sum = 0;
    for (int i = 0; i < numTag1Docs; ++i) {
        sum += i + 1;
    }
    assertThat(filter.getAggregations().asList().isEmpty(), is(false));
    Avg avgValue = filter.getAggregations().get("avg_value");
    assertThat(avgValue, notNullValue());
    assertThat(avgValue.getName(), equalTo("avg_value"));
    assertThat(avgValue.getValue(), equalTo((double) sum / numTag1Docs));
    assertThat((double) ((InternalAggregation) filter).getProperty("avg_value.value"), equalTo((double) sum / numTag1Docs));
}
Also used : InternalAggregation(org.opensearch.search.aggregations.InternalAggregation) Avg(org.opensearch.search.aggregations.metrics.Avg) Filter(org.opensearch.search.aggregations.bucket.filter.Filter) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 4 with InternalAggregation

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

the class GeoBoundsIT method testSingleValuedField_getProperty.

public void testSingleValuedField_getProperty() throws Exception {
    SearchResponse searchResponse = client().prepareSearch(IDX_NAME).setQuery(matchAllQuery()).addAggregation(global("global").subAggregation(geoBounds(aggName).field(SINGLE_VALUED_FIELD_NAME).wrapLongitude(false))).get();
    assertSearchResponse(searchResponse);
    Global global = searchResponse.getAggregations().get("global");
    assertThat(global, notNullValue());
    assertThat(global.getName(), equalTo("global"));
    assertThat(global.getDocCount(), equalTo((long) numDocs));
    assertThat(global.getAggregations(), notNullValue());
    assertThat(global.getAggregations().asMap().size(), equalTo(1));
    GeoBounds geobounds = global.getAggregations().get(aggName);
    assertThat(geobounds, notNullValue());
    assertThat(geobounds.getName(), equalTo(aggName));
    assertThat((GeoBounds) ((InternalAggregation) global).getProperty(aggName), sameInstance(geobounds));
    GeoPoint topLeft = geobounds.topLeft();
    GeoPoint bottomRight = geobounds.bottomRight();
    assertThat(topLeft.lat(), closeTo(singleTopLeft.lat(), GEOHASH_TOLERANCE));
    assertThat(topLeft.lon(), closeTo(singleTopLeft.lon(), GEOHASH_TOLERANCE));
    assertThat(bottomRight.lat(), closeTo(singleBottomRight.lat(), GEOHASH_TOLERANCE));
    assertThat(bottomRight.lon(), closeTo(singleBottomRight.lon(), GEOHASH_TOLERANCE));
    assertThat((double) ((InternalAggregation) global).getProperty(aggName + ".top"), closeTo(singleTopLeft.lat(), GEOHASH_TOLERANCE));
    assertThat((double) ((InternalAggregation) global).getProperty(aggName + ".left"), closeTo(singleTopLeft.lon(), GEOHASH_TOLERANCE));
    assertThat((double) ((InternalAggregation) global).getProperty(aggName + ".bottom"), closeTo(singleBottomRight.lat(), GEOHASH_TOLERANCE));
    assertThat((double) ((InternalAggregation) global).getProperty(aggName + ".right"), closeTo(singleBottomRight.lon(), GEOHASH_TOLERANCE));
}
Also used : InternalAggregation(org.opensearch.search.aggregations.InternalAggregation) GeoPoint(org.opensearch.common.geo.GeoPoint) Global(org.opensearch.search.aggregations.bucket.global.Global) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 5 with InternalAggregation

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

the class CardinalityIT method testSingleValuedNumericGetProperty.

public void testSingleValuedNumericGetProperty() throws Exception {
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(global("global").subAggregation(cardinality("cardinality").precisionThreshold(precisionThreshold).field(singleNumericField()))).get();
    assertSearchResponse(searchResponse);
    Global global = searchResponse.getAggregations().get("global");
    assertThat(global, notNullValue());
    assertThat(global.getName(), equalTo("global"));
    // assertThat(global.getDocCount(), equalTo(numDocs));
    assertThat(global.getAggregations(), notNullValue());
    assertThat(global.getAggregations().asMap().size(), equalTo(1));
    Cardinality cardinality = global.getAggregations().get("cardinality");
    assertThat(cardinality, notNullValue());
    assertThat(cardinality.getName(), equalTo("cardinality"));
    long expectedValue = numDocs;
    assertCount(cardinality, expectedValue);
    assertThat(((InternalAggregation) global).getProperty("cardinality"), equalTo(cardinality));
    assertThat(((InternalAggregation) global).getProperty("cardinality.value"), equalTo((double) cardinality.getValue()));
    assertThat((double) ((InternalAggregation) cardinality).getProperty("value"), equalTo((double) cardinality.getValue()));
}
Also used : InternalAggregation(org.opensearch.search.aggregations.InternalAggregation) Global(org.opensearch.search.aggregations.bucket.global.Global) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Aggregations

InternalAggregation (org.opensearch.search.aggregations.InternalAggregation)75 ArrayList (java.util.ArrayList)40 List (java.util.List)17 SearchResponse (org.opensearch.action.search.SearchResponse)11 InternalAggregations (org.opensearch.search.aggregations.InternalAggregations)11 Global (org.opensearch.search.aggregations.bucket.global.Global)11 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)11 NoneCircuitBreakerService (org.opensearch.indices.breaker.NoneCircuitBreakerService)10 HashMap (java.util.HashMap)9 Map (java.util.Map)9 MockBigArrays (org.opensearch.common.util.MockBigArrays)9 MockPageCacheRecycler (org.opensearch.common.util.MockPageCacheRecycler)9 IOException (java.io.IOException)8 ScriptService (org.opensearch.script.ScriptService)8 DocValueFormat (org.opensearch.search.DocValueFormat)8 ReduceContext (org.opensearch.search.aggregations.InternalAggregation.ReduceContext)8 Collectors (java.util.stream.Collectors)7 MultiBucketConsumerService (org.opensearch.search.aggregations.MultiBucketConsumerService)7 IndexReader (org.apache.lucene.index.IndexReader)6 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)6