org.elasticsearch.search.aggregations.Aggregations Java Examples

The following examples show how to use org.elasticsearch.search.aggregations.Aggregations. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UKAggTopResultExtractor.java    From youkefu with Apache License 2.0 7 votes vote down vote up
@Override
public <T> AggregatedPage<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 (int i = pageable.getPageNumber()*pageable.getPageSize();i<agg.getBuckets().size()  ; i++) {
			Terms.Bucket entry = agg.getBuckets().get(i) ;
			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 AggregatedPageImpl<T>(results, pageable, total);
}
 
Example #2
Source File: ChildrenAggregationMain.java    From elasticsearch-pool with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    RestHighLevelClient client = HighLevelClient.getInstance();
    try{

        ChildrenAggregationBuilder aggregationBuilder = new ChildrenAggregationBuilder("utm","histogram");
        aggregationBuilder.field("utm");

        SearchRequest searchRequest = new SearchRequest("serverlog_20180710");//限定index
        searchRequest.types("log");//限定type

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.aggregation(aggregationBuilder);
        searchRequest.source(searchSourceBuilder);

        SearchResponse searchResponse = client.search(searchRequest);
        System.out.println(searchResponse);
        Aggregations aggregations = searchResponse.getAggregations();
        List<Aggregation> aggregationList = aggregations.asList();
        for(Aggregation each: aggregationList){
            System.out.println(each);
        }
    }finally{
        HighLevelClient.close();
    }
}
 
Example #3
Source File: AggregationTest.java    From elasticsearch-sql with Apache License 2.0 6 votes vote down vote up
@Test
public void groupByTest() throws Exception {
	Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/account GROUP BY gender", TEST_INDEX_ACCOUNT));
	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));
		}
	}
}
 
Example #4
Source File: ElasticsearchDataModel.java    From elasticsearch-taste with Apache License 2.0 6 votes vote down vote up
protected synchronized void loadValueStats() {
    if (stats != null) {
        return;
    }
    // TODO join userQueryBuilder and itemQueryBuilder
    final SearchResponse response = client
            .prepareSearch(preferenceIndex)
            .setTypes(preferenceType)
            .setQuery(getLastAccessedFilterQuery())
            .setSize(0)
            .addAggregation(
                    AggregationBuilders.stats(valueField).field(valueField))
            .execute().actionGet();
    final Aggregations aggregations = response.getAggregations();
    stats = aggregations.get(valueField);
}
 
Example #5
Source File: AggregationTest.java    From elasticsearch-sql with Apache License 2.0 6 votes vote down vote up
@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_ACCOUNT);

       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().getTotalHits().value, 1000);
	Assert.assertEquals(response.getHits().getHits().length, 10);
}
 
Example #6
Source File: AggregationServiceImpl.java    From microservices-platform with Apache License 2.0 6 votes vote down vote up
/**
 * 赋值天趋势统计
 */
private void setStatDate(Map<String, Object> result, Aggregations aggregations) {
    ParsedDateHistogram agg = aggregations.get("statDate");
    List<String> items = new ArrayList<>();
    List<Long> uv = new ArrayList<>();
    List<Long> pv = new ArrayList<>();
    Cardinality cardinality;
    for (Histogram.Bucket bucket : agg.getBuckets()) {
        items.add(getTimeByDatetimeStr(bucket.getKeyAsString()));
        pv.add(bucket.getDocCount());

        cardinality = bucket.getAggregations().get("uv");
        uv.add(cardinality.getValue());
    }
    result.put("statDate_items", items);
    result.put("statDate_uv", uv);
    result.put("statDate_pv", pv);
}
 
Example #7
Source File: JdbcResponseExtractor.java    From elasticsearch-sql with MIT License 6 votes vote down vote up
private void parseAggregations(Aggregations aggregations, Map<String, Object> aggMap, String parent) {
    for (Aggregation aggregation : aggregations) {
        if (aggregation instanceof ParsedNested) {
            parseNestedAggregation(aggregation, aggMap);
        } else if (aggregation instanceof ParsedComposite) {
            parseCompositeAggregation(aggregation, aggMap, parent);
        } else if (aggregation instanceof ParsedTerms) {
            parseTermsAggregation(aggregation, aggMap, parent);
        } else if (aggregation instanceof ParsedTopHits) {
            parseTopHitsAggregation(aggregation, aggMap);
        } else if (aggregation instanceof ParsedCardinality) {
            parseCardinalityAggregation(aggregation, aggMap);
        } else if (aggregation instanceof ParsedRange) {
            parseRangeAggregation(aggregation, aggMap);
        } else if (aggregation instanceof ParsedGeoBounds) {
            parseGeoBoundAggregation(aggregation, aggMap);
        }
    }
}
 
Example #8
Source File: StatsTrendAction.java    From foxtrot with Apache License 2.0 6 votes vote down vote up
private List<BucketResponse<List<StatsTrendValue>>> buildNestedTrendStats(
        List<String> nesting,
        Aggregations aggregations) {
    final String field = nesting.get(0);
    final List<String> remainingFields = (nesting.size() > 1)
                                         ? nesting.subList(1, nesting.size())
                                         : new ArrayList<>();
    Terms terms = aggregations.get(Utils.sanitizeFieldForAggregation(field));
    List<BucketResponse<List<StatsTrendValue>>> bucketResponses = Lists.newArrayList();
    for (Terms.Bucket bucket : terms.getBuckets()) {
        BucketResponse<List<StatsTrendValue>> bucketResponse = new BucketResponse<>();
        bucketResponse.setKey(String.valueOf(bucket.getKey()));
        if (nesting.size() == 1) {
            bucketResponse.setResult(buildStatsTrendValue(getParameter().getField(), bucket.getAggregations()));
        }
        else {
            bucketResponse.setBuckets(buildNestedTrendStats(remainingFields, bucket.getAggregations()));
        }
        bucketResponses.add(bucketResponse);
    }
    return bucketResponses;
}
 
Example #9
Source File: ElasticsearchSearchDao.java    From metron with Apache License 2.0 6 votes vote down vote up
private List<GroupResult> getGroupResults(GroupRequest groupRequest, int index, Aggregations aggregations, Map<String, FieldType> commonColumnMetadata) {
  List<Group> groups = groupRequest.getGroups();
  String field = groups.get(index).getField();
  List<GroupResult> searchResultGroups = new ArrayList<>();
  if(aggregations != null) {
    Terms terms = aggregations.get(getGroupByAggregationName(field));
    for (Bucket bucket : terms.getBuckets()) {
      GroupResult groupResult = new GroupResult();
      groupResult.setKey(formatKey(bucket.getKey(), commonColumnMetadata.get(field)));
      groupResult.setTotal(bucket.getDocCount());
      Optional<String> scoreField = groupRequest.getScoreField();
      if (scoreField.isPresent()) {
        Sum score = bucket.getAggregations().get(getSumAggregationName(scoreField.get()));
        groupResult.setScore(score.getValue());
      }
      if (index < groups.size() - 1) {
        groupResult.setGroupedBy(groups.get(index + 1).getField());
        groupResult.setGroupResults(getGroupResults(groupRequest, index + 1, bucket.getAggregations(), commonColumnMetadata));
      }
      searchResultGroups.add(groupResult);
    }
  }
  return searchResultGroups;
}
 
Example #10
Source File: AggregationTest.java    From elasticsearch-sql with Apache License 2.0 6 votes vote down vote up
@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_NESTED_TYPE));
    InternalNested nested = result.get("message.info@NESTED");
    InternalFilter filter = nested.getAggregations().get("myFilter@FILTER");
    Terms infos = filter.getAggregations().get("message.info");
    Assert.assertEquals(1,infos.getBuckets().size());
    for(Terms.Bucket bucket : infos.getBuckets()) {
        InternalReverseNested reverseNested = bucket.getAggregations().get("comment.data@NESTED_REVERSED");
        InternalNested innerNested = reverseNested.getAggregations().get("comment.data@NESTED");
        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));
        }
    }
}
 
Example #11
Source File: AggregationTest.java    From elasticsearch-sql with Apache License 2.0 6 votes vote down vote up
@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_ACCOUNT));
    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());

    }


}
 
Example #12
Source File: AggregationTest.java    From elasticsearch-sql with Apache License 2.0 6 votes vote down vote up
@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_NESTED_TYPE));
    InternalNested nested = result.get("message.info@NESTED");
    InternalFilter filter = nested.getAggregations().get("myFilter@FILTER");
    Terms infos = filter.getAggregations().get("message.info");
    Assert.assertEquals(1,infos.getBuckets().size());
    for(Terms.Bucket bucket : infos.getBuckets()) {
        InternalReverseNested reverseNested = bucket.getAggregations().get("someField@NESTED");
        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));
        }
    }
}
 
Example #13
Source File: FactSearchManager.java    From act-platform with ISC License 6 votes vote down vote up
private Aggregation resolveChildAggregation(Aggregations aggregations, String targetAggregationName) {
  if (aggregations == null) return null;

  for (Aggregation aggregation : aggregations) {
    // Check if 'aggregation' is already the target aggregation.
    if (aggregation.getName().equals(targetAggregationName)) {
      return aggregation;
    }

    // Otherwise check all sub aggregations if applicable.
    if (HasAggregations.class.isAssignableFrom(aggregation.getClass())) {
      Aggregation target = resolveChildAggregation(HasAggregations.class.cast(aggregation).getAggregations(), targetAggregationName);
      if (target != null) return target;
    }
  }

  // Couldn't find target aggregation.
  return null;
}
 
Example #14
Source File: StatsAction.java    From foxtrot with Apache License 2.0 6 votes vote down vote up
private StatsResponse buildResponse(StatsRequest request, Aggregations aggregations) {
    // First build root level stats value
    StatsValue statsValue = buildStatsValue(request.getField(), aggregations);

    // Now build nested stats if present
    List<BucketResponse<StatsValue>> buckets = null;
    if (!CollectionUtils.isNullOrEmpty(request.getNesting())) {
        buckets = buildNestedStats(request.getNesting(), aggregations);
    }

    StatsResponse statsResponse = new StatsResponse();
    statsResponse.setResult(statsValue);
    statsResponse.setBuckets(buckets);

    return statsResponse;
}
 
Example #15
Source File: ElasticsearchHelper.java    From herd with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the aggregation that is associated with the specified name. This method also validates that the retrieved aggregation exists.
 *
 * @param searchResponse the response of the search request
 * @param aggregationName the name of the aggregation
 *
 * @return the aggregation
 */
public Terms getAggregation(SearchResponse searchResponse, String aggregationName)
{
    // Retrieve the aggregations from the search response.
    Aggregations aggregations = getAggregationsFromSearchResponse(searchResponse);

    // Retrieve the specified aggregation.
    Terms aggregation = aggregations.get(aggregationName);

    // Fail if retrieved aggregation is null.
    if (aggregation == null)
    {
        // Log the error along with the search response contents.
        LOGGER.error("Failed to retrieve \"{}\" aggregation from the search response. searchResponse={}", aggregationName,
            jsonHelper.objectToJson(searchResponse));

        // Throw an exception.
        throw new IllegalStateException("Invalid search result.");
    }

    return aggregation;
}
 
Example #16
Source File: ElasticsearchHelper.java    From herd with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a representation of a set of aggregations from the nested aggregation. This method also validates that the retrieved object is not null.
 *
 * @param nestedAggregation the nested aggregation
 * @param searchResponse the response of the search request
 *
 * @return the aggregations
 */
protected Aggregations getAggregationsFromNestedAggregation(Nested nestedAggregation, SearchResponse searchResponse)
{
    // Retrieve the aggregations.
    Aggregations aggregations = nestedAggregation.getAggregations();

    // Fail if the retrieved object is null.
    if (aggregations == null)
    {
        // Log the error along with the nested aggregation contents.
        LOGGER.error("Failed to retrieve aggregations from the nested aggregation. searchResponse={} nestedAggregation={}",
            jsonHelper.objectToJson(searchResponse), jsonHelper.objectToJson(nestedAggregation));

        // Throw an exception.
        throw new IllegalStateException("Invalid search result.");
    }

    return aggregations;
}
 
Example #17
Source File: AggregationTest.java    From elasticsearch-sql with Apache License 2.0 6 votes vote down vote up
@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_ACCOUNT));
	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"));
}
 
Example #18
Source File: ElasticsearchHelper.java    From herd with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a representation of a set of aggregations from the search response. This method also validates that the retrieved object is not null.
 *
 * @param searchResponse the response of the search request
 *
 * @return the aggregations
 */
protected Aggregations getAggregationsFromSearchResponse(SearchResponse searchResponse)
{
    // Retrieve the aggregations.
    Aggregations aggregations = searchResponse.getAggregations();

    // Fail if the retrieved object is null.
    if (aggregations == null)
    {
        // Log the error along with the search response contents.
        LOGGER.error("Failed to retrieve aggregations from the search response. searchResponse={}", jsonHelper.objectToJson(searchResponse));

        // Throw an exception.
        throw new IllegalStateException("Invalid search result.");
    }

    return aggregations;
}
 
Example #19
Source File: AggregationTest.java    From elasticsearch-sql with Apache License 2.0 6 votes vote down vote up
@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_NESTED_TYPE));
    InternalNested nested = result.get("message.info@NESTED");
    InternalFilter filter = nested.getAggregations().get("myFilter@FILTER");
    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));
        }
    }
}
 
Example #20
Source File: AggregationTest.java    From elasticsearch-sql with Apache License 2.0 6 votes vote down vote up
@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_NESTED_TYPE));
    InternalNested nested  = result.get("message@NESTED");
    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);
        }
    }


}
 
Example #21
Source File: ElasticSearchHelperTest.java    From herd with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetNestedTagTagIndexSearchResponseDtoSearchResponseParameter()
{
    SearchResponse searchResponse = mock(SearchResponse.class);
    Aggregations aggregations = mock(Aggregations.class);

    when(searchResponse.getAggregations()).thenReturn(aggregations);

    Nested nestedAggregation = mock(Nested.class);
    when(aggregations.get(TAG_FACET_AGGS)).thenReturn(nestedAggregation);

    Aggregations aggregationAggregations = mock(Aggregations.class);
    when(nestedAggregation.getAggregations()).thenReturn(aggregationAggregations);

    Terms subAggregation = mock(Terms.class);
    when(aggregationAggregations.get(TAGTYPE_CODE_AGGREGATION)).thenReturn(subAggregation);

    List<TagTypeIndexSearchResponseDto> result = elasticsearchHelper.getNestedTagTagIndexSearchResponseDto(searchResponse);
    assertThat("Result is null.", result, is(notNullValue()));
}
 
Example #22
Source File: ElasticSearchHelperTest.java    From herd with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetResultTypeIndexSearchResponseDto()
{
    SearchResponse searchResponse = mock(SearchResponse.class);
    Terms terms = mock(Terms.class);
    Aggregations aggregations = mock(Aggregations.class);
    Terms.Bucket bucket = mock(Terms.Bucket.class);
    List<Terms.Bucket> buckets = Collections.singletonList(bucket);

    when(searchResponse.getAggregations()).thenReturn(aggregations);
    when(aggregations.get(RESULT_TYPE_AGGS)).thenReturn(terms);
    when(terms.getBuckets()).thenReturn(buckets);
    when(bucket.getKeyAsString()).thenReturn(TAG_CODE);
    when(bucket.getDocCount()).thenReturn(TAG_COUNT);

    List<ResultTypeIndexSearchResponseDto> expectedList = new ArrayList<>();
    expectedList.add(new ResultTypeIndexSearchResponseDto(TAG_CODE, TAG_COUNT, TAG_CODE));
    List<ResultTypeIndexSearchResponseDto> resultList = elasticsearchHelper.getResultTypeIndexSearchResponseDto(searchResponse);

    assertEquals(expectedList, resultList);
}
 
Example #23
Source File: AggregationTest.java    From elasticsearch-sql with Apache License 2.0 6 votes vote down vote up
@Test
public void groupByTestWithFilter() throws Exception {
    Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/account GROUP BY filter(gender='m'),gender", TEST_INDEX_ACCOUNT));
    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));
        }
    }
}
 
Example #24
Source File: BucketMetricsPipelineAggregator.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example #25
Source File: AggregateResponseParser.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
private Cardinality getDistinctAggregation(Aggregations aggs, Attribute attr) {
  Aggregation agg = aggs.get(attr.getName() + FieldConstants.AGGREGATION_DISTINCT_POSTFIX);
  if (agg == null) {
    throw new RuntimeException("Missing cardinality aggregation");
  }
  if (!(agg instanceof Cardinality)) {
    throw new RuntimeException("Aggregation is not a cardinality aggregation");
  }
  return (Cardinality) agg;
}
 
Example #26
Source File: AggregationTest.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
@Test
public void statsTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException {
	Aggregations result = query(String.format("SELECT STATS(age) FROM %s/account", TEST_INDEX_ACCOUNT));
	Stats stats = result.get("STATS(age)");
	Assert.assertEquals(1000, stats.getCount());
	assertThat(stats.getSum(), equalTo(30171.0));
	assertThat(stats.getMin(), equalTo(20.0));
	assertThat(stats.getMax(), equalTo(40.0));
	assertThat(stats.getAvg(), equalTo(30.171));
}
 
Example #27
Source File: AggregationTest.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
@Test
public void topHitTest_WithIncludeAndExclude() throws IOException, SqlParseException, SQLFeatureNotSupportedException {
    Aggregations result = query(String.format("select topHits('size'=3,'exclude'='lastname','include'='firstname,lastname',age='desc') from %s/account group by gender ", TEST_INDEX_ACCOUNT));
    List<? extends Terms.Bucket> buckets = ((Terms) (result.asList().get(0))).getBuckets();
    for (Terms.Bucket bucket : buckets) {
        SearchHits hits = ((InternalTopHits) bucket.getAggregations().asList().get(0)).getHits();
        for (SearchHit hit : hits) {
            Set<String> fields = hit.getSourceAsMap().keySet();
            Assert.assertEquals(1, fields.size());
            Assert.assertTrue(fields.contains("firstname"));
        }
    }
}
 
Example #28
Source File: AggregationTest.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
@Test
public void extendedStatsTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException {
    Aggregations result = query(String.format("SELECT EXTENDED_STATS(age) FROM %s/account", TEST_INDEX_ACCOUNT));
    ExtendedStats stats = result.get("EXTENDED_STATS(age)");
    Assert.assertEquals(1000, stats.getCount());
    assertThat(stats.getMin(),equalTo(20.0));
    assertThat(stats.getMax(),equalTo(40.0));
    assertThat(stats.getAvg(),equalTo(30.171));
    assertThat(stats.getSum(),equalTo(30171.0));
    assertThat(stats.getSumOfSquares(),equalTo(946393.0));
    Assert.assertTrue(Math.abs(stats.getStdDeviation()- 6.008640362012022) < 0.0001);
    Assert.assertTrue(Math.abs(stats.getVariance()- 36.10375899999996) < 0.0001);
}
 
Example #29
Source File: AggregationTest.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
@Test
public void reverseToRootGroupByOnNestedFieldWithFilterAndSumOnReverseNestedField() throws Exception {
    Aggregations result = query(String.format("SELECT sum(reverse_nested(myNum)) bla FROM %s/nestedType GROUP BY  nested(message.info),filter('myFilter',message.info = 'a')", TEST_INDEX_NESTED_TYPE));
    InternalNested nested = result.get("message.info@NESTED");
    InternalFilter filter = nested.getAggregations().get("myFilter@FILTER");
    Terms infos = filter.getAggregations().get("message.info");
    Assert.assertEquals(1,infos.getBuckets().size());
    for(Terms.Bucket bucket : infos.getBuckets()) {
        InternalReverseNested reverseNested = bucket.getAggregations().get("myNum@NESTED");
        InternalSum sum = reverseNested.getAggregations().get("bla");
        Assert.assertEquals(5.0,sum.getValue(),0.000001);

    }
}
 
Example #30
Source File: AggregationTest.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
@Test
public void termsWithOrder() throws Exception {
    Aggregations result = query(String.format("SELECT count(*) FROM %s/dog GROUP BY terms('field'='dog_name', 'alias'='dog_name', order='desc')", TEST_INDEX_DOG));
    Terms name = result.get("dog_name");
    Assert.assertEquals("snoopy",name.getBuckets().get(0).getKeyAsString());
    Assert.assertEquals("rex",name.getBuckets().get(1).getKeyAsString());
    
    result = query(String.format("SELECT count(*) FROM %s/dog GROUP BY terms('field'='dog_name', 'alias'='dog_name', order='asc')", TEST_INDEX_DOG));
    name = result.get("dog_name");        
    Assert.assertEquals("rex",name.getBuckets().get(0).getKeyAsString());
    Assert.assertEquals("snoopy",name.getBuckets().get(1).getKeyAsString());
}