Search in sources :

Example 1 with Global

use of org.opensearch.search.aggregations.bucket.global.Global 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 2 with Global

use of org.opensearch.search.aggregations.bucket.global.Global in project OpenSearch by opensearch-project.

the class TransportTwoNodesSearchIT method testSimpleFacets.

public void testSimpleFacets() throws Exception {
    prepareData();
    SearchSourceBuilder sourceBuilder = searchSource().query(termQuery("multi", "test")).from(0).size(20).explain(true).aggregation(AggregationBuilders.global("global").subAggregation(AggregationBuilders.filter("all", termQuery("multi", "test")))).aggregation(AggregationBuilders.filter("test1", termQuery("name", "test1")));
    SearchResponse searchResponse = client().search(searchRequest("test").source(sourceBuilder)).actionGet();
    assertNoFailures(searchResponse);
    assertThat(searchResponse.getHits().getTotalHits().value, equalTo(100L));
    Global global = searchResponse.getAggregations().get("global");
    Filter all = global.getAggregations().get("all");
    Filter test1 = searchResponse.getAggregations().get("test1");
    assertThat(test1.getDocCount(), equalTo(1L));
    assertThat(all.getDocCount(), equalTo(100L));
}
Also used : Filter(org.opensearch.search.aggregations.bucket.filter.Filter) Global(org.opensearch.search.aggregations.bucket.global.Global) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) SearchResponse(org.opensearch.action.search.SearchResponse) MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse)

Example 3 with Global

use of org.opensearch.search.aggregations.bucket.global.Global in project OpenSearch by opensearch-project.

the class ShardReduceIT method testGlobal.

public void testGlobal() throws Exception {
    SearchResponse response = client().prepareSearch("idx").setQuery(QueryBuilders.matchAllQuery()).addAggregation(global("global").subAggregation(dateHistogram("histo").field("date").dateHistogramInterval(DateHistogramInterval.DAY).minDocCount(0))).get();
    assertSearchResponse(response);
    Global global = response.getAggregations().get("global");
    Histogram histo = global.getAggregations().get("histo");
    assertThat(histo.getBuckets().size(), equalTo(4));
}
Also used : AggregationBuilders.dateHistogram(org.opensearch.search.aggregations.AggregationBuilders.dateHistogram) Histogram(org.opensearch.search.aggregations.bucket.histogram.Histogram) Global(org.opensearch.search.aggregations.bucket.global.Global) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 4 with Global

use of org.opensearch.search.aggregations.bucket.global.Global 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 Global

use of org.opensearch.search.aggregations.bucket.global.Global in project OpenSearch by opensearch-project.

the class HDRPercentileRanksIT method testSingleValuedFieldGetProperty.

@Override
public void testSingleValuedFieldGetProperty() throws Exception {
    int sigDigits = randomSignificantDigits();
    final double[] pcts = randomPercents(minValue, maxValue);
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(global("global").subAggregation(percentileRanks("percentile_ranks", pcts).method(PercentilesMethod.HDR).numberOfSignificantValueDigits(sigDigits).field("value"))).get();
    assertHitCount(searchResponse, 10);
    Global global = searchResponse.getAggregations().get("global");
    assertThat(global, notNullValue());
    assertThat(global.getName(), equalTo("global"));
    assertThat(global.getDocCount(), equalTo(10L));
    assertThat(global.getAggregations(), notNullValue());
    assertThat(global.getAggregations().asMap().size(), equalTo(1));
    PercentileRanks values = global.getAggregations().get("percentile_ranks");
    assertThat(values, notNullValue());
    assertThat(values.getName(), equalTo("percentile_ranks"));
    assertThat(((InternalAggregation) global).getProperty("percentile_ranks"), sameInstance(values));
}
Also used : Global(org.opensearch.search.aggregations.bucket.global.Global) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Aggregations

Global (org.opensearch.search.aggregations.bucket.global.Global)22 SearchResponse (org.opensearch.action.search.SearchResponse)20 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)18 InternalAggregation (org.opensearch.search.aggregations.InternalAggregation)11 Filter (org.opensearch.search.aggregations.bucket.filter.Filter)3 IndexReader (org.apache.lucene.index.IndexReader)2 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)2 IndexSearcher (org.apache.lucene.search.IndexSearcher)2 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)2 Directory (org.apache.lucene.store.Directory)2 GeoPoint (org.opensearch.common.geo.GeoPoint)2 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)2 AggregationBuilder (org.opensearch.search.aggregations.AggregationBuilder)2 AggregationBuilders.dateHistogram (org.opensearch.search.aggregations.AggregationBuilders.dateHistogram)2 GlobalAggregator (org.opensearch.search.aggregations.bucket.global.GlobalAggregator)2 Histogram (org.opensearch.search.aggregations.bucket.histogram.Histogram)2 Terms (org.opensearch.search.aggregations.bucket.terms.Terms)2 TermsAggregationBuilder (org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1