use of org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket 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.Bucket 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));
}
use of org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket in project elasticsearch by elastic.
the class SamplerIT 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;
}
}
use of org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket in project elasticsearch by elastic.
the class LongTermsIT method testSingleValuedFieldOrderedByMultiValueSubAggregationDesc.
public void testSingleValuedFieldOrderedByMultiValueSubAggregationDesc() throws Exception {
boolean asc = false;
SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("terms").field(SINGLE_VALUED_FIELD_NAME).collectMode(randomFrom(SubAggCollectionMode.values())).order(Terms.Order.aggregation("stats.avg", asc)).subAggregation(stats("stats").field(SINGLE_VALUED_FIELD_NAME))).execute().actionGet();
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 = 4; i >= 0; i--) {
Terms.Bucket bucket = terms.getBucketByKey("" + i);
assertThat(bucket, notNullValue());
assertThat(key(bucket), equalTo("" + i));
assertThat(bucket.getDocCount(), equalTo(1L));
Stats stats = bucket.getAggregations().get("stats");
assertThat(stats, notNullValue());
assertThat(stats.getMax(), equalTo((double) i));
}
}
use of org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket in project elasticsearch by elastic.
the class LongTermsIT method runTestFieldWithPartitionedFiltering.
private void runTestFieldWithPartitionedFiltering(String field) throws Exception {
// Find total number of unique terms
SearchResponse allResponse = client().prepareSearch("idx").setTypes("type").addAggregation(terms("terms").field(field).collectMode(randomFrom(SubAggCollectionMode.values()))).execute().actionGet();
assertSearchResponse(allResponse);
Terms terms = allResponse.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
int expectedCardinality = terms.getBuckets().size();
// Gather terms using partitioned aggregations
final int numPartitions = randomIntBetween(2, 4);
Set<Number> foundTerms = new HashSet<>();
for (int partition = 0; partition < numPartitions; partition++) {
SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("terms").field(field).includeExclude(new IncludeExclude(partition, numPartitions)).collectMode(randomFrom(SubAggCollectionMode.values()))).execute().actionGet();
assertSearchResponse(response);
terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
for (Bucket bucket : terms.getBuckets()) {
assertFalse(foundTerms.contains(bucket.getKeyAsNumber()));
foundTerms.add(bucket.getKeyAsNumber());
}
}
assertEquals(expectedCardinality, foundTerms.size());
}
Aggregations