Java Code Examples for org.elasticsearch.search.aggregations.bucket.terms.Terms#Bucket

The following examples show how to use org.elasticsearch.search.aggregations.bucket.terms.Terms#Bucket . 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: 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 2
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 3
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 4
Source File: TopologyQueryEsDAO.java    From skywalking with Apache License 2.0 6 votes vote down vote up
private List<Call.CallDetail> buildServiceRelation(SearchSourceBuilder sourceBuilder, String indexName,
                                                   DetectPoint detectPoint) throws IOException {
    sourceBuilder.aggregation(
        AggregationBuilders
            .terms(Metrics.ENTITY_ID).field(Metrics.ENTITY_ID)
            .subAggregation(
                AggregationBuilders.terms(ServiceRelationServerSideMetrics.COMPONENT_ID)
                                   .field(ServiceRelationServerSideMetrics.COMPONENT_ID))
            .size(1000));

    SearchResponse response = getClient().search(indexName, sourceBuilder);

    List<Call.CallDetail> calls = new ArrayList<>();
    Terms entityTerms = response.getAggregations().get(Metrics.ENTITY_ID);
    for (Terms.Bucket entityBucket : entityTerms.getBuckets()) {
        String entityId = entityBucket.getKeyAsString();
        Terms componentTerms = entityBucket.getAggregations().get(ServiceRelationServerSideMetrics.COMPONENT_ID);
        final int componentId = componentTerms.getBuckets().get(0).getKeyAsNumber().intValue();

        Call.CallDetail call = new Call.CallDetail();
        call.buildFromServiceRelation(entityId, componentId, detectPoint);
        calls.add(call);
    }
    return calls;
}
 
Example 5
Source File: AgentSalesQueryAdapter.java    From micronaut-microservices-poc with Apache License 2.0 6 votes vote down vote up
@Override
AgentSalesQuery.Result extractResult(SearchResponse searchResponse) {
    AgentSalesQuery.Result.ResultBuilder result = AgentSalesQuery.Result.builder();
    Filter filterAgg = searchResponse.getAggregations().get("agg_filter");
    Terms agents = filterAgg.getAggregations().get("count_by_agent");

    for (Terms.Bucket b : agents.getBuckets()) {
        Sum sum = b.getAggregations().get("total_premium");
        result.agentTotal(
                b.getKeyAsString(),
                SalesResult.of(b.getDocCount(), BigDecimal.valueOf(sum.getValue()))
        );
    }

    return result.build();
}
 
Example 6
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 7
Source File: TopologyQueryEsDAO.java    From skywalking with Apache License 2.0 6 votes vote down vote up
private List<Call.CallDetail> loadEndpoint(SearchSourceBuilder sourceBuilder, String indexName,
                                           DetectPoint detectPoint) throws IOException {
    sourceBuilder.aggregation(AggregationBuilders.terms(Metrics.ENTITY_ID).field(Metrics.ENTITY_ID).size(1000));

    SearchResponse response = getClient().search(indexName, sourceBuilder);

    List<Call.CallDetail> calls = new ArrayList<>();
    Terms entityTerms = response.getAggregations().get(Metrics.ENTITY_ID);
    for (Terms.Bucket entityBucket : entityTerms.getBuckets()) {
        String entityId = entityBucket.getKeyAsString();

        Call.CallDetail call = new Call.CallDetail();
        call.buildFromEndpointRelation(entityId, detectPoint);
        calls.add(call);
    }
    return calls;
}
 
Example 8
Source File: AggregationTest.java    From elasticsearch-sql with Apache License 2.0 6 votes vote down vote up
@Test
public void groupByOnNestedFieldTest() throws Exception {
    Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/nestedType GROUP BY nested(message.info)", TEST_INDEX_NESTED_TYPE));
    InternalNested nested = result.get("message.info@NESTED");
    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));
        }
    }
}
 
Example 9
Source File: ElasticsearchHelper.java    From herd with Apache License 2.0 5 votes vote down vote up
/**
 * get Tag Type index response
 *
 * @param aggregation aggregation
 *
 * @return list of tag type index search dto
 */
private List<TagTypeIndexSearchResponseDto> getTagTypeIndexSearchResponseDtosFromTerms(Terms aggregation)
{
    List<TagTypeIndexSearchResponseDto> tagTypeIndexSearchResponseDtos = new ArrayList<>();

    for (Terms.Bucket tagTypeCodeEntry : aggregation.getBuckets())
    {
        List<TagIndexSearchResponseDto> tagIndexSearchResponseDtos = new ArrayList<>();

        TagTypeIndexSearchResponseDto tagTypeIndexSearchResponseDto =
            new TagTypeIndexSearchResponseDto(tagTypeCodeEntry.getKeyAsString(), tagIndexSearchResponseDtos, null);
        tagTypeIndexSearchResponseDtos.add(tagTypeIndexSearchResponseDto);

        Terms tagTypeDisplayNameAggs = tagTypeCodeEntry.getAggregations().get(TAGTYPE_NAME_AGGREGATION);
        for (Terms.Bucket tagTypeDisplayNameEntry : tagTypeDisplayNameAggs.getBuckets())
        {
            tagTypeIndexSearchResponseDto.setDisplayName(tagTypeDisplayNameEntry.getKeyAsString());

            Terms tagCodeAggs = tagTypeDisplayNameEntry.getAggregations().get(TAG_CODE_AGGREGATION);
            TagIndexSearchResponseDto tagIndexSearchResponseDto;

            for (Terms.Bucket tagCodeEntry : tagCodeAggs.getBuckets())
            {
                tagIndexSearchResponseDto = new TagIndexSearchResponseDto(tagCodeEntry.getKeyAsString(), tagCodeEntry.getDocCount(), null);
                tagIndexSearchResponseDtos.add(tagIndexSearchResponseDto);

                Terms tagNameAggs = tagCodeEntry.getAggregations().get(TAG_NAME_AGGREGATION);
                for (Terms.Bucket tagNameEntry : tagNameAggs.getBuckets())
                {
                    tagIndexSearchResponseDto.setTagDisplayName(tagNameEntry.getKeyAsString());
                }
            }
        }
    }

    return tagTypeIndexSearchResponseDtos;
}
 
Example 10
Source File: FactSearchManager.java    From act-platform with ISC License 5 votes vote down vote up
private List<ObjectDocument> retrieveSearchObjectsResultValues(SearchResponse response) {
  List<ObjectDocument> result = ListUtils.list();

  Aggregation uniqueObjectsAggregation = resolveChildAggregation(response.getAggregations(), UNIQUE_OBJECTS_AGGREGATION_NAME);
  if (!(uniqueObjectsAggregation instanceof Terms)) {
    LOGGER.warning("Could not retrieve result values when searching for Objects.");
    return result;
  }

  List<? extends Terms.Bucket> buckets = Terms.class.cast(uniqueObjectsAggregation).getBuckets();
  if (CollectionUtils.isEmpty(buckets)) {
    // No buckets means no results.
    return result;
  }

  // Each bucket contains one unique Object, where the TopHits aggregation provides the document source.
  for (Terms.Bucket bucket : buckets) {
    Aggregation uniqueObjectsSourceAggregation = bucket.getAggregations().get(UNIQUE_OBJECTS_SOURCE_AGGREGATION_NAME);
    if (!(uniqueObjectsSourceAggregation instanceof TopHits)) continue;

    // Each bucket should contain only one hit with one unique Object.
    SearchHits hits = TopHits.class.cast(uniqueObjectsSourceAggregation).getHits();
    if (hits.getHits().length < 1) continue;

    // Retrieve Object document from provided search hit.
    ObjectDocument document = decodeObjectDocument(toBytes(hits.getAt(0).getSourceRef()));
    if (document != null) {
      result.add(document);
    }
  }

  return result;
}
 
Example 11
Source File: FactSearchManager.java    From act-platform with ISC License 5 votes vote down vote up
private ObjectStatisticsContainer retrieveObjectStatisticsResult(SearchResponse response) {
  Aggregation uniqueObjectsAggregation = resolveChildAggregation(response.getAggregations(), UNIQUE_OBJECTS_AGGREGATION_NAME);
  if (!(uniqueObjectsAggregation instanceof Terms)) {
    LOGGER.warning("Could not retrieve results when calculating statistics for Objects.");
    return ObjectStatisticsContainer.builder().build();
  }

  List<? extends Terms.Bucket> uniqueObjectBuckets = Terms.class.cast(uniqueObjectsAggregation).getBuckets();
  if (CollectionUtils.isEmpty(uniqueObjectBuckets)) {
    // No buckets means no results.
    return ObjectStatisticsContainer.builder().build();
  }

  ObjectStatisticsContainer.Builder resultBuilder = ObjectStatisticsContainer.builder();

  // Each bucket contains one unique Object. Calculate the statistics for each Object.
  for (Terms.Bucket objectBucket : uniqueObjectBuckets) {
    UUID objectID = UUID.fromString(objectBucket.getKeyAsString());

    // Resolve buckets of unique FactTypes ...
    Aggregation uniqueFactTypesAggregation = resolveChildAggregation(objectBucket.getAggregations(), UNIQUE_FACT_TYPES_AGGREGATION_NAME);
    if (!(uniqueFactTypesAggregation instanceof Terms)) continue;

    List<? extends Terms.Bucket> uniqueFactTypeBuckets = Terms.class.cast(uniqueFactTypesAggregation).getBuckets();
    if (CollectionUtils.isEmpty(uniqueFactTypeBuckets)) continue;

    // ... and add the statistics for each FactType to the result.
    for (Terms.Bucket factTypeBucket : uniqueFactTypeBuckets) {
      UUID factTypeID = UUID.fromString(factTypeBucket.getKeyAsString());
      int factCount = (int) factTypeBucket.getDocCount();
      long lastAddedTimestamp = retrieveMaxTimestamp(factTypeBucket, MAX_LAST_ADDED_TIMESTAMP_AGGREGATION_NAME);
      long lastSeenTimestamp = retrieveMaxTimestamp(factTypeBucket, MAX_LAST_SEEN_TIMESTAMP_AGGREGATION_NAME);
      resultBuilder.addStatistic(objectID, new ObjectStatisticsContainer.FactStatistic(factTypeID, factCount, lastAddedTimestamp, lastSeenTimestamp));
    }
  }

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

	}

}
 
Example 13
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 14
Source File: EsInstanceStore.java    From soundwave with Apache License 2.0 5 votes vote down vote up
/**
 * This a recursive function to process data and produce a hashmap of subaggregations
 * @param aggregationParams List of all aggregation parameters
 * @param currentParam Current parameter to use for processing buckets
 * @param buckets Collection of Terms.Bucket
 * @return HashMap of results
 * @throws Exception
 */
private HashMap<String, HashMap> processBucketData(
    List<String> aggregationParams, String currentParam, Collection<Terms.Bucket> buckets)
    throws Exception {

  HashMap<String, HashMap> result = new HashMap<>();

  for (Terms.Bucket bucket : buckets) {

    String bucketKey = bucket.getKey();
    long docCount = bucket.getDocCount();

    result.put(bucketKey, new HashMap());
    result.get(bucketKey).put("count", docCount);

    // Check if we are at the end of aggregation list
    int index = aggregationParams.indexOf(currentParam);

    if (index == aggregationParams.size() - 1) {
      // We have reached to the end of list stop processing and move to next bucket
      continue;

    }

    String nextParam = aggregationParams.get(index + 1);
    Terms terms = bucket.getAggregations().get(nextParam);
    Collection<Terms.Bucket> subBuckets = terms.getBuckets();

    // use of recursion to get subbuckets hashmap
    result.get(bucketKey)
        .put(nextParam, processBucketData(aggregationParams, nextParam, subBuckets));
  }

  return result;
}
 
Example 15
Source File: AggregationTest.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
@Test
public void topHitTest_WithInclude() throws IOException, SqlParseException, SQLFeatureNotSupportedException {
    Aggregations result = query(String.format("select topHits('size'=3,age='desc',include=age) 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.assertEquals("age",fields.toArray()[0]);
        }
    }
}
 
Example 16
Source File: SysPlantMapStatisticsChartByElasticsearchService.java    From danyuan-application with Apache License 2.0 4 votes vote down vote up
/**
 * 方法名: buildMapType
 * 功 能: TODO(这里用一句话描述这个方法的作用)
 * 参 数: @param map
 * 参 数: @param info
 * 参 数: @param queryBuilder
 * 参 数: @param type1
 * 返 回: void
 * 作 者 : Administrator
 * @throws
 */
private void buildMapType(Map<String, Object> map, SysDbmsChartDimension info, QueryBuilder queryBuilder, String type1) {
	String type2 = "行政区域_省";
	Client client = elasticsearchTemplate.getClient();
	SearchRequestBuilder requestBuilder = client.prepareSearch(ELASTICSEARCH_INDEX_NAME).setTypes(ELASTICSEARCH_INDEX_TYPE);
	TermsAggregationBuilder aggregationBuilder1 = AggregationBuilders.terms(type1 + "_count").field(type1).size(0);
	TermsAggregationBuilder aggregationBuilder2 = AggregationBuilders.terms(type2 + "_count").field(type2).size(0);
	requestBuilder.setQuery(queryBuilder).addAggregation(aggregationBuilder1.subAggregation(aggregationBuilder2));
	SearchResponse response = requestBuilder.execute().actionGet();
	Terms terms1 = response.getAggregations().get(type1 + "_count");

	Terms terms2;

	List<Map<String, Object>> series_data = new ArrayList<>();
	List<String> legend_data = new ArrayList<>();
	for (Terms.Bucket bucket : terms1.getBuckets()) {
		if (bucket.getKey() == null || "".equals(bucket.getKey().toString())) {
			continue;
		}
		terms2 = bucket.getAggregations().get(type2 + "_count");
		legend_data.add(bucket.getKey().toString());
		
		Map<String, Object> mapt = new HashMap<>();
		mapt.put("name", bucket.getKey().toString());
		mapt.put("type", "map");
		mapt.put("mapType", "china");
		mapt.put("roam", false);

		Map<String, Boolean> emap = new HashMap<>();
		Map<String, Object> label = new HashMap<>();
		emap.put("show", true);
		label.put("emphasis", emap);
		Map<String, Boolean> normal = new HashMap<>();
		normal.put("show", false);
		label.put("normal", normal);
		mapt.put("label", label);
		
		List<Map<String, Object>> series_data_data = new ArrayList<>();
		for (Terms.Bucket bucket2 : terms2.getBuckets()) {
			if (bucket2.getKey() == null || "".equals(bucket2.getKey().toString())) {
				continue;
			}
			Map<String, Object> data = new HashMap<>();
			data.put("name", bucket2.getKey().toString());
			data.put("value", bucket2.getDocCount());
			series_data_data.add(data);
		}
		mapt.put("data", series_data_data);
		series_data.add(mapt);
	}

	map.put("series_data", series_data);
	map.put("legend_data", legend_data);
	map.put("chartType", info.getChartType());
}
 
Example 17
Source File: AggregationQueryEsDAO.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
/**
 * 方法名: buildBarOrLineType3
 * 功 能: TODO(这里用一句话描述这个方法的作用)
 * 参 数: @param queryBuilder
 * 参 数: @param type1
 * 参 数: @param type2
 * 参 数: @param type3
 * 参 数: @param info
 * 参 数: @param map
 * 返 回: void
 * 作 者 : Administrator
 * @throws
 */
private void buildBarOrLineType3(QueryBuilder queryBuilder, String type1, String type2, String type3, 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(type1 + "_count").field(type1).size(0);
	TermsAggregationBuilder aggregationBuilder2 = AggregationBuilders.terms(type2 + "_count").field(type2).size(0);
	TermsAggregationBuilder aggregationBuilder3 = AggregationBuilders.terms(type3 + "_count").field(type3).size(0);
	requestBuilder.setQuery(queryBuilder).addAggregation(aggregationBuilder3.subAggregation(aggregationBuilder2.subAggregation(aggregationBuilder1)));
	SearchResponse response = requestBuilder.execute().actionGet();
	Terms terms1 = response.getAggregations().get(type3 + "_count");

	Terms terms2;
	Terms terms3;
	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, type2);

	for (Terms.Bucket bucket : terms1.getBuckets()) {
		if (bucket.getKey() == null || "".equals(bucket.getKeyAsString())) {
			continue;
		}
		terms2 = bucket.getAggregations().get(type2 + "_count");
		for (Terms.Bucket bucket2 : terms2.getBuckets()) {
			Map<String, Object> sdata = new HashMap<>();
			sdata.put("type", "tbar".equals(info.getChartType()) ? "bar" : info.getChartType());
			sdata.put("name", bucket2.getKeyAsString() + "." + bucket.getKeyAsString());
			sdata.put("stack", bucket2.getKeyAsString());

			List<Long> series_data_data = new ArrayList<>();
			for (String string : xAxis_data) {
				boolean check = true;
				terms3 = bucket2.getAggregations().get(type1 + "_count");
				for (Terms.Bucket bucket3 : terms3.getBuckets()) {
					if (bucket3.getKeyAsString().equals(string)) {
						series_data_data.add(bucket3.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
private static Cardinality toCardinality(Terms.Bucket bucket) {
    Cardinality card = new Cardinality();
    card.setValue(bucket.getKey());
    card.setCount(bucket.getDocCount());
    return card;
}
 
Example 20
Source File: CommonWebpageService.java    From spider with GNU General Public License v3.0 2 votes vote down vote up
/**
 * 获取query的关联信息
 *
 * @param query 查询queryString
 * @param size  结果集数量
 * @return 相关信息
 */
public ResultBundle<Pair<Map<String, List<Terms.Bucket>>, List<Webpage>>> relatedInfo(String query, int size) {
    return bundleBuilder.bundle(query, () -> commonWebpageDAO.relatedInfo(query, size));
}