use of org.elasticsearch.search.aggregations.bucket.terms.Terms in project elasticsearch by elastic.
the class RangeIT method testRangeAsSubAggregation.
public void testRangeAsSubAggregation() throws Exception {
SearchResponse response = client().prepareSearch("idx").addAggregation(terms("terms").field(MULTI_VALUED_FIELD_NAME).size(100).collectMode(randomFrom(SubAggCollectionMode.values())).subAggregation(range("range").field(SINGLE_VALUED_FIELD_NAME).addUnboundedTo(3).addRange(3, 6).addUnboundedFrom(6))).execute().actionGet();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getBuckets().size(), equalTo(numDocs + 1));
for (int i = 1; i < numDocs + 2; ++i) {
Terms.Bucket bucket = terms.getBucketByKey("" + i);
assertThat(bucket, notNullValue());
final long docCount = i == 1 || i == numDocs + 1 ? 1 : 2;
assertThat(bucket.getDocCount(), equalTo(docCount));
Range range = bucket.getAggregations().get("range");
List<? extends Bucket> buckets = range.getBuckets();
Range.Bucket rangeBucket = buckets.get(0);
assertThat(rangeBucket.getKey(), equalTo("*-3.0"));
assertThat(rangeBucket.getKeyAsString(), equalTo("*-3.0"));
assertThat(rangeBucket, notNullValue());
assertThat(rangeBucket.getFromAsString(), nullValue());
assertThat(rangeBucket.getToAsString(), equalTo("3.0"));
if (i == 1 || i == 3) {
assertThat(rangeBucket.getDocCount(), equalTo(1L));
} else if (i == 2) {
assertThat(rangeBucket.getDocCount(), equalTo(2L));
} else {
assertThat(rangeBucket.getDocCount(), equalTo(0L));
}
rangeBucket = buckets.get(1);
assertThat(rangeBucket.getKey(), equalTo("3.0-6.0"));
assertThat(rangeBucket.getKeyAsString(), equalTo("3.0-6.0"));
assertThat(rangeBucket, notNullValue());
assertThat(rangeBucket.getFromAsString(), equalTo("3.0"));
assertThat(rangeBucket.getToAsString(), equalTo("6.0"));
if (i == 3 || i == 6) {
assertThat(rangeBucket.getDocCount(), equalTo(1L));
} else if (i == 4 || i == 5) {
assertThat(rangeBucket.getDocCount(), equalTo(2L));
} else {
assertThat(rangeBucket.getDocCount(), equalTo(0L));
}
rangeBucket = buckets.get(2);
assertThat(rangeBucket.getKey(), equalTo("6.0-*"));
assertThat(rangeBucket.getKeyAsString(), equalTo("6.0-*"));
assertThat(rangeBucket, notNullValue());
assertThat(rangeBucket.getFromAsString(), equalTo("6.0"));
assertThat(rangeBucket.getToAsString(), nullValue());
if (i == 6 || i == numDocs + 1) {
assertThat(rangeBucket.getDocCount(), equalTo(1L));
} else if (i < 6) {
assertThat(rangeBucket.getDocCount(), equalTo(0L));
} else {
assertThat(rangeBucket.getDocCount(), equalTo(2L));
}
}
}
use of org.elasticsearch.search.aggregations.bucket.terms.Terms in project elasticsearch by elastic.
the class NaNSortingIT method assertCorrectlySorted.
private void assertCorrectlySorted(Terms terms, boolean asc, SubAggregation agg) {
assertThat(terms, notNullValue());
double previousValue = asc ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY;
for (Terms.Bucket bucket : terms.getBuckets()) {
Aggregation sub = bucket.getAggregations().get(agg.name);
double value = agg.getValue(sub);
assertTrue(Comparators.compareDiscardNaN(previousValue, value, asc) <= 0);
previousValue = value;
}
}
use of org.elasticsearch.search.aggregations.bucket.terms.Terms in project elasticsearch by elastic.
the class NestedIT method testNestedSameDocIdProcessedMultipleTime.
public void testNestedSameDocIdProcessedMultipleTime() throws Exception {
assertAcked(prepareCreate("idx4").setSettings(Settings.builder().put(SETTING_NUMBER_OF_SHARDS, 1).put(SETTING_NUMBER_OF_REPLICAS, 0)).addMapping("product", "categories", "type=keyword", "name", "type=text", "property", "type=nested"));
ensureGreen("idx4");
client().prepareIndex("idx4", "product", "1").setSource(jsonBuilder().startObject().field("name", "product1").array("categories", "1", "2", "3", "4").startArray("property").startObject().field("id", 1).endObject().startObject().field("id", 2).endObject().startObject().field("id", 3).endObject().endArray().endObject()).get();
client().prepareIndex("idx4", "product", "2").setSource(jsonBuilder().startObject().field("name", "product2").array("categories", "1", "2").startArray("property").startObject().field("id", 1).endObject().startObject().field("id", 5).endObject().startObject().field("id", 4).endObject().endArray().endObject()).get();
refresh();
SearchResponse response = client().prepareSearch("idx4").setTypes("product").addAggregation(terms("category").field("categories").subAggregation(nested("property", "property").subAggregation(terms("property_id").field("property.id")))).get();
assertNoFailures(response);
assertHitCount(response, 2);
Terms category = response.getAggregations().get("category");
assertThat(category.getBuckets().size(), equalTo(4));
Terms.Bucket bucket = category.getBucketByKey("1");
assertThat(bucket.getDocCount(), equalTo(2L));
Nested property = bucket.getAggregations().get("property");
assertThat(property.getDocCount(), equalTo(6L));
Terms propertyId = property.getAggregations().get("property_id");
assertThat(propertyId.getBuckets().size(), equalTo(5));
assertThat(propertyId.getBucketByKey("1").getDocCount(), equalTo(2L));
assertThat(propertyId.getBucketByKey("2").getDocCount(), equalTo(1L));
assertThat(propertyId.getBucketByKey("3").getDocCount(), equalTo(1L));
assertThat(propertyId.getBucketByKey("4").getDocCount(), equalTo(1L));
assertThat(propertyId.getBucketByKey("5").getDocCount(), equalTo(1L));
bucket = category.getBucketByKey("2");
assertThat(bucket.getDocCount(), equalTo(2L));
property = bucket.getAggregations().get("property");
assertThat(property.getDocCount(), equalTo(6L));
propertyId = property.getAggregations().get("property_id");
assertThat(propertyId.getBuckets().size(), equalTo(5));
assertThat(propertyId.getBucketByKey("1").getDocCount(), equalTo(2L));
assertThat(propertyId.getBucketByKey("2").getDocCount(), equalTo(1L));
assertThat(propertyId.getBucketByKey("3").getDocCount(), equalTo(1L));
assertThat(propertyId.getBucketByKey("4").getDocCount(), equalTo(1L));
assertThat(propertyId.getBucketByKey("5").getDocCount(), equalTo(1L));
bucket = category.getBucketByKey("3");
assertThat(bucket.getDocCount(), equalTo(1L));
property = bucket.getAggregations().get("property");
assertThat(property.getDocCount(), equalTo(3L));
propertyId = property.getAggregations().get("property_id");
assertThat(propertyId.getBuckets().size(), equalTo(3));
assertThat(propertyId.getBucketByKey("1").getDocCount(), equalTo(1L));
assertThat(propertyId.getBucketByKey("2").getDocCount(), equalTo(1L));
assertThat(propertyId.getBucketByKey("3").getDocCount(), equalTo(1L));
bucket = category.getBucketByKey("4");
assertThat(bucket.getDocCount(), equalTo(1L));
property = bucket.getAggregations().get("property");
assertThat(property.getDocCount(), equalTo(3L));
propertyId = property.getAggregations().get("property_id");
assertThat(propertyId.getBuckets().size(), equalTo(3));
assertThat(propertyId.getBucketByKey("1").getDocCount(), equalTo(1L));
assertThat(propertyId.getBucketByKey("2").getDocCount(), equalTo(1L));
assertThat(propertyId.getBucketByKey("3").getDocCount(), equalTo(1L));
}
use of org.elasticsearch.search.aggregations.bucket.terms.Terms in project elasticsearch by elastic.
the class ShardReduceIT method testStringTerms.
public void testStringTerms() throws Exception {
SearchResponse response = client().prepareSearch("idx").setQuery(QueryBuilders.matchAllQuery()).addAggregation(terms("terms").field("term-s").collectMode(randomFrom(SubAggCollectionMode.values())).subAggregation(dateHistogram("histo").field("date").dateHistogramInterval(DateHistogramInterval.DAY).minDocCount(0))).execute().actionGet();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
Histogram histo = terms.getBucketByKey("term").getAggregations().get("histo");
assertThat(histo.getBuckets().size(), equalTo(4));
}
use of org.elasticsearch.search.aggregations.bucket.terms.Terms in project elasticsearch by elastic.
the class ShardSizeTermsIT method testNoShardSizeString.
public void testNoShardSizeString() throws Exception {
createIdx("type=keyword");
indexData();
SearchResponse response = client().prepareSearch("idx").setTypes("type").setQuery(matchAllQuery()).addAggregation(terms("keys").field("key").size(3).collectMode(randomFrom(SubAggCollectionMode.values())).order(Terms.Order.count(false))).execute().actionGet();
Terms terms = response.getAggregations().get("keys");
Collection<Terms.Bucket> buckets = terms.getBuckets();
assertThat(buckets.size(), equalTo(3));
Map<String, Long> expected = new HashMap<>();
expected.put("1", 8L);
expected.put("3", 8L);
expected.put("2", 5L);
for (Terms.Bucket bucket : buckets) {
assertThat(bucket.getDocCount(), equalTo(expected.get(bucket.getKeyAsString())));
}
}
Aggregations