Search in sources :

Example 21 with Max

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

the class MaxIT method testMultiValuedFieldWithValueScriptWithParams.

@Override
public void testMultiValuedFieldWithValueScriptWithParams() throws Exception {
    Map<String, Object> params = new HashMap<>();
    params.put("inc", 1);
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(max("max").field("values").script(new Script(ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, "_value + inc", params))).get();
    assertHitCount(searchResponse, 10);
    Max max = searchResponse.getAggregations().get("max");
    assertThat(max, notNullValue());
    assertThat(max.getName(), equalTo("max"));
    assertThat(max.getValue(), equalTo(13.0));
}
Also used : Script(org.elasticsearch.script.Script) HashMap(java.util.HashMap) Max(org.elasticsearch.search.aggregations.metrics.max.Max) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 22 with Max

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

the class NestedIT method testNestedAsSubAggregation.

public void testNestedAsSubAggregation() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(terms("top_values").field("value").size(100).collectMode(aggCollectionMode).subAggregation(nested("nested", "nested").subAggregation(max("max_value").field("nested.value")))).execute().actionGet();
    assertSearchResponse(response);
    LongTerms values = response.getAggregations().get("top_values");
    assertThat(values, notNullValue());
    assertThat(values.getName(), equalTo("top_values"));
    assertThat(values.getBuckets(), notNullValue());
    assertThat(values.getBuckets().size(), equalTo(numParents));
    for (int i = 0; i < numParents; i++) {
        String topValue = "" + (i + 1);
        assertThat(values.getBucketByKey(topValue), notNullValue());
        Nested nested = values.getBucketByKey(topValue).getAggregations().get("nested");
        assertThat(nested, notNullValue());
        Max max = nested.getAggregations().get("max_value");
        assertThat(max, notNullValue());
        assertThat(max.getValue(), equalTo(numChildren[i] == 0 ? Double.NEGATIVE_INFINITY : (double) i + numChildren[i]));
    }
}
Also used : Max(org.elasticsearch.search.aggregations.metrics.max.Max) LongTerms(org.elasticsearch.search.aggregations.bucket.terms.LongTerms) Nested(org.elasticsearch.search.aggregations.bucket.nested.Nested) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 23 with Max

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

the class DiversifiedSamplerIT method testIssue10719.

public void testIssue10719() throws Exception {
    // Tests that we can refer to nested elements under a sample in a path
    // statement
    boolean asc = randomBoolean();
    SearchResponse response = client().prepareSearch("test").setTypes("book").setSearchType(SearchType.QUERY_THEN_FETCH).addAggregation(terms("genres").field("genre").order(Terms.Order.aggregation("sample>max_price.value", asc)).subAggregation(sampler("sample").shardSize(100).subAggregation(max("max_price").field("price")))).execute().actionGet();
    assertSearchResponse(response);
    Terms genres = response.getAggregations().get("genres");
    Collection<Bucket> genreBuckets = genres.getBuckets();
    // For this test to be useful we need >1 genre bucket to compare
    assertThat(genreBuckets.size(), greaterThan(1));
    double lastMaxPrice = asc ? Double.MIN_VALUE : Double.MAX_VALUE;
    for (Terms.Bucket genreBucket : genres.getBuckets()) {
        Sampler sample = genreBucket.getAggregations().get("sample");
        Max maxPriceInGenre = sample.getAggregations().get("max_price");
        double price = maxPriceInGenre.getValue();
        if (asc) {
            assertThat(price, greaterThanOrEqualTo(lastMaxPrice));
        } else {
            assertThat(price, lessThanOrEqualTo(lastMaxPrice));
        }
        lastMaxPrice = price;
    }
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) Max(org.elasticsearch.search.aggregations.metrics.max.Max) Sampler(org.elasticsearch.search.aggregations.bucket.sampler.Sampler) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 24 with Max

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

the class HistogramIT method testSingleValuedFieldOrderedBySubAggregationDescDeepOrderPath.

public void testSingleValuedFieldOrderedBySubAggregationDescDeepOrderPath() throws Exception {
    boolean asc = randomBoolean();
    SearchResponse response = client().prepareSearch("idx").addAggregation(histogram("histo").field(SINGLE_VALUED_FIELD_NAME).interval(interval).order(Histogram.Order.aggregation("filter>max", asc)).subAggregation(filter("filter", matchAllQuery()).subAggregation(max("max").field(SINGLE_VALUED_FIELD_NAME)))).execute().actionGet();
    assertSearchResponse(response);
    Histogram histo = response.getAggregations().get("histo");
    assertThat(histo, notNullValue());
    assertThat(histo.getName(), equalTo("histo"));
    assertThat(histo.getBuckets().size(), equalTo(numValueBuckets));
    LongHashSet visited = new LongHashSet();
    double prevMax = asc ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY;
    // TODO: use diamond once JI-9019884 is fixed
    List<Histogram.Bucket> buckets = new ArrayList<>(histo.getBuckets());
    for (int i = 0; i < numValueBuckets; ++i) {
        Histogram.Bucket bucket = buckets.get(i);
        assertThat(bucket, notNullValue());
        long key = ((Number) bucket.getKey()).longValue();
        assertTrue(visited.add(key));
        int b = (int) (key / interval);
        assertThat(bucket.getDocCount(), equalTo(valueCounts[b]));
        assertThat(bucket.getAggregations().asList().isEmpty(), is(false));
        Filter filter = bucket.getAggregations().get("filter");
        assertThat(filter, notNullValue());
        assertThat(bucket.getDocCount(), equalTo(filter.getDocCount()));
        Max max = filter.getAggregations().get("max");
        assertThat(max, Matchers.notNullValue());
        assertThat(max.getValue(), asc ? greaterThanOrEqualTo(prevMax) : lessThanOrEqualTo(prevMax));
        prevMax = max.getValue();
    }
}
Also used : Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) Max(org.elasticsearch.search.aggregations.metrics.max.Max) ArrayList(java.util.ArrayList) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) LongHashSet(com.carrotsearch.hppc.LongHashSet) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) Filter(org.elasticsearch.search.aggregations.bucket.filter.Filter)

Example 25 with Max

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

the class DoubleTermsIT method testSingleValuedFieldOrderedBySubAggregationAscMultiHierarchyLevels.

public void testSingleValuedFieldOrderedBySubAggregationAscMultiHierarchyLevels() throws Exception {
    boolean asc = randomBoolean();
    SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("tags").field("num_tag").collectMode(randomFrom(SubAggCollectionMode.values())).order(Terms.Order.aggregation("filter1>filter2>max", asc)).subAggregation(filter("filter1", QueryBuilders.matchAllQuery()).subAggregation(filter("filter2", QueryBuilders.matchAllQuery()).subAggregation(max("max").field(SINGLE_VALUED_FIELD_NAME))))).execute().actionGet();
    assertSearchResponse(response);
    Terms tags = response.getAggregations().get("tags");
    assertThat(tags, notNullValue());
    assertThat(tags.getName(), equalTo("tags"));
    assertThat(tags.getBuckets().size(), equalTo(2));
    Iterator<Terms.Bucket> iters = tags.getBuckets().iterator();
    // the max for "1" is 2
    // the max for "0" is 4
    Terms.Bucket tag = iters.next();
    assertThat(tag, notNullValue());
    assertThat(key(tag), equalTo(asc ? "1" : "0"));
    assertThat(tag.getDocCount(), equalTo(asc ? 3L : 2L));
    Filter filter1 = tag.getAggregations().get("filter1");
    assertThat(filter1, notNullValue());
    assertThat(filter1.getDocCount(), equalTo(asc ? 3L : 2L));
    Filter filter2 = filter1.getAggregations().get("filter2");
    assertThat(filter2, notNullValue());
    assertThat(filter2.getDocCount(), equalTo(asc ? 3L : 2L));
    Max max = filter2.getAggregations().get("max");
    assertThat(max, notNullValue());
    assertThat(max.getValue(), equalTo(asc ? 2.0 : 4.0));
    tag = iters.next();
    assertThat(tag, notNullValue());
    assertThat(key(tag), equalTo(asc ? "0" : "1"));
    assertThat(tag.getDocCount(), equalTo(asc ? 2L : 3L));
    filter1 = tag.getAggregations().get("filter1");
    assertThat(filter1, notNullValue());
    assertThat(filter1.getDocCount(), equalTo(asc ? 2L : 3L));
    filter2 = filter1.getAggregations().get("filter2");
    assertThat(filter2, notNullValue());
    assertThat(filter2.getDocCount(), equalTo(asc ? 2L : 3L));
    max = filter2.getAggregations().get("max");
    assertThat(max, notNullValue());
    assertThat(max.getValue(), equalTo(asc ? 4.0 : 2.0));
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) Filter(org.elasticsearch.search.aggregations.bucket.filter.Filter) Max(org.elasticsearch.search.aggregations.metrics.max.Max) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

Max (org.elasticsearch.search.aggregations.metrics.max.Max)25 SearchResponse (org.elasticsearch.action.search.SearchResponse)24 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)23 Script (org.elasticsearch.script.Script)8 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)7 Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)5 HashMap (java.util.HashMap)4 Bucket (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket)4 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)2 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)2 Sampler (org.elasticsearch.search.aggregations.bucket.sampler.Sampler)2 LongHashSet (com.carrotsearch.hppc.LongHashSet)1 ImmutableList (com.google.common.collect.ImmutableList)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)1 SearchRequest (org.elasticsearch.action.search.SearchRequest)1 IndexClosedException (org.elasticsearch.indices.IndexClosedException)1 SearchHits (org.elasticsearch.search.SearchHits)1 FilterAggregationBuilder (org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder)1