本文整理汇总了Java中org.elasticsearch.search.aggregations.Aggregations类的典型用法代码示例。如果您正苦于以下问题:Java Aggregations类的具体用法?Java Aggregations怎么用?Java Aggregations使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Aggregations类属于org.elasticsearch.search.aggregations包,在下文中一共展示了Aggregations类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doReduce
import org.elasticsearch.search.aggregations.Aggregations; //导入依赖的package包/类
@Override
public final InternalAggregation doReduce(Aggregations aggregations, ReduceContext context) {
preCollection();
List<String> bucketsPath = AggregationPath.parse(bucketsPaths()[0]).getPathElementsAsStringList();
for (Aggregation aggregation : aggregations) {
if (aggregation.getName().equals(bucketsPath.get(0))) {
bucketsPath = bucketsPath.subList(1, bucketsPath.size());
InternalMultiBucketAggregation multiBucketsAgg = (InternalMultiBucketAggregation) aggregation;
List<? extends Bucket> buckets = multiBucketsAgg.getBuckets();
for (int i = 0; i < buckets.size(); i++) {
Bucket bucket = buckets.get(i);
Double bucketValue = BucketHelpers.resolveBucketValue(multiBucketsAgg, bucket, bucketsPath, gapPolicy);
if (bucketValue != null && !Double.isNaN(bucketValue)) {
collectBucketValue(bucket.getKeyAsString(), bucketValue);
}
}
}
}
return buildAggregation(Collections.EMPTY_LIST, metaData());
}
示例2: doReduce
import org.elasticsearch.search.aggregations.Aggregations; //导入依赖的package包/类
@Override
public final InternalAggregation doReduce(Aggregations aggregations, ReduceContext context) {
preCollection();
List<String> bucketsPath = AggregationPath.parse(bucketsPaths()[0]).getPathElementsAsStringList();
for (Aggregation aggregation : aggregations) {
if (aggregation.getName().equals(bucketsPath.get(0))) {
bucketsPath = bucketsPath.subList(1, bucketsPath.size());
InternalMultiBucketAggregation<?, ?> multiBucketsAgg = (InternalMultiBucketAggregation<?, ?>) aggregation;
List<? extends Bucket> buckets = multiBucketsAgg.getBuckets();
for (int i = 0; i < buckets.size(); i++) {
Bucket bucket = buckets.get(i);
Double bucketValue = BucketHelpers.resolveBucketValue(multiBucketsAgg, bucket, bucketsPath, gapPolicy);
if (bucketValue != null && !Double.isNaN(bucketValue)) {
collectBucketValue(bucket.getKeyAsString(), bucketValue);
}
}
}
}
return buildAggregation(Collections.emptyList(), metaData());
}
示例3: mapResults
import org.elasticsearch.search.aggregations.Aggregations; //导入依赖的package包/类
@Override
public <T> FacetedPageImpl<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
Aggregations aggregations = response.getAggregations();
Terms agg = aggregations.get(term) ;
long total = agg.getSumOfOtherDocCounts() ;
List<T> results = new ArrayList<T>();
if(agg.getBuckets()!=null && agg.getBuckets().size()>0){
for (Terms.Bucket entry : agg.getBuckets()) {
if(!StringUtils.isBlank(name) && entry.getAggregations().get(name)!=null){
TopHits topHits = entry.getAggregations().get(name);
for (SearchHit hit : topHits.getHits().getHits()) {
T data = mapEntity(hit.getSourceAsString() , hit , clazz) ;
if(data instanceof UKAgg){
((UKAgg) data).setRowcount((int) topHits.getHits().getTotalHits());
}
results.add(data) ;
}
}
}
}
return new FacetedPageImpl<T>(results, pageable, total);
}
示例4: groupByTest
import org.elasticsearch.search.aggregations.Aggregations; //导入依赖的package包/类
@Test
public void groupByTest() throws Exception {
Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/account GROUP BY gender", TEST_INDEX));
Terms gender = result.get("gender");
for(Terms.Bucket bucket : gender.getBuckets()) {
String key = bucket.getKey().toString();
long count = ((ValueCount) bucket.getAggregations().get("COUNT(*)")).getValue();
if(key.equalsIgnoreCase("m")) {
Assert.assertEquals(507, count);
}
else if(key.equalsIgnoreCase("f")) {
Assert.assertEquals(493, count);
}
else {
throw new Exception(String.format("Unexpected key. expected: m OR f. found: %s", key));
}
}
}
示例5: multipleGroupByTest
import org.elasticsearch.search.aggregations.Aggregations; //导入依赖的package包/类
@Test
public void multipleGroupByTest() throws Exception {
Set expectedAges = new HashSet<Integer>(ContiguousSet.create(Range.closed(20, 40), DiscreteDomain.integers()));
Map<String, Set<Integer>> buckets = new HashMap<>();
Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/account GROUP BY gender, terms('field'='age','size'=200,'alias'='age')", TEST_INDEX));
Terms gender = result.get("gender");
for(Terms.Bucket genderBucket : gender.getBuckets()) {
String genderKey = genderBucket.getKey().toString();
buckets.put(genderKey, new HashSet<Integer>());
Terms ageBuckets = (Terms) genderBucket.getAggregations().get("age");
for(Terms.Bucket ageBucket : ageBuckets.getBuckets()) {
buckets.get(genderKey).add(Integer.parseInt(ageBucket.getKey().toString()));
}
}
Assert.assertEquals(2, buckets.keySet().size());
Assert.assertEquals(expectedAges, buckets.get("m"));
Assert.assertEquals(expectedAges, buckets.get("f"));
}
示例6: multipleGroupBysWithSize
import org.elasticsearch.search.aggregations.Aggregations; //导入依赖的package包/类
@Test
public void multipleGroupBysWithSize() throws Exception {
Set expectedAges = new HashSet<Integer>(ContiguousSet.create(Range.closed(20, 40), DiscreteDomain.integers()));
Map<String, Set<Integer>> buckets = new HashMap<>();
Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/account GROUP BY gender, terms('alias'='ageAgg','field'='age','size'=3)", TEST_INDEX));
Terms gender = result.get("gender");
Assert.assertEquals(2,gender.getBuckets().size());
for(Terms.Bucket genderBucket : gender.getBuckets()) {
String genderKey = genderBucket.getKey().toString();
buckets.put(genderKey, new HashSet<Integer>());
Terms ageBuckets = genderBucket.getAggregations().get("ageAgg");
Assert.assertEquals(3,ageBuckets.getBuckets().size());
}
}
示例7: testSimpleSubAggregations
import org.elasticsearch.search.aggregations.Aggregations; //导入依赖的package包/类
@Test
public void testSimpleSubAggregations() throws Exception {
final String query = String.format("SELECT /*! DOCS_WITH_AGGREGATION(10) */ * FROM %s/account GROUP BY (gender), (state) ", TEST_INDEX);
SqlElasticSearchRequestBuilder select = getSearchRequestBuilder(query);
SearchResponse response = (SearchResponse) select.get();
Aggregations result = response.getAggregations();
Terms gender = result.get("gender");
for(Terms.Bucket genderBucket : gender.getBuckets()) {
String genderKey = genderBucket.getKey().toString();
Assert.assertTrue("Gender should be m or f", genderKey.equals("m") || genderKey.equals("f"));
}
Assert.assertEquals(2, gender.getBuckets().size());
Terms state = result.get("state");
for(Terms.Bucket stateBucket : state.getBuckets()) {
if(stateBucket.getKey().toString().equalsIgnoreCase("ak")) {
Assert.assertTrue("There are 22 entries for state ak", stateBucket.getDocCount() == 22);
}
}
Assert.assertEquals(response.getHits().totalHits(), 1000);
Assert.assertEquals(response.getHits().hits().length, 10);
}
示例8: groupByOnNestedFieldTest
import org.elasticsearch.search.aggregations.Aggregations; //导入依赖的package包/类
@Test
public void groupByOnNestedFieldTest() throws Exception {
Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/nestedType GROUP BY nested(message.info)", TEST_INDEX));
InternalNested nested = result.get("[email protected]");
Terms infos = nested.getAggregations().get("message.info");
Assert.assertEquals(3,infos.getBuckets().size());
for(Terms.Bucket bucket : infos.getBuckets()) {
String key = bucket.getKey().toString();
long count = ((ValueCount) bucket.getAggregations().get("COUNT(*)")).getValue();
if(key.equalsIgnoreCase("a")) {
Assert.assertEquals(2, count);
}
else if(key.equalsIgnoreCase("c")) {
Assert.assertEquals(2, count);
}
else if(key.equalsIgnoreCase("b")) {
Assert.assertEquals(1, count);
}
else {
throw new Exception(String.format("Unexpected key. expected: a OR b OR c . found: %s", key));
}
}
}
示例9: groupByTestWithFilter
import org.elasticsearch.search.aggregations.Aggregations; //导入依赖的package包/类
@Test
public void groupByTestWithFilter() throws Exception {
Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/account GROUP BY filter(gender='m'),gender", TEST_INDEX));
InternalFilter filter = result.get("filter(gender = 'm')@FILTER");
Terms gender = filter.getAggregations().get("gender");
for(Terms.Bucket bucket : gender.getBuckets()) {
String key = bucket.getKey().toString();
long count = ((ValueCount) bucket.getAggregations().get("COUNT(*)")).getValue();
if(key.equalsIgnoreCase("m")) {
Assert.assertEquals(507, count);
}
else {
throw new Exception(String.format("Unexpected key. expected: only m. found: %s", key));
}
}
}
示例10: groupByOnNestedFieldWithFilterTest
import org.elasticsearch.search.aggregations.Aggregations; //导入依赖的package包/类
@Test
public void groupByOnNestedFieldWithFilterTest() throws Exception {
Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/nestedType GROUP BY nested(message.info),filter('myFilter',message.info = 'a')", TEST_INDEX));
InternalNested nested = result.get("[email protected]");
InternalFilter filter = nested.getAggregations().get("[email protected]");
Terms infos = filter.getAggregations().get("message.info");
Assert.assertEquals(1,infos.getBuckets().size());
for(Terms.Bucket bucket : infos.getBuckets()) {
String key = bucket.getKey().toString();
long count = ((ValueCount) bucket.getAggregations().get("COUNT(*)")).getValue();
if(key.equalsIgnoreCase("a")) {
Assert.assertEquals(2, count);
}
else {
throw new Exception(String.format("Unexpected key. expected: only a . found: %s", key));
}
}
}
示例11: histogramOnNestedField
import org.elasticsearch.search.aggregations.Aggregations; //导入依赖的package包/类
@Test
public void histogramOnNestedField() throws Exception {
Aggregations result = query(String.format("select count(*) from %s/nestedType group by histogram('field'='message.dayOfWeek','nested'='message','interval'='2' , 'alias' = 'someAlias' )", TEST_INDEX));
InternalNested nested = result.get("[email protected]");
Histogram histogram = nested.getAggregations().get("someAlias");
for(Histogram.Bucket bucket : histogram.getBuckets()){
long count = ((ValueCount) bucket.getAggregations().get("COUNT(*)")).getValue();
String key = ((Double)bucket.getKey()).intValue()+"";
if(key.equals("0") || key.equals("4")){
Assert.assertEquals(2,count);
}
else if (key.equals("2")){
Assert.assertEquals(1,count);
}
else{
Assert.assertTrue("only 0 2 4 keys are allowed got:" + key,false);
}
}
}
示例12: reverseToRootGroupByOnNestedFieldWithFilterTestWithReverseNestedAndEmptyPath
import org.elasticsearch.search.aggregations.Aggregations; //导入依赖的package包/类
@Test
public void reverseToRootGroupByOnNestedFieldWithFilterTestWithReverseNestedAndEmptyPath() throws Exception {
Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/nestedType GROUP BY nested(message.info),filter('myFilter',message.info = 'a'),reverse_nested(someField,'')", TEST_INDEX));
InternalNested nested = result.get("[email protected]");
InternalFilter filter = nested.getAggregations().get("[email protected]");
Terms infos = filter.getAggregations().get("message.info");
Assert.assertEquals(1,infos.getBuckets().size());
for(Terms.Bucket bucket : infos.getBuckets()) {
InternalReverseNested reverseNested = bucket.getAggregations().get("[email protected]");
Terms terms = reverseNested.getAggregations().get("someField");
Terms.Bucket internalBucket = terms.getBuckets().get(0);
long count = ((ValueCount) internalBucket.getAggregations().get("COUNT(*)")).getValue();
String key = internalBucket.getKey().toString();
if(key.equalsIgnoreCase("b")) {
Assert.assertEquals(2, count);
}
else {
throw new Exception(String.format("Unexpected key. expected: only a . found: %s", key));
}
}
}
示例13: reverseToRootGroupByOnNestedFieldWithFilterTestWithReverseNestedNoPath
import org.elasticsearch.search.aggregations.Aggregations; //导入依赖的package包/类
@Test
public void reverseToRootGroupByOnNestedFieldWithFilterTestWithReverseNestedNoPath() throws Exception {
Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/nestedType GROUP BY nested(message.info),filter('myFilter',message.info = 'a'),reverse_nested(someField)", TEST_INDEX));
InternalNested nested = result.get("[email protected]");
InternalFilter filter = nested.getAggregations().get("[email protected]");
Terms infos = filter.getAggregations().get("message.info");
Assert.assertEquals(1,infos.getBuckets().size());
for(Terms.Bucket bucket : infos.getBuckets()) {
InternalReverseNested reverseNested = bucket.getAggregations().get("[email protected]");
Terms terms = reverseNested.getAggregations().get("someField");
Terms.Bucket internalBucket = terms.getBuckets().get(0);
long count = ((ValueCount) internalBucket.getAggregations().get("COUNT(*)")).getValue();
String key = internalBucket.getKey().toString();
if(key.equalsIgnoreCase("b")) {
Assert.assertEquals(2, count);
}
else {
throw new Exception(String.format("Unexpected key. expected: only a . found: %s", key));
}
}
}
示例14: reverseAnotherNestedGroupByOnNestedFieldWithFilterTestWithReverseNestedNoPath
import org.elasticsearch.search.aggregations.Aggregations; //导入依赖的package包/类
@Test
public void reverseAnotherNestedGroupByOnNestedFieldWithFilterTestWithReverseNestedNoPath() throws Exception {
Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/nestedType GROUP BY nested(message.info),filter('myFilter',message.info = 'a'),reverse_nested(comment.data,'~comment')", TEST_INDEX));
InternalNested nested = result.get("[email protected]");
InternalFilter filter = nested.getAggregations().get("[email protected]");
Terms infos = filter.getAggregations().get("message.info");
Assert.assertEquals(1,infos.getBuckets().size());
for(Terms.Bucket bucket : infos.getBuckets()) {
InternalReverseNested reverseNested = bucket.getAggregations().get("[email protected]_REVERSED");
InternalNested innerNested = reverseNested.getAggregations().get("[email protected]");
Terms terms = innerNested.getAggregations().get("comment.data");
Terms.Bucket internalBucket = terms.getBuckets().get(0);
long count = ((ValueCount) internalBucket.getAggregations().get("COUNT(*)")).getValue();
String key = internalBucket.getKey().toString();
if(key.equalsIgnoreCase("ab")) {
Assert.assertEquals(2, count);
}
else {
throw new Exception(String.format("Unexpected key. expected: only a . found: %s", key));
}
}
}
示例15: parse
import org.elasticsearch.search.aggregations.Aggregations; //导入依赖的package包/类
@Override
public InstanceData<EdmEntityType, AbstractEntityCollection> parse(SearchResponse response,
ElasticEdmEntitySet entitySet) {
ElasticEdmEntityType entityType = entitySet.getEntityType();
Entity entity = new Entity();
Aggregations aggs = response.getAggregations();
if (aggs != null) {
aggs.asList().stream().filter(SingleValue.class::isInstance)
.map(SingleValue.class::cast)
.map(aggr -> createProperty(aggr.getName(), aggr.value(), entityType))
.forEach(entity::addProperty);
}
addCountIfNeeded(entity, response.getHits().getTotalHits());
EntityCollection entities = new EntityCollection();
entities.getEntities().add(entity);
return new InstanceData<>(entityType, entities);
}