use of org.elasticsearch.search.aggregations.bucket.terms.Terms in project elasticsearch by elastic.
the class StringTermsIT method assertMultiSortResponse.
private void assertMultiSortResponse(String[] expectedKeys, Terms.Order... order) {
SearchResponse response = client().prepareSearch("sort_idx").setTypes("multi_sort_type").addAggregation(terms("terms").executionHint(randomExecutionHint()).field(SINGLE_VALUED_FIELD_NAME).collectMode(randomFrom(SubAggCollectionMode.values())).order(Terms.Order.compound(order)).subAggregation(avg("avg_l").field("l")).subAggregation(sum("sum_d").field("d"))).execute().actionGet();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(expectedKeys.length));
int i = 0;
for (Terms.Bucket bucket : terms.getBuckets()) {
assertThat(bucket, notNullValue());
assertThat(key(bucket), equalTo(expectedKeys[i]));
assertThat(bucket.getDocCount(), equalTo(expectedMultiSortBuckets.get(expectedKeys[i]).get("_count")));
Avg avg = bucket.getAggregations().get("avg_l");
assertThat(avg, notNullValue());
assertThat(avg.getValue(), equalTo(expectedMultiSortBuckets.get(expectedKeys[i]).get("avg_l")));
Sum sum = bucket.getAggregations().get("sum_d");
assertThat(sum, notNullValue());
assertThat(sum.getValue(), equalTo(expectedMultiSortBuckets.get(expectedKeys[i]).get("sum_d")));
i++;
}
}
use of org.elasticsearch.search.aggregations.bucket.terms.Terms in project elasticsearch by elastic.
the class StringTermsIT method testIndexMetaField.
public void testIndexMetaField() throws Exception {
SearchResponse response = client().prepareSearch("idx", "empty_bucket_idx").setTypes("type").addAggregation(terms("terms").collectMode(randomFrom(SubAggCollectionMode.values())).executionHint(randomExecutionHint()).field(IndexFieldMapper.NAME)).execute().actionGet();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(2));
int i = 0;
for (Terms.Bucket bucket : terms.getBuckets()) {
assertThat(bucket, notNullValue());
assertThat(key(bucket), equalTo(i == 0 ? "idx" : "empty_bucket_idx"));
assertThat(bucket.getDocCount(), equalTo(i == 0 ? 5L : 2L));
i++;
}
}
use of org.elasticsearch.search.aggregations.bucket.terms.Terms in project elasticsearch by elastic.
the class StringTermsIT method testEmptyAggregation.
public void testEmptyAggregation() throws Exception {
SearchResponse searchResponse = client().prepareSearch("empty_bucket_idx").setQuery(matchAllQuery()).addAggregation(histogram("histo").field(SINGLE_VALUED_FIELD_NAME).interval(1L).minDocCount(0).subAggregation(terms("terms").field("value"))).execute().actionGet();
assertThat(searchResponse.getHits().getTotalHits(), equalTo(2L));
Histogram histo = searchResponse.getAggregations().get("histo");
assertThat(histo, Matchers.notNullValue());
Histogram.Bucket bucket = histo.getBuckets().get(1);
assertThat(bucket, Matchers.notNullValue());
Terms terms = bucket.getAggregations().get("terms");
assertThat(terms, Matchers.notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().isEmpty(), is(true));
}
use of org.elasticsearch.search.aggregations.bucket.terms.Terms in project elasticsearch by elastic.
the class StringTermsIT method testSingleValuedFieldOrderedBySingleValueSubAggregationDesc.
public void testSingleValuedFieldOrderedBySingleValueSubAggregationDesc() throws Exception {
boolean asc = false;
SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("terms").executionHint(randomExecutionHint()).field(SINGLE_VALUED_FIELD_NAME).collectMode(randomFrom(SubAggCollectionMode.values())).order(Terms.Order.aggregation("avg_i", asc)).subAggregation(avg("avg_i").field("i"))).execute().actionGet();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(5));
int i = 4;
for (Terms.Bucket bucket : terms.getBuckets()) {
assertThat(bucket, notNullValue());
assertThat(key(bucket), equalTo("val" + i));
assertThat(bucket.getDocCount(), equalTo(1L));
Avg avg = bucket.getAggregations().get("avg_i");
assertThat(avg, notNullValue());
assertThat(avg.getValue(), equalTo((double) i));
i--;
}
}
use of org.elasticsearch.search.aggregations.bucket.terms.Terms in project elasticsearch by elastic.
the class StringTermsIT method testScriptSingleValue.
/*
*
* [foo_val0, foo_val1] [foo_val1, foo_val2] [foo_val2, foo_val3] [foo_val3,
* foo_val4] [foo_val4, foo_val5]
*
*
* foo_val0 - doc_count: 1 - val_count: 2 foo_val1 - doc_count: 2 -
* val_count: 4 foo_val2 - doc_count: 2 - val_count: 4 foo_val3 - doc_count:
* 2 - val_count: 4 foo_val4 - doc_count: 2 - val_count: 4 foo_val5 -
* doc_count: 1 - val_count: 2
*/
public void testScriptSingleValue() throws Exception {
Script script = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "doc['" + SINGLE_VALUED_FIELD_NAME + "'].value", Collections.emptyMap());
SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("terms").collectMode(randomFrom(SubAggCollectionMode.values())).executionHint(randomExecutionHint()).script(script)).get();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(5));
for (int i = 0; i < 5; i++) {
Terms.Bucket bucket = terms.getBucketByKey("val" + i);
assertThat(bucket, notNullValue());
assertThat(key(bucket), equalTo("val" + i));
assertThat(bucket.getDocCount(), equalTo(1L));
}
}
Aggregations