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));
}
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));
}
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));
}
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));
}
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));
}
Aggregations