Search in sources :

Example 1 with Range

use of org.opensearch.search.aggregations.bucket.range.Range in project OpenSearch by opensearch-project.

the class SearchIT method testSearchWithRangeAgg.

public void testSearchWithRangeAgg() throws IOException {
    {
        SearchRequest searchRequest = new SearchRequest();
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.aggregation(new RangeAggregationBuilder("agg1").field("num"));
        searchSourceBuilder.size(0);
        searchRequest.source(searchSourceBuilder);
        OpenSearchStatusException exception = expectThrows(OpenSearchStatusException.class, () -> execute(searchRequest, highLevelClient()::search, highLevelClient()::searchAsync));
        assertEquals(RestStatus.BAD_REQUEST, exception.status());
    }
    SearchRequest searchRequest = new SearchRequest("index");
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.aggregation(new RangeAggregationBuilder("agg1").field("num").addRange("first", 0, 30).addRange("second", 31, 200));
    searchSourceBuilder.size(0);
    searchRequest.source(searchSourceBuilder);
    SearchResponse searchResponse = execute(searchRequest, highLevelClient()::search, highLevelClient()::searchAsync);
    assertSearchHeader(searchResponse);
    assertNull(searchResponse.getSuggest());
    assertEquals(Collections.emptyMap(), searchResponse.getProfileResults());
    assertEquals(5, searchResponse.getHits().getTotalHits().value);
    assertEquals(0, searchResponse.getHits().getHits().length);
    assertEquals(Float.NaN, searchResponse.getHits().getMaxScore(), 0f);
    Range rangeAgg = searchResponse.getAggregations().get("agg1");
    assertEquals("agg1", rangeAgg.getName());
    assertEquals(2, rangeAgg.getBuckets().size());
    {
        Range.Bucket bucket = rangeAgg.getBuckets().get(0);
        assertEquals("first", bucket.getKeyAsString());
        assertEquals(2, bucket.getDocCount());
    }
    {
        Range.Bucket bucket = rangeAgg.getBuckets().get(1);
        assertEquals("second", bucket.getKeyAsString());
        assertEquals(3, bucket.getDocCount());
    }
}
Also used : MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) Range(org.opensearch.search.aggregations.bucket.range.Range) RangeAggregationBuilder(org.opensearch.search.aggregations.bucket.range.RangeAggregationBuilder) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) OpenSearchStatusException(org.opensearch.OpenSearchStatusException) MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 2 with Range

use of org.opensearch.search.aggregations.bucket.range.Range in project OpenSearch by opensearch-project.

the class EquivalenceIT method testRandomRanges.

// Make sure that unordered, reversed, disjoint and/or overlapping ranges are supported
// Duel with filters
public void testRandomRanges() throws Exception {
    final int numDocs = scaledRandomIntBetween(500, 5000);
    final double[][] docs = new double[numDocs][];
    for (int i = 0; i < numDocs; ++i) {
        final int numValues = randomInt(5);
        docs[i] = new double[numValues];
        for (int j = 0; j < numValues; ++j) {
            docs[i][j] = randomDouble() * 100;
        }
    }
    prepareCreate("idx").addMapping("type", jsonBuilder().startObject().startObject("type").startObject("properties").startObject("values").field("type", "double").endObject().endObject().endObject().endObject()).get();
    for (int i = 0; i < docs.length; ++i) {
        XContentBuilder source = jsonBuilder().startObject().startArray("values");
        for (int j = 0; j < docs[i].length; ++j) {
            source = source.value(docs[i][j]);
        }
        source = source.endArray().endObject();
        client().prepareIndex("idx").setSource(source).get();
    }
    assertNoFailures(client().admin().indices().prepareRefresh("idx").setIndicesOptions(IndicesOptions.lenientExpandOpen()).get());
    final int numRanges = randomIntBetween(1, 20);
    final double[][] ranges = new double[numRanges][];
    for (int i = 0; i < ranges.length; ++i) {
        switch(randomInt(2)) {
            case 0:
                ranges[i] = new double[] { Double.NEGATIVE_INFINITY, randomInt(100) };
                break;
            case 1:
                ranges[i] = new double[] { randomInt(100), Double.POSITIVE_INFINITY };
                break;
            case 2:
                ranges[i] = new double[] { randomInt(100), randomInt(100) };
                break;
            default:
                throw new AssertionError();
        }
    }
    RangeAggregationBuilder query = range("range").field("values");
    for (int i = 0; i < ranges.length; ++i) {
        String key = Integer.toString(i);
        if (ranges[i][0] == Double.NEGATIVE_INFINITY) {
            query.addUnboundedTo(key, ranges[i][1]);
        } else if (ranges[i][1] == Double.POSITIVE_INFINITY) {
            query.addUnboundedFrom(key, ranges[i][0]);
        } else {
            query.addRange(key, ranges[i][0], ranges[i][1]);
        }
    }
    SearchRequestBuilder reqBuilder = client().prepareSearch("idx").addAggregation(query);
    for (int i = 0; i < ranges.length; ++i) {
        RangeQueryBuilder filter = QueryBuilders.rangeQuery("values");
        if (ranges[i][0] != Double.NEGATIVE_INFINITY) {
            filter = filter.from(ranges[i][0]);
        }
        if (ranges[i][1] != Double.POSITIVE_INFINITY) {
            filter = filter.to(ranges[i][1]);
        }
        reqBuilder = reqBuilder.addAggregation(filter("filter" + i, filter));
    }
    SearchResponse resp = reqBuilder.get();
    Range range = resp.getAggregations().get("range");
    List<? extends Bucket> buckets = range.getBuckets();
    HashMap<String, Bucket> bucketMap = new HashMap<>(buckets.size());
    for (Bucket bucket : buckets) {
        bucketMap.put(bucket.getKeyAsString(), bucket);
    }
    for (int i = 0; i < ranges.length; ++i) {
        long count = 0;
        for (double[] values : docs) {
            for (double value : values) {
                if (value >= ranges[i][0] && value < ranges[i][1]) {
                    ++count;
                    break;
                }
            }
        }
        final Range.Bucket bucket = bucketMap.get(Integer.toString(i));
        assertEquals(bucket.getKeyAsString(), Integer.toString(i), bucket.getKeyAsString());
        assertEquals(bucket.getKeyAsString(), count, bucket.getDocCount());
        final Filter filter = resp.getAggregations().get("filter" + i);
        assertThat(filter.getDocCount(), equalTo(count));
    }
}
Also used : Bucket(org.opensearch.search.aggregations.bucket.range.Range.Bucket) SearchRequestBuilder(org.opensearch.action.search.SearchRequestBuilder) HashMap(java.util.HashMap) RangeQueryBuilder(org.opensearch.index.query.RangeQueryBuilder) Range(org.opensearch.search.aggregations.bucket.range.Range) RangeAggregationBuilder(org.opensearch.search.aggregations.bucket.range.RangeAggregationBuilder) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse) Bucket(org.opensearch.search.aggregations.bucket.range.Range.Bucket) Filter(org.opensearch.search.aggregations.bucket.filter.Filter) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 3 with Range

use of org.opensearch.search.aggregations.bucket.range.Range in project OpenSearch by opensearch-project.

the class EquivalenceIT method testReduce.

// https://github.com/elastic/elasticsearch/issues/6435
public void testReduce() throws Exception {
    createIndex("idx");
    final int value = randomIntBetween(0, 10);
    indexRandom(true, client().prepareIndex("idx").setSource("f", value));
    SearchResponse response = client().prepareSearch("idx").addAggregation(filter("filter", QueryBuilders.matchAllQuery()).subAggregation(range("range").field("f").addUnboundedTo(6).addUnboundedFrom(6).subAggregation(sum("sum").field("f")))).get();
    assertSearchResponse(response);
    Filter filter = response.getAggregations().get("filter");
    assertNotNull(filter);
    assertEquals(1, filter.getDocCount());
    Range range = filter.getAggregations().get("range");
    assertThat(range, notNullValue());
    assertThat(range.getName(), equalTo("range"));
    List<? extends Bucket> buckets = range.getBuckets();
    assertThat(buckets.size(), equalTo(2));
    Range.Bucket bucket = buckets.get(0);
    assertThat(bucket, notNullValue());
    assertThat((String) bucket.getKey(), equalTo("*-6.0"));
    assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(Double.NEGATIVE_INFINITY));
    assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(6.0));
    assertThat(bucket.getDocCount(), equalTo(value < 6 ? 1L : 0L));
    Sum sum = bucket.getAggregations().get("sum");
    assertEquals(value < 6 ? value : 0, sum.getValue(), 0d);
    bucket = buckets.get(1);
    assertThat(bucket, notNullValue());
    assertThat((String) bucket.getKey(), equalTo("6.0-*"));
    assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(6.0));
    assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(Double.POSITIVE_INFINITY));
    assertThat(bucket.getDocCount(), equalTo(value >= 6 ? 1L : 0L));
    sum = bucket.getAggregations().get("sum");
    assertEquals(value >= 6 ? value : 0, sum.getValue(), 0d);
}
Also used : Bucket(org.opensearch.search.aggregations.bucket.range.Range.Bucket) Filter(org.opensearch.search.aggregations.bucket.filter.Filter) Sum(org.opensearch.search.aggregations.metrics.Sum) Range(org.opensearch.search.aggregations.bucket.range.Range) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 4 with Range

use of org.opensearch.search.aggregations.bucket.range.Range in project OpenSearch by opensearch-project.

the class GeoDistanceIT method testSimpleWithCustomKeys.

public void testSimpleWithCustomKeys() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(geoDistance("amsterdam_rings", new GeoPoint(52.3760, 4.894)).field("location").unit(DistanceUnit.KILOMETERS).addUnboundedTo("ring1", 500).addRange("ring2", 500, 1000).addUnboundedFrom("ring3", 1000)).get();
    assertSearchResponse(response);
    Range geoDist = response.getAggregations().get("amsterdam_rings");
    assertThat(geoDist, notNullValue());
    assertThat(geoDist.getName(), equalTo("amsterdam_rings"));
    List<? extends Bucket> buckets = geoDist.getBuckets();
    assertThat(buckets.size(), equalTo(3));
    Range.Bucket bucket = buckets.get(0);
    assertThat(bucket, notNullValue());
    assertThat((String) bucket.getKey(), equalTo("ring1"));
    assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(0.0));
    assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(500.0));
    assertThat(bucket.getFromAsString(), equalTo("0.0"));
    assertThat(bucket.getToAsString(), equalTo("500.0"));
    assertThat(bucket.getDocCount(), equalTo(2L));
    bucket = buckets.get(1);
    assertThat(bucket, notNullValue());
    assertThat((String) bucket.getKey(), equalTo("ring2"));
    assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(500.0));
    assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(1000.0));
    assertThat(bucket.getFromAsString(), equalTo("500.0"));
    assertThat(bucket.getToAsString(), equalTo("1000.0"));
    assertThat(bucket.getDocCount(), equalTo(2L));
    bucket = buckets.get(2);
    assertThat(bucket, notNullValue());
    assertThat((String) bucket.getKey(), equalTo("ring3"));
    assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(1000.0));
    assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(Double.POSITIVE_INFINITY));
    assertThat(bucket.getFromAsString(), equalTo("1000.0"));
    assertThat(bucket.getToAsString(), nullValue());
    assertThat(bucket.getDocCount(), equalTo(1L));
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) Bucket(org.opensearch.search.aggregations.bucket.range.Range.Bucket) Range(org.opensearch.search.aggregations.bucket.range.Range) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 5 with Range

use of org.opensearch.search.aggregations.bucket.range.Range in project OpenSearch by opensearch-project.

the class GeoDistanceIT method testEmptyAggregation.

public void testEmptyAggregation() throws Exception {
    SearchResponse searchResponse = client().prepareSearch("empty_bucket_idx").setQuery(matchAllQuery()).addAggregation(histogram("histo").field("value").interval(1L).minDocCount(0).subAggregation(geoDistance("geo_dist", new GeoPoint(52.3760, 4.894)).field("location").addRange("0-100", 0.0, 100.0))).get();
    assertThat(searchResponse.getHits().getTotalHits().value, equalTo(2L));
    Histogram histo = searchResponse.getAggregations().get("histo");
    assertThat(histo, Matchers.notNullValue());
    Histogram.Bucket bucket = histo.getBuckets().get(1);
    assertThat(bucket, Matchers.notNullValue());
    Range geoDistance = bucket.getAggregations().get("geo_dist");
    // TODO: use diamond once JI-9019884 is fixed
    List<Range.Bucket> buckets = new ArrayList<>(geoDistance.getBuckets());
    assertThat(geoDistance, Matchers.notNullValue());
    assertThat(geoDistance.getName(), equalTo("geo_dist"));
    assertThat(buckets.size(), is(1));
    assertThat((String) buckets.get(0).getKey(), equalTo("0-100"));
    assertThat(((Number) buckets.get(0).getFrom()).doubleValue(), equalTo(0.0));
    assertThat(((Number) buckets.get(0).getTo()).doubleValue(), equalTo(100.0));
    assertThat(buckets.get(0).getFromAsString(), equalTo("0.0"));
    assertThat(buckets.get(0).getToAsString(), equalTo("100.0"));
    assertThat(buckets.get(0).getDocCount(), equalTo(0L));
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) Histogram(org.opensearch.search.aggregations.bucket.histogram.Histogram) Bucket(org.opensearch.search.aggregations.bucket.range.Range.Bucket) ArrayList(java.util.ArrayList) Range(org.opensearch.search.aggregations.bucket.range.Range) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Aggregations

Range (org.opensearch.search.aggregations.bucket.range.Range)56 SearchResponse (org.opensearch.action.search.SearchResponse)55 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)51 Bucket (org.opensearch.search.aggregations.bucket.range.Range.Bucket)41 AggregationBuilders.dateRange (org.opensearch.search.aggregations.AggregationBuilders.dateRange)19 ZonedDateTime (java.time.ZonedDateTime)14 Script (org.opensearch.script.Script)10 GeoPoint (org.opensearch.common.geo.GeoPoint)8 Histogram (org.opensearch.search.aggregations.bucket.histogram.Histogram)7 HashMap (java.util.HashMap)6 Matchers.containsString (org.hamcrest.Matchers.containsString)6 ArrayList (java.util.ArrayList)5 Terms (org.opensearch.search.aggregations.bucket.terms.Terms)5 Sum (org.opensearch.search.aggregations.metrics.Sum)4 MultiSearchRequest (org.opensearch.action.search.MultiSearchRequest)3 MultiSearchResponse (org.opensearch.action.search.MultiSearchResponse)3 SearchRequest (org.opensearch.action.search.SearchRequest)3 SearchSourceBuilder (org.opensearch.search.builder.SearchSourceBuilder)3 SearchRequestBuilder (org.opensearch.action.search.SearchRequestBuilder)2 AggregationBuilders.dateHistogram (org.opensearch.search.aggregations.AggregationBuilders.dateHistogram)2