Java Code Examples for org.elasticsearch.search.aggregations.bucket.terms.Terms#getBuckets()

The following examples show how to use org.elasticsearch.search.aggregations.bucket.terms.Terms#getBuckets() . 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: TotalSalesQueryAdapter.java    From micronaut-microservices-poc with Apache License 2.0 7 votes vote down vote up
@Override
TotalSalesQuery.Result extractResult(SearchResponse searchResponse) {
    TotalSalesQuery.Result.ResultBuilder result = TotalSalesQuery.Result.builder();
    long count = 0;
    BigDecimal amount = BigDecimal.ZERO;
    Filter filterAgg = searchResponse.getAggregations().get("agg_filter");
    Terms products = filterAgg.getAggregations().get("count_by_product");
    for (Terms.Bucket b : products.getBuckets()){
        count += b.getDocCount();
        Sum sum = b.getAggregations().get("total_premium");
        amount = amount.add(BigDecimal.valueOf(sum.getValue()).setScale(2,BigDecimal.ROUND_HALF_UP));
        result.productTotal(b.getKeyAsString(), SalesResult.of(b.getDocCount(),BigDecimal.valueOf(sum.getValue())));
    }
    result.total(SalesResult.of(count,amount));

    return result.build();
}
 
Example 2
Source File: GroupAction.java    From foxtrot with Apache License 2.0 6 votes vote down vote up
private Map<String, Object> getMap(List<String> fields, Aggregations aggregations) {
    final String field = fields.get(0);
    final List<String> remainingFields = (fields.size() > 1)
                                         ? fields.subList(1, fields.size())
                                         : new ArrayList<>();
    Terms terms = aggregations.get(Utils.sanitizeFieldForAggregation(field));
    Map<String, Object> levelCount = Maps.newHashMap();
    for (Terms.Bucket bucket : terms.getBuckets()) {
        if (fields.size() == 1) {
            if (!CollectionUtils.isNullOrEmpty(getParameter().getUniqueCountOn())) {
                String key = Utils.sanitizeFieldForAggregation(getParameter().getUniqueCountOn());
                Cardinality cardinality = bucket.getAggregations()
                        .get(key);
                levelCount.put(String.valueOf(bucket.getKey()), cardinality.getValue());
            }
            else {
                levelCount.put(String.valueOf(bucket.getKey()), bucket.getDocCount());
            }
        }
        else {
            levelCount.put(String.valueOf(bucket.getKey()), getMap(remainingFields, bucket.getAggregations()));
        }
    }
    return levelCount;

}
 
Example 3
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 4
Source File: AggregationTest.java    From elasticsearch-sql with Apache License 2.0 6 votes vote down vote up
@Test
public void postFilterTest() throws Exception {
    SqlElasticSearchRequestBuilder select = getSearchRequestBuilder(String.format("SELECT /*! POST_FILTER({\"term\":{\"gender\":\"m\"}}) */ COUNT(*) FROM %s/account GROUP BY gender", TEST_INDEX_ACCOUNT));
    SearchResponse res = (SearchResponse) select.get();
    Assert.assertEquals(507, res.getHits().getTotalHits().value);

    Aggregations result = res.getAggregations();
    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 5
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 6
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 7
Source File: SearchServlet.java    From stash-codesearch-plugin with Apache License 2.0 6 votes vote down vote up
private static ImmutableList<ImmutableMap<String, Object>> getSoyRankingList(
    Terms aggregation, long totalCount) {
    ImmutableList.Builder<ImmutableMap<String, Object>> builder = ImmutableList.builder();
    long otherCount = totalCount;
    for (Terms.Bucket bucket : aggregation.getBuckets()) {
        String key = bucket.getKey();
        long count = bucket.getDocCount();
        otherCount -= count;
        builder.add(ImmutableMap.<String, Object> of(
            "key", key,
            "count", count,
            "proportion", ((double) count) / totalCount));
    }
    if (otherCount > 0) {
        builder.add(ImmutableMap.<String, Object> of(
            "other", true,
            "key", "Other",
            "count", otherCount,
            "proportion", ((double) otherCount / totalCount)));
    }
    return builder.build();
}
 
Example 8
Source File: ElasticsearchHelper.java    From herd with Apache License 2.0 6 votes vote down vote up
/**
 * Creates result type facet response dto
 *
 * @param searchResponse search response
 *
 * @return result type facet response dto list
 */
public List<ResultTypeIndexSearchResponseDto> getResultTypeIndexSearchResponseDto(SearchResponse searchResponse)
{
    List<ResultTypeIndexSearchResponseDto> list = new ArrayList<>();
    Terms aggregation = getAggregation(searchResponse, RESULT_TYPE_AGGS);

    for (Terms.Bucket resultTypeEntry : aggregation.getBuckets())
    {
        ResultTypeIndexSearchResponseDto dto = new ResultTypeIndexSearchResponseDto();
        dto.setResultTypeCode(resultTypeEntry.getKeyAsString());
        dto.setResultTypeDisplayName(resultTypeEntry.getKeyAsString());
        dto.setCount(resultTypeEntry.getDocCount());
        list.add(dto);
    }

    return list;
}
 
Example 9
Source File: SysPlantBarOrLineStatisticsChartByElasticsearchService.java    From danyuan-application with Apache License 2.0 6 votes vote down vote up
/**
 * @param legend_data2
 * @param type2
 * 方法名: buildGroup
 * 功 能: TODO(这里用一句话描述这个方法的作用)
 * 参 数: @param xAxis_data
 * 参 数: @param terms1
 * 返 回: void
 * 作 者 : Administrator
 * @throws
 */
private void buildGroup(List<String> xAxis_data, List<String> legend_data, Terms terms1, String type1) {
	if (terms1 == null) {
		return;
	}
	for (Terms.Bucket bucket : terms1.getBuckets()) {
		if (bucket.getKey() == null || "".equals(bucket.getKeyAsString())) {
			continue;
		}
		if (!legend_data.contains(bucket.getKeyAsString())) {
			legend_data.add(bucket.getKeyAsString());
		}
		Terms terms2 = bucket.getAggregations().get(type1 + "_count");
		for (Terms.Bucket bucket2 : terms2.getBuckets()) {
			if (bucket2.getKey() == null || "".equals(bucket2.getKeyAsString())) {
				continue;
			}
			if (!xAxis_data.contains(bucket2.getKeyAsString())) {
				xAxis_data.add(bucket2.getKeyAsString());
			}
		}
	}

}
 
Example 10
Source File: StatsAction.java    From foxtrot with Apache License 2.0 6 votes vote down vote up
private List<BucketResponse<StatsValue>> buildNestedStats(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<StatsValue>> bucketResponses = Lists.newArrayList();
    for (Terms.Bucket bucket : terms.getBuckets()) {
        BucketResponse<StatsValue> bucketResponse = new BucketResponse<>();
        bucketResponse.setKey(String.valueOf(bucket.getKey()));
        if (nesting.size() == 1) {
            bucketResponse.setResult(buildStatsValue(getParameter().getField(), bucket.getAggregations()));
        }
        else {
            bucketResponse.setBuckets(buildNestedStats(remainingFields, bucket.getAggregations()));
        }
        bucketResponses.add(bucketResponse);
    }
    return bucketResponses;
}
 
Example 11
Source File: AggregationServiceImpl.java    From microservices-platform with Apache License 2.0 5 votes vote down vote up
/**
 * 赋值单字段统计
 */
private void setTermsData(Map<String, Object> result, Aggregations aggregations, String key) {
    Terms terms = aggregations.get(key);
    List<String> legendData = new ArrayList<>();
    List<AggItemVo> datas = new ArrayList<>();
    for (Terms.Bucket bucket : terms.getBuckets()) {
        legendData.add((String)bucket.getKey());
        AggItemVo item = new AggItemVo();
        item.setName((String)bucket.getKey());
        item.setValue(bucket.getDocCount());
        datas.add(item);
    }
    result.put(key+"_legendData", legendData);
    result.put(key+"_datas", datas);
}
 
Example 12
Source File: CommonWebpageDAO.java    From spider with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 统计制度域名的网站的文章的词频
 *
 * @param domain 网站域名
 * @return 词-词频
 */
public Map<String, Long> countWordByDomain(String domain) {
    SearchRequestBuilder searchRequestBuilder = client.prepareSearch(INDEX_NAME)
            .setTypes(TYPE_NAME)
            .setQuery(QueryBuilders.matchQuery("domain", domain))
            .addAggregation(AggregationBuilders.terms("content").field("content").size(200));
    SearchResponse response = searchRequestBuilder.execute().actionGet();
    Terms termsAgg = response.getAggregations().get("content");
    List<Terms.Bucket> list = termsAgg.getBuckets();
    Map<String, Long> count = new HashMap<>();
    list.stream().filter(bucket -> ((String) bucket.getKey()).length() > 1).forEach(bucket -> {
        count.put((String) bucket.getKey(), bucket.getDocCount());
    });
    return count;
}
 
Example 13
Source File: AggregationTest.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
@Test
public void orderByAscTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException {
	ArrayList<Long> agesCount = new ArrayList<>();

	Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/account GROUP BY age ORDER BY COUNT(*)", TEST_INDEX_ACCOUNT));
	Terms age = result.get("age");

	for(Terms.Bucket bucket : age.getBuckets()) {
		agesCount.add(((ValueCount) bucket.getAggregations().get("COUNT(*)")).getValue());
	}

	ArrayList<Long> sortedAgesCount = (ArrayList<Long>)agesCount.clone();
	Collections.sort(sortedAgesCount);
	Assert.assertTrue("The list is not ordered ascending", agesCount.equals(agesCount));
}
 
Example 14
Source File: ElasticSearchHelper.java    From testgrid with Apache License 2.0 5 votes vote down vote up
/**
 * Will return all indices given under a certain k8s namespace in the elastic search instance
 * @param nameSpace Namespace of which all available indices are required
 * @return ArrayList containing all indices which have the mentioned namespace
 */
public ArrayList<String> getAllIndexes(String nameSpace) {

    ArrayList<String> indexList = new ArrayList<>();
    open();
    if (esClient != null) {
        try {
            QueryBuilder namespaceMatchQuery = QueryBuilders.boolQuery()
                    .should(QueryBuilders.termQuery("namespace", nameSpace));
            AggregationBuilder uniqueIndexAggregator
                    = AggregationBuilders.terms("unique_ids").field("_index");

            SearchSourceBuilder querySourceBuilder = new SearchSourceBuilder();
            querySourceBuilder.aggregation(uniqueIndexAggregator).query(namespaceMatchQuery);

            SearchRequest searchRequest = new SearchRequest().source(querySourceBuilder);
            SearchResponse searchResponse = esClient.search(searchRequest);
            Terms aggregationOutput =  searchResponse.getAggregations().get("unique_ids");

            for (Terms.Bucket indexBucket : aggregationOutput.getBuckets()) {
                indexList.add(indexBucket.getKey().toString());
            }
            esClient.close();
        } catch (IOException e) {
            logger.error(e.getMessage());
            logger.error("Could not get index list");
        }
    } else {
        logger.error("Client not initialized Could not get index list");
    }

    return indexList;
}
 
Example 15
Source File: MetricsQueryEsDAO.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public int readMetricsValue(final MetricsCondition condition,
                            final String valueColumnName,
                            final Duration duration) throws IOException {
    SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource();
    buildQuery(sourceBuilder, condition, duration);

    TermsAggregationBuilder entityIdAggregation = AggregationBuilders.terms(Metrics.ENTITY_ID)
                                                                     .field(Metrics.ENTITY_ID)
                                                                     .size(1);
    final Function function = ValueColumnMetadata.INSTANCE.getValueFunction(condition.getName());
    functionAggregation(function, entityIdAggregation, valueColumnName);

    sourceBuilder.aggregation(entityIdAggregation);

    SearchResponse response = getClient().search(condition.getName(), sourceBuilder);

    Terms idTerms = response.getAggregations().get(Metrics.ENTITY_ID);
    for (Terms.Bucket idBucket : idTerms.getBuckets()) {
        switch (function) {
            case Sum:
                Sum sum = idBucket.getAggregations().get(valueColumnName);
                return (int) sum.getValue();
            case Avg:
                Avg avg = idBucket.getAggregations().get(valueColumnName);
                return (int) avg.getValue();
            default:
                avg = idBucket.getAggregations().get(valueColumnName);
                return (int) avg.getValue();
        }
    }
    return ValueColumnMetadata.INSTANCE.getDefaultValue(condition.getName());
}
 
Example 16
Source File: AggregationTest.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
@Test
public void reverseAnotherNestedGroupByOnNestedFieldWithFilterAndSumOnReverseNestedField() throws Exception {
    Aggregations result = query(String.format("SELECT sum(reverse_nested(comment.likes,'~comment')) 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("comment.likes@NESTED_REVERSED");
        InternalNested innerNested = reverseNested.getAggregations().get("comment.likes@NESTED");
        InternalSum sum = innerNested.getAggregations().get("bla");
        Assert.assertEquals(4.0,sum.getValue(),0.000001);

    }
}
 
Example 17
Source File: AggregationQueryEs7DAO.java    From skywalking with Apache License 2.0 4 votes vote down vote up
@Override
public List<SelectedRecord> sortMetrics(final TopNCondition condition,
                                        final String valueColumnName,
                                        final Duration duration,
                                        final List<KeyValue> additionalConditions) throws IOException {
    SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource();

    final RangeQueryBuilder queryBuilder = QueryBuilders.rangeQuery(Metrics.TIME_BUCKET)
                                                        .lte(duration.getEndTimeBucket())
                                                        .gte(duration.getStartTimeBucket());

    boolean asc = false;
    if (condition.getOrder().equals(Order.ASC)) {
        asc = true;
    }

    if (additionalConditions != null && additionalConditions.size() > 0) {
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        additionalConditions.forEach(additionalCondition -> {
            boolQuery.must()
                     .add(QueryBuilders.termsQuery(additionalCondition.getKey(), additionalCondition.getValue()));
        });
        boolQuery.must().add(queryBuilder);
        sourceBuilder.query(boolQuery);
    } else {
        sourceBuilder.query(queryBuilder);
    }

    sourceBuilder.aggregation(
        AggregationBuilders.terms(Metrics.ENTITY_ID)
                           .field(Metrics.ENTITY_ID)
                           .order(BucketOrder.aggregation(valueColumnName, asc))
                           .size(condition.getTopN())
                           .subAggregation(AggregationBuilders.avg(valueColumnName).field(valueColumnName))
    );

    SearchResponse response = getClient().search(condition.getName(), sourceBuilder);

    List<SelectedRecord> topNList = new ArrayList<>();
    Terms idTerms = response.getAggregations().get(Metrics.ENTITY_ID);
    for (Terms.Bucket termsBucket : idTerms.getBuckets()) {
        SelectedRecord record = new SelectedRecord();
        record.setId(termsBucket.getKeyAsString());
        Avg value = termsBucket.getAggregations().get(valueColumnName);
        record.setValue(String.valueOf((long) value.getValue()));
        topNList.add(record);
    }

    return topNList;
}
 
Example 18
Source File: SysPlantBarOrLineStatisticsChartByElasticsearchService.java    From danyuan-application with Apache License 2.0 4 votes vote down vote up
/**
 * 方法名: buildBarOrLineType2
 * 功 能: TODO(这里用一句话描述这个方法的作用)
 * 参 数: @param queryBuilder
 * 参 数: @param type1
 * 参 数: @param type2
 * 参 数: @param info
 * 参 数: @param map
 * 返 回: void
 * 作 者 : Administrator
 * @throws
 */
private void buildBarOrLineType2(QueryBuilder queryBuilder, String type1, String type2, SysDbmsChartDimension info, Map<String, Object> map) {
	Client client = elasticsearchTemplate.getClient();
	SearchRequestBuilder requestBuilder = client.prepareSearch(ELASTICSEARCH_INDEX_NAME).setTypes(ELASTICSEARCH_INDEX_TYPE);
	TermsAggregationBuilder aggregationBuilder1 = AggregationBuilders.terms(type2 + "_count").field(type2).size(0);
	TermsAggregationBuilder aggregationBuilder2 = AggregationBuilders.terms(type1 + "_count").field(type1).size(0);
	requestBuilder.setQuery(queryBuilder).addAggregation(aggregationBuilder1.subAggregation(aggregationBuilder2));
	SearchResponse response = requestBuilder.execute().actionGet();
	Terms terms1 = response.getAggregations().get(type2 + "_count");

	Terms terms2;
	List<Map<String, Object>> series_data = new ArrayList<>();
	List<String> legend_data = new ArrayList<>();
	List<String> xAxis_data = new ArrayList<>();
	// 预处理 一次 获取完整xAxis_data
	buildGroup(xAxis_data, legend_data, terms1, type1);

	for (Terms.Bucket bucket : terms1.getBuckets()) {
		if (bucket.getKey() == null || "".equals(bucket.getKeyAsString())) {
			continue;
		}

		Map<String, Object> sdata = new HashMap<>();
		sdata.put("type", "tbar".equals(info.getChartType()) ? "bar" : info.getChartType());
		sdata.put("name", bucket.getKey().toString());

		terms2 = bucket.getAggregations().get(type1 + "_count");
		List<Long> series_data_data = new ArrayList<>();
		for (String string : xAxis_data) {
			boolean check = true;
			for (Terms.Bucket bucket2 : terms2.getBuckets()) {
				if (bucket2.getKey() == null || "".equals(bucket2.getKeyAsString())) {
					continue;
				}

				if (bucket2.getKeyAsString().equals(string)) {
					series_data_data.add(bucket2.getDocCount());
					check = false;
					break;
				}
			}
			if (check) {
				series_data_data.add(0L);
			}
		}
		sdata.put("data", series_data_data);
		series_data.add(sdata);
	}

	map.put("series_data", series_data);
	map.put("legend_data", legend_data);
	map.put("xAxis_data", xAxis_data);
	map.put("chartType", info.getChartType());

}
 
Example 19
Source File: AnalyticsServiceElasticsearch.java    From hawkular-apm with Apache License 2.0 4 votes vote down vote up
/**
 * This method builds a map of communication summary stats related to the supplied
 * criteria.
 *
 * @param stats The map of communication summary stats
 * @param index The index
 * @param criteria The criteria
 * @param addMetrics Whether to add metrics on the nodes/links
 */
private void buildCommunicationSummaryStatistics(Map<String, CommunicationSummaryStatistics> stats, String index,
                                                 Criteria criteria, boolean addMetrics) {
    if (!refresh(index)) {
        return;
    }

    // Don't specify target class, so that query provided that can be used with
    // CommunicationDetails and CompletionTime
    BoolQueryBuilder query = buildQuery(criteria, ElasticsearchUtil.TRANSACTION_FIELD, null);

    // Only want external communications
    query = query.mustNot(QueryBuilders.matchQuery("internal", "true"));

    StatsBuilder latencyBuilder = AggregationBuilders
            .stats("latency")
            .field(ElasticsearchUtil.LATENCY_FIELD);

    TermsBuilder targetBuilder = AggregationBuilders
            .terms("target")
            .field(ElasticsearchUtil.TARGET_FIELD)
            .size(criteria.getMaxResponseSize())
            .subAggregation(latencyBuilder);

    TermsBuilder sourceBuilder = AggregationBuilders
            .terms("source")
            .field(ElasticsearchUtil.SOURCE_FIELD)
            .size(criteria.getMaxResponseSize())
            .subAggregation(targetBuilder);

    SearchRequestBuilder request = getBaseSearchRequestBuilder(COMMUNICATION_DETAILS_TYPE, index, criteria, query, 0)
            .addAggregation(sourceBuilder);
    SearchResponse response = getSearchResponse(request);

    for (Terms.Bucket sourceBucket : response.getAggregations().<Terms>get("source").getBuckets()) {
        Terms targets = sourceBucket.getAggregations().get("target");

        CommunicationSummaryStatistics css = stats.get(sourceBucket.getKey());

        if (css == null) {
            css = new CommunicationSummaryStatistics();
            css.setId(sourceBucket.getKey());
            css.setUri(EndpointUtil.decodeEndpointURI(css.getId()));
            css.setOperation(EndpointUtil.decodeEndpointOperation(css.getId(), true));
            stats.put(css.getId(), css);
        }

        if (addMetrics) {
            css.setCount(sourceBucket.getDocCount());
        }

        for (Terms.Bucket targetBucket : targets.getBuckets()) {
            Stats latency = targetBucket.getAggregations().get("latency");

            String linkId = targetBucket.getKey();
            ConnectionStatistics con = css.getOutbound().get(linkId);

            if (con == null) {
                con = new ConnectionStatistics();
                css.getOutbound().put(linkId, con);
            }

            if (addMetrics) {
                con.setMinimumLatency((long)latency.getMin());
                con.setAverageLatency((long)latency.getAvg());
                con.setMaximumLatency((long)latency.getMax());
                con.setCount(targetBucket.getDocCount());
            }
        }
    }

    addNodeInformation(stats, index, criteria, addMetrics, false);
    addNodeInformation(stats, index, criteria, addMetrics, true);
}
 
Example 20
Source File: SearchAggregationParser.java    From sql4es with Apache License 2.0 4 votes vote down vote up
/**
 * Parse an aggregation result based on one or more aggregated terms
 * @param terms
 * @param rs
 * @param row
 * @throws SQLException
 */
private void dfsAggregations(Terms terms, ESResultSet rs, List<Object> row) throws SQLException{
	List<Object> currentRow = Utils.clone(row);
	String columnName = terms.getName();
	if(!rs.getHeading().hasLabel(columnName)) throw new SQLException("Unable to identify column for aggregation named "+columnName);
	Column aggCol = rs.getHeading().getColumnByLabel(columnName);
	for(Terms.Bucket bucket : terms.getBuckets()){
		if (bucket instanceof StringTerms.Bucket) {
			aggCol.setSqlType(Types.VARCHAR);
		} else if (bucket instanceof LongTerms.Bucket) {
			aggCol.setSqlType(Types.TIMESTAMP);
			//ToDO: chack Timestamp
		}
		boolean metricAggs = false;
		List<Aggregation> aggs = bucket.getAggregations().asList();
		if(aggs.size() == 0){
			currentRow.set(aggCol.getIndex(), bucket.getKey());
			metricAggs = true;
		}else for(Aggregation agg : bucket.getAggregations().asList()){
			if(agg instanceof Terms){
				currentRow.set(aggCol.getIndex(), bucket.getKey());
				dfsAggregations((Terms)agg, rs, currentRow);
			}else{
				if(metricAggs == false){
					currentRow.set(aggCol.getIndex(), bucket.getKey());
					metricAggs = true;
				}
				String metricName = agg.getName();
				if(!rs.getHeading().hasLabel(metricName)) throw new SQLException("Unable to identify column for aggregation named "+metricName);
				Column metricCol = rs.getHeading().getColumnByLabel(metricName);
				// ToDo: check it
                   if (agg instanceof InternalAvg) {
                       currentRow.set(metricCol.getIndex(), ((InternalAvg) agg).getValue());
                   } else if (agg instanceof InternalCardinality) {
                       currentRow.set(metricCol.getIndex(), ((InternalCardinality) agg).getValue());
                   } else if (agg instanceof InternalMax) {
                       currentRow.set(metricCol.getIndex(), ((InternalMax) agg).getValue());
                   } else if (agg instanceof InternalMin) {
                       currentRow.set(metricCol.getIndex(), ((InternalMin) agg).getValue());
                   } else if (agg instanceof Percentile) {
                       currentRow.set(metricCol.getIndex(), ((Percentile) agg).getValue());
                   } else if (agg instanceof InternalSum) {
                       currentRow.set(metricCol.getIndex(), ((InternalSum) agg).getValue());
                   } else if (agg instanceof InternalValueCount) {
                       currentRow.set(metricCol.getIndex(), ((InternalValueCount) agg).getValue());
                   } else if (agg instanceof InternalNumericMetricsAggregation.SingleValue) {
                       currentRow.set(metricCol.getIndex(), ((InternalNumericMetricsAggregation.SingleValue) agg).getValueAsString());
                   } else {
                       // ToDo: I don't know (
                       currentRow.set(metricCol.getIndex(), agg.getName());
                   }
			}
		}
		if(metricAggs){
			rs.add(currentRow);
			currentRow = Utils.clone(row);
		}
		currentRow = Utils.clone(row);
	}
}