Search in sources :

Example 1 with Global

use of org.elasticsearch.search.aggregations.bucket.global.Global in project elasticsearch by elastic.

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"))).execute().actionGet();
    assertSearchResponse(response);
    Global global = response.getAggregations().get("global");
    assertThat(global, notNullValue());
    assertThat(global.getName(), equalTo("global"));
    assertThat(global.getDocCount(), equalTo((long) numDocs));
    assertThat((long) global.getProperty("_count"), equalTo((long) numDocs));
    assertThat(global.getAggregations().asList().isEmpty(), is(false));
    Stats stats = global.getAggregations().get("value_stats");
    assertThat((Stats) 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 : Stats(org.elasticsearch.search.aggregations.metrics.stats.Stats) Global(org.elasticsearch.search.aggregations.bucket.global.Global) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 2 with Global

use of org.elasticsearch.search.aggregations.bucket.global.Global in project elasticsearch by elastic.

the class AvgIT method testSingleValuedFieldGetProperty.

@Override
public void testSingleValuedFieldGetProperty() throws Exception {
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(global("global").subAggregation(avg("avg").field("value"))).execute().actionGet();
    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));
    Avg avg = global.getAggregations().get("avg");
    assertThat(avg, notNullValue());
    assertThat(avg.getName(), equalTo("avg"));
    double expectedAvgValue = (double) (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10) / 10;
    assertThat(avg.getValue(), equalTo(expectedAvgValue));
    assertThat((Avg) global.getProperty("avg"), equalTo(avg));
    assertThat((double) global.getProperty("avg.value"), equalTo(expectedAvgValue));
    assertThat((double) avg.getProperty("value"), equalTo(expectedAvgValue));
}
Also used : Avg(org.elasticsearch.search.aggregations.metrics.avg.Avg) Global(org.elasticsearch.search.aggregations.bucket.global.Global) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 3 with Global

use of org.elasticsearch.search.aggregations.bucket.global.Global in project elasticsearch by elastic.

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").method(PercentilesMethod.HDR).numberOfSignificantValueDigits(sigDigits).field("value").values(pcts))).execute().actionGet();
    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(global.getProperty("percentile_ranks"), sameInstance(values));
}
Also used : PercentileRanks(org.elasticsearch.search.aggregations.metrics.percentiles.PercentileRanks) Global(org.elasticsearch.search.aggregations.bucket.global.Global) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 4 with Global

use of org.elasticsearch.search.aggregations.bucket.global.Global in project elasticsearch by elastic.

the class MaxIT method testSingleValuedFieldGetProperty.

@Override
public void testSingleValuedFieldGetProperty() throws Exception {
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(global("global").subAggregation(max("max").field("value"))).execute().actionGet();
    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));
    Max max = global.getAggregations().get("max");
    assertThat(max, notNullValue());
    assertThat(max.getName(), equalTo("max"));
    double expectedMaxValue = 10.0;
    assertThat(max.getValue(), equalTo(expectedMaxValue));
    assertThat((Max) global.getProperty("max"), equalTo(max));
    assertThat((double) global.getProperty("max.value"), equalTo(expectedMaxValue));
    assertThat((double) max.getProperty("value"), equalTo(expectedMaxValue));
}
Also used : Max(org.elasticsearch.search.aggregations.metrics.max.Max) Global(org.elasticsearch.search.aggregations.bucket.global.Global) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 5 with Global

use of org.elasticsearch.search.aggregations.bucket.global.Global in project elasticsearch by elastic.

the class ScriptedMetricIT method testInitMapCombineReduceGetProperty.

@SuppressWarnings("rawtypes")
public void testInitMapCombineReduceGetProperty() throws Exception {
    Map<String, Object> varsMap = new HashMap<>();
    varsMap.put("multiplier", 1);
    Map<String, Object> params = new HashMap<>();
    params.put("_agg", new ArrayList<>());
    params.put("vars", varsMap);
    Script initScript = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "vars.multiplier = 3", Collections.emptyMap());
    Script mapScript = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "_agg.add(vars.multiplier)", Collections.emptyMap());
    Script combineScript = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "sum agg values as a new aggregation", Collections.emptyMap());
    Script reduceScript = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "sum aggs of agg values as a new aggregation", Collections.emptyMap());
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(global("global").subAggregation(scriptedMetric("scripted").params(params).initScript(initScript).mapScript(mapScript).combineScript(combineScript).reduceScript(reduceScript))).get();
    assertSearchResponse(searchResponse);
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(numDocs));
    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));
    ScriptedMetric scriptedMetricAggregation = global.getAggregations().get("scripted");
    assertThat(scriptedMetricAggregation, notNullValue());
    assertThat(scriptedMetricAggregation.getName(), equalTo("scripted"));
    assertThat(scriptedMetricAggregation.aggregation(), notNullValue());
    assertThat(scriptedMetricAggregation.aggregation(), instanceOf(ArrayList.class));
    List<?> aggregationList = (List<?>) scriptedMetricAggregation.aggregation();
    assertThat(aggregationList.size(), equalTo(1));
    Object object = aggregationList.get(0);
    assertThat(object, notNullValue());
    assertThat(object, instanceOf(Number.class));
    assertThat(((Number) object).longValue(), equalTo(numDocs * 3));
    assertThat((ScriptedMetric) global.getProperty("scripted"), sameInstance(scriptedMetricAggregation));
    assertThat((List) global.getProperty("scripted.value"), sameInstance((List) aggregationList));
    assertThat((List) scriptedMetricAggregation.getProperty("value"), sameInstance((List) aggregationList));
}
Also used : Script(org.elasticsearch.script.Script) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ScriptedMetric(org.elasticsearch.search.aggregations.metrics.scripted.ScriptedMetric) Global(org.elasticsearch.search.aggregations.bucket.global.Global) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

SearchResponse (org.elasticsearch.action.search.SearchResponse)23 Global (org.elasticsearch.search.aggregations.bucket.global.Global)23 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)20 Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)3 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)3 GeoPoint (org.elasticsearch.common.geo.GeoPoint)2 AggregationBuilders.dateHistogram (org.elasticsearch.search.aggregations.AggregationBuilders.dateHistogram)2 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)2 PercentileRanks (org.elasticsearch.search.aggregations.metrics.percentiles.PercentileRanks)2 Percentiles (org.elasticsearch.search.aggregations.metrics.percentiles.Percentiles)2 Stats (org.elasticsearch.search.aggregations.metrics.stats.Stats)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 MultiSearchResponse (org.elasticsearch.action.search.MultiSearchResponse)1 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)1 Script (org.elasticsearch.script.Script)1 GlobalBuilder (org.elasticsearch.search.aggregations.bucket.global.GlobalBuilder)1 Missing (org.elasticsearch.search.aggregations.bucket.missing.Missing)1 TermsBuilder (org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder)1