use of org.elasticsearch.search.aggregations.bucket.terms.Terms in project elasticsearch by elastic.
the class DoubleTermsIT method testMultiValuedFieldWithValueScript.
public void testMultiValuedFieldWithValueScript() throws Exception {
SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("terms").field(MULTI_VALUED_FIELD_NAME).collectMode(randomFrom(SubAggCollectionMode.values())).script(new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "_value + 1", Collections.emptyMap()))).get();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(6));
for (int i = 0; i < 6; i++) {
Terms.Bucket bucket = terms.getBucketByKey("" + (i + 1d));
assertThat(bucket, notNullValue());
assertThat(key(bucket), equalTo("" + (i + 1d)));
assertThat(bucket.getKeyAsNumber().intValue(), equalTo(i + 1));
if (i == 0 || i == 5) {
assertThat(bucket.getDocCount(), equalTo(1L));
} else {
assertThat(bucket.getDocCount(), equalTo(2L));
}
}
}
use of org.elasticsearch.search.aggregations.bucket.terms.Terms in project elasticsearch by elastic.
the class DoubleTermsIT method testSingleValuedFieldOrderedBySingleBucketSubAggregationAsc.
public void testSingleValuedFieldOrderedBySingleBucketSubAggregationAsc() throws Exception {
boolean asc = randomBoolean();
SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("num_tags").field("num_tag").collectMode(randomFrom(SubAggCollectionMode.values())).order(Terms.Order.aggregation("filter", asc)).subAggregation(filter("filter", QueryBuilders.matchAllQuery()))).execute().actionGet();
assertSearchResponse(response);
Terms tags = response.getAggregations().get("num_tags");
assertThat(tags, notNullValue());
assertThat(tags.getName(), equalTo("num_tags"));
assertThat(tags.getBuckets().size(), equalTo(2));
Iterator<Terms.Bucket> iters = tags.getBuckets().iterator();
Terms.Bucket tag = iters.next();
assertThat(tag, notNullValue());
assertThat(key(tag), equalTo(asc ? "0" : "1"));
assertThat(tag.getDocCount(), equalTo(asc ? 2L : 3L));
Filter filter = tag.getAggregations().get("filter");
assertThat(filter, notNullValue());
assertThat(filter.getDocCount(), equalTo(asc ? 2L : 3L));
tag = iters.next();
assertThat(tag, notNullValue());
assertThat(key(tag), equalTo(asc ? "1" : "0"));
assertThat(tag.getDocCount(), equalTo(asc ? 3L : 2L));
filter = tag.getAggregations().get("filter");
assertThat(filter, notNullValue());
assertThat(filter.getDocCount(), equalTo(asc ? 3L : 2L));
}
use of org.elasticsearch.search.aggregations.bucket.terms.Terms in project elasticsearch by elastic.
the class ReverseNestedIT method testSimpleReverseNestedToNested1.
public void testSimpleReverseNestedToNested1() throws Exception {
SearchResponse response = client().prepareSearch("idx").setTypes("type2").addAggregation(nested("nested1", "nested1.nested2").subAggregation(terms("field2").field("nested1.nested2.field2").order(Terms.Order.term(true)).collectMode(randomFrom(SubAggCollectionMode.values())).size(10000).subAggregation(reverseNested("nested1_to_field1").path("nested1").subAggregation(terms("field1").field("nested1.field1").order(Terms.Order.term(true)).collectMode(randomFrom(SubAggCollectionMode.values())))))).get();
assertSearchResponse(response);
Nested nested = response.getAggregations().get("nested1");
assertThat(nested, notNullValue());
assertThat(nested.getName(), equalTo("nested1"));
assertThat(nested.getDocCount(), equalTo(27L));
assertThat(nested.getAggregations().asList().isEmpty(), is(false));
Terms usernames = nested.getAggregations().get("field2");
assertThat(usernames, notNullValue());
assertThat(usernames.getBuckets().size(), equalTo(5));
List<Terms.Bucket> usernameBuckets = new ArrayList<>(usernames.getBuckets());
Terms.Bucket bucket = usernameBuckets.get(0);
assertThat(bucket.getKeyAsString(), equalTo("0"));
assertThat(bucket.getDocCount(), equalTo(12L));
ReverseNested reverseNested = bucket.getAggregations().get("nested1_to_field1");
assertThat(reverseNested.getDocCount(), equalTo(5L));
Terms tags = reverseNested.getAggregations().get("field1");
List<Terms.Bucket> tagsBuckets = new ArrayList<>(tags.getBuckets());
assertThat(tagsBuckets.size(), equalTo(2));
assertThat(tagsBuckets.get(0).getKeyAsString(), equalTo("a"));
assertThat(tagsBuckets.get(0).getDocCount(), equalTo(3L));
assertThat(tagsBuckets.get(1).getKeyAsString(), equalTo("b"));
assertThat(tagsBuckets.get(1).getDocCount(), equalTo(2L));
bucket = usernameBuckets.get(1);
assertThat(bucket.getKeyAsString(), equalTo("1"));
assertThat(bucket.getDocCount(), equalTo(6L));
reverseNested = bucket.getAggregations().get("nested1_to_field1");
assertThat(reverseNested.getDocCount(), equalTo(4L));
tags = reverseNested.getAggregations().get("field1");
tagsBuckets = new ArrayList<>(tags.getBuckets());
assertThat(tagsBuckets.size(), equalTo(4));
assertThat(tagsBuckets.get(0).getKeyAsString(), equalTo("a"));
assertThat(tagsBuckets.get(0).getDocCount(), equalTo(1L));
assertThat(tagsBuckets.get(1).getKeyAsString(), equalTo("b"));
assertThat(tagsBuckets.get(1).getDocCount(), equalTo(1L));
assertThat(tagsBuckets.get(2).getKeyAsString(), equalTo("c"));
assertThat(tagsBuckets.get(2).getDocCount(), equalTo(1L));
assertThat(tagsBuckets.get(3).getKeyAsString(), equalTo("e"));
assertThat(tagsBuckets.get(3).getDocCount(), equalTo(1L));
bucket = usernameBuckets.get(2);
assertThat(bucket.getKeyAsString(), equalTo("2"));
assertThat(bucket.getDocCount(), equalTo(5L));
reverseNested = bucket.getAggregations().get("nested1_to_field1");
assertThat(reverseNested.getDocCount(), equalTo(4L));
tags = reverseNested.getAggregations().get("field1");
tagsBuckets = new ArrayList<>(tags.getBuckets());
assertThat(tagsBuckets.size(), equalTo(4));
assertThat(tagsBuckets.get(0).getKeyAsString(), equalTo("a"));
assertThat(tagsBuckets.get(0).getDocCount(), equalTo(1L));
assertThat(tagsBuckets.get(1).getKeyAsString(), equalTo("b"));
assertThat(tagsBuckets.get(1).getDocCount(), equalTo(1L));
assertThat(tagsBuckets.get(2).getKeyAsString(), equalTo("c"));
assertThat(tagsBuckets.get(2).getDocCount(), equalTo(1L));
assertThat(tagsBuckets.get(3).getKeyAsString(), equalTo("e"));
assertThat(tagsBuckets.get(3).getDocCount(), equalTo(1L));
bucket = usernameBuckets.get(3);
assertThat(bucket.getKeyAsString(), equalTo("3"));
assertThat(bucket.getDocCount(), equalTo(2L));
reverseNested = bucket.getAggregations().get("nested1_to_field1");
assertThat(reverseNested.getDocCount(), equalTo(2L));
tags = reverseNested.getAggregations().get("field1");
tagsBuckets = new ArrayList<>(tags.getBuckets());
assertThat(tagsBuckets.size(), equalTo(2));
assertThat(tagsBuckets.get(0).getKeyAsString(), equalTo("d"));
assertThat(tagsBuckets.get(0).getDocCount(), equalTo(1L));
assertThat(tagsBuckets.get(1).getKeyAsString(), equalTo("f"));
bucket = usernameBuckets.get(4);
assertThat(bucket.getKeyAsString(), equalTo("4"));
assertThat(bucket.getDocCount(), equalTo(2L));
reverseNested = bucket.getAggregations().get("nested1_to_field1");
assertThat(reverseNested.getDocCount(), equalTo(2L));
tags = reverseNested.getAggregations().get("field1");
tagsBuckets = new ArrayList<>(tags.getBuckets());
assertThat(tagsBuckets.size(), equalTo(2));
assertThat(tagsBuckets.get(0).getKeyAsString(), equalTo("d"));
assertThat(tagsBuckets.get(0).getDocCount(), equalTo(1L));
assertThat(tagsBuckets.get(1).getKeyAsString(), equalTo("f"));
}
use of org.elasticsearch.search.aggregations.bucket.terms.Terms in project elasticsearch by elastic.
the class SamplerIT method testUnmappedChildAggNoDiversity.
public void testUnmappedChildAggNoDiversity() throws Exception {
SamplerAggregationBuilder sampleAgg = sampler("sample").shardSize(100);
sampleAgg.subAggregation(terms("authors").field("author"));
SearchResponse response = client().prepareSearch("idx_unmapped").setSearchType(SearchType.QUERY_THEN_FETCH).setQuery(new TermQueryBuilder("genre", "fantasy")).setFrom(0).setSize(60).addAggregation(sampleAgg).execute().actionGet();
assertSearchResponse(response);
Sampler sample = response.getAggregations().get("sample");
assertThat(sample.getDocCount(), equalTo(0L));
Terms authors = sample.getAggregations().get("authors");
assertThat(authors.getBuckets().size(), equalTo(0));
}
use of org.elasticsearch.search.aggregations.bucket.terms.Terms in project elasticsearch by elastic.
the class SamplerIT method testSimpleSampler.
public void testSimpleSampler() throws Exception {
SamplerAggregationBuilder sampleAgg = sampler("sample").shardSize(100);
sampleAgg.subAggregation(terms("authors").field("author"));
SearchResponse response = client().prepareSearch("test").setSearchType(SearchType.QUERY_THEN_FETCH).setQuery(new TermQueryBuilder("genre", "fantasy")).setFrom(0).setSize(60).addAggregation(sampleAgg).execute().actionGet();
assertSearchResponse(response);
Sampler sample = response.getAggregations().get("sample");
Terms authors = sample.getAggregations().get("authors");
Collection<Bucket> testBuckets = authors.getBuckets();
long maxBooksPerAuthor = 0;
for (Terms.Bucket testBucket : testBuckets) {
maxBooksPerAuthor = Math.max(testBucket.getDocCount(), maxBooksPerAuthor);
}
assertThat(maxBooksPerAuthor, equalTo(3L));
}
Aggregations