Java Code Examples for org.elasticsearch.search.aggregations.bucket.terms.StringTerms

The following examples show how to use org.elasticsearch.search.aggregations.bucket.terms.StringTerms. These examples are extracted from open source projects. 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 Project: ChengFeng1.5   Source File: ElasticSearchTest.java    License: MIT License 6 votes vote down vote up
@Test
public void demo(){
    NativeSearchQueryBuilder builder=new NativeSearchQueryBuilder();
    // 不查询任何结果
    builder.withSourceFilter(new FetchSourceFilter(new String[]{""}, null));


    builder.addAggregation(
            AggregationBuilders.terms("品牌").field("subtitle"));
    // 2、查询,需要把结果强转为AggregatedPage类型
    AggregatedPage<PurchaseInfoDto> aggPage = (AggregatedPage<PurchaseInfoDto>) this.purchaseInfoRepository.search(builder.build());

    StringTerms agg = (StringTerms) aggPage.getAggregation("subtitle");
    // 3.2、获取桶
    List<StringTerms.Bucket> buckets = agg.getBuckets();
    // 3.3、遍历
    for (StringTerms.Bucket bucket : buckets) {
        // 3.4、获取桶中的key,即品牌名称
        System.out.println(bucket.getKeyAsString());
        // 3.5、获取桶中的文档数量
        System.out.println(bucket.getDocCount());
    }


}
 
Example 2
Source Project: mall   Source File: EsSearchResponse.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 获得属性的聚合
 *
 * @param aggregation 属性的聚合
 * @return 返回属性的聚合
 */
private static Map<String, List<String>> getAttributes(Aggregation aggregation) {
    Map<String, List<String>> attributes = new HashMap<>();

    if (Objects.isNull(aggregation)) {
        return attributes;
    }

    InternalNested nested = (InternalNested) aggregation;
    if (CollectionUtils.isEmpty(nested.getAggregations().asList())) {
        return attributes;
    }
    StringTerms stringTerms = (StringTerms) nested.getAggregations().asList().get(0);
    stringTerms.getBuckets().stream().forEach(bucket -> attributes.put((String) bucket.getKey(), getStringTerms(bucket.getAggregations().asList().get(0))));
    return attributes;
}
 
Example 3
public Map<String, Long> getTransactionStatistics() {
  TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders
      .terms("count_group_by_state").field("state.keyword");
  SearchQuery searchQuery = new NativeSearchQueryBuilder()
      .withIndices(INDEX_NAME)
      .addAggregation(termsAggregationBuilder)
      .build();
  return this.template.query(searchQuery, response -> {
    Map<String, Long> statistics = new HashMap<>();
    if (response.getHits().totalHits > 0) {
      final StringTerms groupState = response.getAggregations().get("count_group_by_state");
      statistics = groupState.getBuckets()
          .stream()
          .collect(Collectors.toMap(MultiBucketsAggregation.Bucket::getKeyAsString,
              MultiBucketsAggregation.Bucket::getDocCount));
    }
    return statistics;
  });
}
 
Example 4
Source Project: ChengFeng1.5   Source File: PurchaseInfoServiceImpl.java    License: MIT License 5 votes vote down vote up
@Override
public List<PurchaseInfoVo> getPurchaseInfo() {
    NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
    // 不查询任何结果
    builder.withSourceFilter(new FetchSourceFilter(new String[]{""}, null));


    builder.addAggregation(
            AggregationBuilders.terms("subtitle").field("subtitle")
                    .subAggregation(AggregationBuilders.avg("sales").field("sales")));
    // 2、查询,需要把结果强转为AggregatedPage类型
    AggregatedPage<PurchaseInfoDto> aggPage =
            (AggregatedPage<PurchaseInfoDto>) this.purchaseInfoRepository.search(builder.build());

    StringTerms agg = (StringTerms) aggPage.getAggregation("subtitle");
    // 3.2、获取桶
    List<StringTerms.Bucket> buckets = agg.getBuckets();
    List<PurchaseInfoVo> purchaseInfoVos= Lists.newArrayList();
    // 3.3、遍历
    for (StringTerms.Bucket bucket : buckets) {
        // 3.4、获取桶中的key,即品牌名称
        System.out.println(bucket.getKeyAsString());
        InternalAvg sales= (InternalAvg) bucket.getAggregations().asMap().get("sales");

        purchaseInfoVos.add(new PurchaseInfoVo(bucket.getKeyAsString(),sales.getValue()));
    }
    return purchaseInfoVos;
}
 
Example 5
Source Project: mall   Source File: EsSearchResponse.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 获得StringTerms的值的集合
 *
 * @param aggregation 聚合
 * @return 返回StringTerms的值的集合
 */
private static List<String> getStringTerms(Aggregation aggregation) {
    if (Objects.isNull(aggregation)) {
        return Collections.emptyList();
    }

    StringTerms stringTerms = (StringTerms) aggregation;

    if (CollectionUtils.isEmpty(stringTerms.getBuckets())) {
        return Collections.emptyList();
    }

    return stringTerms.getBuckets().stream().map(key -> (String) key.getKey()).collect(Collectors.toList());
}
 
Example 6
/**
 * 聚合分析
 * 1. 先分组
 * 2. 子分组
 * 3. 最后算出子分组的平均值
 *
 * @param transportClient
 * @throws IOException
 */
private static void aggregate(TransportClient transportClient) throws IOException {

	SearchResponse searchResponse = transportClient.prepareSearch("product_index").setTypes("product")
			.addAggregation(AggregationBuilders.terms("product_group_by_price").field("price")
					.subAggregation(AggregationBuilders.dateHistogram("product_group_by_created_date_time").field("created_date_time")
							.dateHistogramInterval(DateHistogramInterval.YEAR)
							.subAggregation(AggregationBuilders.avg("product_avg_price").field("price")))
			).execute().actionGet();

	Map<String, Aggregation> aggregationMap = searchResponse.getAggregations().asMap();

	StringTerms productGroupByPrice = (StringTerms) aggregationMap.get("product_group_by_price");
	Iterator<Terms.Bucket> productGroupByPriceIterator = productGroupByPrice.getBuckets().iterator();
	while (productGroupByPriceIterator.hasNext()) {
		Terms.Bucket productGroupByPriceBucket = productGroupByPriceIterator.next();
		logger.info("--------------------------------:" + productGroupByPriceBucket.getKey() + ":" + productGroupByPriceBucket.getDocCount());

		Histogram productGroupByPrice1 = (Histogram) productGroupByPriceBucket.getAggregations().asMap().get("product_group_by_price");
		Iterator<org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket> groupByCreateDateTimeIterator = productGroupByPrice1.getBuckets().iterator();
		while (groupByCreateDateTimeIterator.hasNext()) {
			Histogram.Bucket groupByCreateDateTimeBucket = groupByCreateDateTimeIterator.next();
			logger.info("--------------------------------:" + groupByCreateDateTimeBucket.getKey() + ":" + groupByCreateDateTimeBucket.getDocCount());

			Avg avgPrice = (Avg) groupByCreateDateTimeBucket.getAggregations().asMap().get("product_avg_price");
			logger.info("--------------------------------:" + avgPrice.getValue());
		}
	}


}
 
Example 7
@Override
public Map<String, Long> aggregateByRawField(String field, String filtre) {

    TermsBuilder aggregation = AggregationBuilders.terms("top_" + field)
        .field(field + ".raw")
        .size(0)// le résultat n'est pas complet si on ne précise pas la taille, 0 : infini
        // (voir : https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#_size)
        .order(Terms.Order.aggregation("_count", false));

    SearchResponse response = createResponse(filtre, aggregation);

    Map<String, Aggregation> results = response.getAggregations().asMap();
    StringTerms topField = (StringTerms) results.get("top_" + field);

    //sur l'ensemble des buckets, triés par ordre décroissant sur le nombre de documents
    // on retourne une Map (LinkedHashMap) pour conserver l'ordre avec la clé, le nom du champ (exemple version), et la valeur, le nombre de docs
    //exemple :
    // "trunk" -> 34012
    // "branche_1" -> 35800
    return topField.getBuckets()
        .stream()
        .sorted(Comparator.comparing(Terms.Bucket::getDocCount).reversed())
        .collect(
            Collectors.toMap(bucket -> bucket.getKeyAsString(), bucket -> bucket.getDocCount(), (v1, v2) -> v1, LinkedHashMap::new
            ));

}
 
Example 8
Source Project: heroic   Source File: TransportConnection.java    License: Apache License 2.0 5 votes vote down vote up
@NotNull
@Override
public Stream<Pair<String, SearchHits>> parseHits(@NotNull StringTerms terms) {
    return terms.getBuckets()
        .stream()
        .map(bucket -> {
            TopHits hits = bucket.getAggregations().get("hits");
            return new Pair<>(bucket.getKeyAsString(), hits.getHits());
        });
}
 
Example 9
Source Project: heroic   Source File: TransportClientWrapper.java    License: Apache License 2.0 5 votes vote down vote up
@NotNull
@Override
public Connection<StringTerms> start(
    @NotNull AsyncFramework async,
    @NotNull IndexMapping index,
    @NotNull String templateName,
    @NotNull BackendType type
) {
    return new TransportConnection(async, index, templateName, type, client);
}
 
Example 10
/**
 * 将返回结果转换为对象
 */
private EsProductRelatedInfo convertProductRelatedInfo(SearchResponse response) {
    EsProductRelatedInfo productRelatedInfo = new EsProductRelatedInfo();
    Map<String, Aggregation> aggregationMap = response.getAggregations().getAsMap();
    //设置品牌
    Aggregation brandNames = aggregationMap.get("brandNames");
    List<String> brandNameList = new ArrayList<>();
    for(int i = 0; i<((Terms) brandNames).getBuckets().size(); i++){
        brandNameList.add(((Terms) brandNames).getBuckets().get(i).getKeyAsString());
    }
    productRelatedInfo.setBrandNames(brandNameList);
    //设置分类
    Aggregation productCategoryNames = aggregationMap.get("productCategoryNames");
    List<String> productCategoryNameList = new ArrayList<>();
    for(int i=0;i<((Terms) productCategoryNames).getBuckets().size();i++){
        productCategoryNameList.add(((Terms) productCategoryNames).getBuckets().get(i).getKeyAsString());
    }
    productRelatedInfo.setProductCategoryNames(productCategoryNameList);
    //设置参数
    Aggregation productAttrs = aggregationMap.get("allAttrValues");
    List<LongTerms.Bucket> attrIds = ((LongTerms) ((InternalFilter) ((InternalNested) productAttrs).getProperty("productAttrs")).getProperty("attrIds")).getBuckets();
    List<EsProductRelatedInfo.ProductAttr> attrList = new ArrayList<>();
    for (Terms.Bucket attrId : attrIds) {
        EsProductRelatedInfo.ProductAttr attr = new EsProductRelatedInfo.ProductAttr();
        attr.setAttrId((Long) attrId.getKey());
        List<String> attrValueList = new ArrayList<>();
        List<StringTerms.Bucket> attrValues = ((StringTerms) attrId.getAggregations().get("attrValues")).getBuckets();
        List<StringTerms.Bucket> attrNames = ((StringTerms) attrId.getAggregations().get("attrNames")).getBuckets();
        for (Terms.Bucket attrValue : attrValues) {
            attrValueList.add(attrValue.getKeyAsString());
        }
        attr.setAttrValues(attrValueList);
        if(!CollectionUtils.isEmpty(attrNames)){
            String attrName = attrNames.get(0).getKeyAsString();
            attr.setAttrName(attrName);
        }
        attrList.add(attr);
    }
    productRelatedInfo.setProductAttrs(attrList);
    return productRelatedInfo;
}
 
Example 11
Source Project: mall-swarm   Source File: EsProductServiceImpl.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * 将返回结果转换为对象
 */
private EsProductRelatedInfo convertProductRelatedInfo(SearchResponse response) {
    EsProductRelatedInfo productRelatedInfo = new EsProductRelatedInfo();
    Map<String, Aggregation> aggregationMap = response.getAggregations().getAsMap();
    //设置品牌
    Aggregation brandNames = aggregationMap.get("brandNames");
    List<String> brandNameList = new ArrayList<>();
    for(int i = 0; i<((Terms) brandNames).getBuckets().size(); i++){
        brandNameList.add(((Terms) brandNames).getBuckets().get(i).getKeyAsString());
    }
    productRelatedInfo.setBrandNames(brandNameList);
    //设置分类
    Aggregation productCategoryNames = aggregationMap.get("productCategoryNames");
    List<String> productCategoryNameList = new ArrayList<>();
    for(int i=0;i<((Terms) productCategoryNames).getBuckets().size();i++){
        productCategoryNameList.add(((Terms) productCategoryNames).getBuckets().get(i).getKeyAsString());
    }
    productRelatedInfo.setProductCategoryNames(productCategoryNameList);
    //设置参数
    Aggregation productAttrs = aggregationMap.get("allAttrValues");
    List<LongTerms.Bucket> attrIds = ((LongTerms) ((InternalFilter) ((InternalNested) productAttrs).getProperty("productAttrs")).getProperty("attrIds")).getBuckets();
    List<EsProductRelatedInfo.ProductAttr> attrList = new ArrayList<>();
    for (Terms.Bucket attrId : attrIds) {
        EsProductRelatedInfo.ProductAttr attr = new EsProductRelatedInfo.ProductAttr();
        attr.setAttrId((Long) attrId.getKey());
        List<String> attrValueList = new ArrayList<>();
        List<StringTerms.Bucket> attrValues = ((StringTerms) attrId.getAggregations().get("attrValues")).getBuckets();
        List<StringTerms.Bucket> attrNames = ((StringTerms) attrId.getAggregations().get("attrNames")).getBuckets();
        for (Terms.Bucket attrValue : attrValues) {
            attrValueList.add(attrValue.getKeyAsString());
        }
        attr.setAttrValues(attrValueList);
        if(!CollectionUtils.isEmpty(attrNames)){
            String attrName = attrNames.get(0).getKeyAsString();
            attr.setAttrName(attrName);
        }
        attrList.add(attr);
    }
    productRelatedInfo.setProductAttrs(attrList);
    return productRelatedInfo;
}
 
Example 12
Source Project: macrozheng   Source File: EsProductServiceImpl.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * 将返回结果转换为对象
 */
private EsProductRelatedInfo convertProductRelatedInfo(SearchResponse response) {
    EsProductRelatedInfo productRelatedInfo = new EsProductRelatedInfo();
    Map<String, Aggregation> aggregationMap = response.getAggregations().getAsMap();
    //设置品牌
    Aggregation brandNames = aggregationMap.get("brandNames");
    List<String> brandNameList = new ArrayList<>();
    for(int i = 0; i<((Terms) brandNames).getBuckets().size(); i++){
        brandNameList.add(((Terms) brandNames).getBuckets().get(i).getKeyAsString());
    }
    productRelatedInfo.setBrandNames(brandNameList);
    //设置分类
    Aggregation productCategoryNames = aggregationMap.get("productCategoryNames");
    List<String> productCategoryNameList = new ArrayList<>();
    for(int i=0;i<((Terms) productCategoryNames).getBuckets().size();i++){
        productCategoryNameList.add(((Terms) productCategoryNames).getBuckets().get(i).getKeyAsString());
    }
    productRelatedInfo.setProductCategoryNames(productCategoryNameList);
    //设置参数
    Aggregation productAttrs = aggregationMap.get("allAttrValues");
    List<LongTerms.Bucket> attrIds = ((LongTerms) ((InternalFilter) ((InternalNested) productAttrs).getProperty("productAttrs")).getProperty("attrIds")).getBuckets();
    List<EsProductRelatedInfo.ProductAttr> attrList = new ArrayList<>();
    for (Terms.Bucket attrId : attrIds) {
        EsProductRelatedInfo.ProductAttr attr = new EsProductRelatedInfo.ProductAttr();
        attr.setAttrId((Long) attrId.getKey());
        List<String> attrValueList = new ArrayList<>();
        List<StringTerms.Bucket> attrValues = ((StringTerms) attrId.getAggregations().get("attrValues")).getBuckets();
        List<StringTerms.Bucket> attrNames = ((StringTerms) attrId.getAggregations().get("attrNames")).getBuckets();
        for (Terms.Bucket attrValue : attrValues) {
            attrValueList.add(attrValue.getKeyAsString());
        }
        attr.setAttrValues(attrValueList);
        if(!CollectionUtils.isEmpty(attrNames)){
            String attrName = attrNames.get(0).getKeyAsString();
            attr.setAttrName(attrName);
        }
        attrList.add(attr);
    }
    productRelatedInfo.setProductAttrs(attrList);
    return productRelatedInfo;
}
 
Example 13
Source Project: mall   Source File: EsProductServiceImpl.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * 将返回结果转换为对象
 */
private EsProductRelatedInfo convertProductRelatedInfo(SearchResponse response) {
    EsProductRelatedInfo productRelatedInfo = new EsProductRelatedInfo();
    Map<String, Aggregation> aggregationMap = response.getAggregations().getAsMap();
    //设置品牌
    Aggregation brandNames = aggregationMap.get("brandNames");
    List<String> brandNameList = new ArrayList<>();
    for(int i = 0; i<((Terms) brandNames).getBuckets().size(); i++){
        brandNameList.add(((Terms) brandNames).getBuckets().get(i).getKeyAsString());
    }
    productRelatedInfo.setBrandNames(brandNameList);
    //设置分类
    Aggregation productCategoryNames = aggregationMap.get("productCategoryNames");
    List<String> productCategoryNameList = new ArrayList<>();
    for(int i=0;i<((Terms) productCategoryNames).getBuckets().size();i++){
        productCategoryNameList.add(((Terms) productCategoryNames).getBuckets().get(i).getKeyAsString());
    }
    productRelatedInfo.setProductCategoryNames(productCategoryNameList);
    //设置参数
    Aggregation productAttrs = aggregationMap.get("allAttrValues");
    List<LongTerms.Bucket> attrIds = ((LongTerms) ((InternalFilter) ((InternalNested) productAttrs).getProperty("productAttrs")).getProperty("attrIds")).getBuckets();
    List<EsProductRelatedInfo.ProductAttr> attrList = new ArrayList<>();
    for (Terms.Bucket attrId : attrIds) {
        EsProductRelatedInfo.ProductAttr attr = new EsProductRelatedInfo.ProductAttr();
        attr.setAttrId((Long) attrId.getKey());
        List<String> attrValueList = new ArrayList<>();
        List<StringTerms.Bucket> attrValues = ((StringTerms) attrId.getAggregations().get("attrValues")).getBuckets();
        List<StringTerms.Bucket> attrNames = ((StringTerms) attrId.getAggregations().get("attrNames")).getBuckets();
        for (Terms.Bucket attrValue : attrValues) {
            attrValueList.add(attrValue.getKeyAsString());
        }
        attr.setAttrValues(attrValueList);
        if(!CollectionUtils.isEmpty(attrNames)){
            String attrName = attrNames.get(0).getKeyAsString();
            attr.setAttrName(attrName);
        }
        attrList.add(attr);
    }
    productRelatedInfo.setProductAttrs(attrList);
    return productRelatedInfo;
}
 
Example 14
Source Project: macrozheng-mall   Source File: EsProductServiceImpl.java    License: MIT License 4 votes vote down vote up
/**
 * 将返回结果转换为对象
 */
private EsProductRelatedInfo convertProductRelatedInfo(SearchResponse response) {
    EsProductRelatedInfo productRelatedInfo = new EsProductRelatedInfo();
    Map<String, Aggregation> aggregationMap = response.getAggregations().getAsMap();
    //设置品牌
    Aggregation brandNames = aggregationMap.get("brandNames");
    List<String> brandNameList = new ArrayList<>();
    for(int i = 0; i<((StringTerms) brandNames).getBuckets().size(); i++){
        brandNameList.add(((StringTerms) brandNames).getBuckets().get(i).getKeyAsString());
    }
    productRelatedInfo.setBrandNames(brandNameList);
    //设置分类
    Aggregation productCategoryNames = aggregationMap.get("productCategoryNames");
    List<String> productCategoryNameList = new ArrayList<>();
    for(int i=0;i<((StringTerms) productCategoryNames).getBuckets().size();i++){
        productCategoryNameList.add(((StringTerms) productCategoryNames).getBuckets().get(i).getKeyAsString());
    }
    productRelatedInfo.setProductCategoryNames(productCategoryNameList);
    //设置参数
    Aggregation productAttrs = aggregationMap.get("allAttrValues");
    List<Terms.Bucket> attrIds = ((LongTerms) ((InternalFilter)productAttrs.getProperty("productAttrs")).getAggregations().getProperty("attrIds")).getBuckets();
    List<EsProductRelatedInfo.ProductAttr> attrList = new ArrayList<>();
    for (Terms.Bucket attrId : attrIds) {
        EsProductRelatedInfo.ProductAttr attr = new EsProductRelatedInfo.ProductAttr();
        attr.setAttrId((Long) attrId.getKey());
        List<String> attrValueList = new ArrayList<>();
        List<Terms.Bucket> attrValues = ((StringTerms) attrId.getAggregations().get("attrValues")).getBuckets();
        List<Terms.Bucket> attrNames = ((StringTerms) attrId.getAggregations().get("attrNames")).getBuckets();
        for (Terms.Bucket attrValue : attrValues) {
            attrValueList.add(attrValue.getKeyAsString());
        }
        attr.setAttrValues(attrValueList);
        if(!CollectionUtils.isEmpty(attrNames)){
            String attrName = attrNames.get(0).getKeyAsString();
            attr.setAttrName(attrName);
        }
        attrList.add(attr);
    }
    productRelatedInfo.setProductAttrs(attrList);
    return productRelatedInfo;
}
 
Example 15
Source Project: sql4es   Source File: SearchAggregationParser.java    License: 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);
	}
}
 
Example 16
Source Project: herd   Source File: ElasticSearchHelperTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testGetTagTagIndexSearchResponseDto()
{
    SearchResponse searchResponse = mock(SearchResponse.class);
    Terms terms = mock(Terms.class);
    Aggregations aggregations = mock(Aggregations.class);
    Terms.Bucket tagTypeCodeEntry = mock(Terms.Bucket.class);
    List<Terms.Bucket> tagTypeCodeEntryList = Collections.singletonList(tagTypeCodeEntry);

    when(searchResponse.getAggregations()).thenReturn(aggregations);
    when(aggregations.get(TAG_TYPE_FACET_AGGS)).thenReturn(terms);
    when(terms.getBuckets()).thenReturn(tagTypeCodeEntryList);
    when(tagTypeCodeEntry.getKeyAsString()).thenReturn(TAG_TYPE_CODE);
    when(tagTypeCodeEntry.getAggregations()).thenReturn(aggregations);

    Terms tagTypeDisplayNameAggs = mock(Terms.class);
    Terms.Bucket tagTypeDisplayNameEntry = mock(Terms.Bucket.class);
    List<Terms.Bucket> tagTypeDisplayNameEntryList = Collections.singletonList(tagTypeDisplayNameEntry);
    when(aggregations.get(TAGTYPE_NAME_AGGREGATION)).thenReturn(tagTypeDisplayNameAggs);
    when(tagTypeDisplayNameEntry.getAggregations()).thenReturn(aggregations);
    when(tagTypeDisplayNameAggs.getBuckets()).thenReturn(tagTypeDisplayNameEntryList);
    when(tagTypeDisplayNameEntry.getKeyAsString()).thenReturn(TAG_TYPE_DISPLAY_NAME);

    StringTerms tagCodeAggs = mock(StringTerms.class);
    StringTerms.Bucket tagCodeEntry = mock(StringTerms.Bucket.class);
    List<Terms.Bucket> tagCodeEntryList = Collections.singletonList(tagCodeEntry);

    when(aggregations.get(TAG_CODE_AGGREGATION)).thenReturn(tagCodeAggs);
    when(tagCodeAggs.getBuckets()).thenReturn(tagCodeEntryList);
    when(tagCodeEntry.getAggregations()).thenReturn(aggregations);
    when(tagCodeEntry.getKeyAsString()).thenReturn(TAG_CODE);
    when(tagCodeEntry.getDocCount()).thenReturn((long) TAG_CODE_COUNT);
    Terms tagNameAggs = mock(Terms.class);
    Terms.Bucket tagNameEntry = mock(Terms.Bucket.class);
    List<Terms.Bucket> tagNameEntryList = Collections.singletonList(tagNameEntry);
    when(tagNameEntry.getAggregations()).thenReturn(aggregations);
    when(aggregations.get(TAG_NAME_AGGREGATION)).thenReturn(tagNameAggs);
    when(tagNameAggs.getBuckets()).thenReturn(tagNameEntryList);
    when(tagNameEntry.getKeyAsString()).thenReturn(TAG_DISPLAY_NAME);

    List<TagTypeIndexSearchResponseDto> resultList = elasticsearchHelper.getTagTagIndexSearchResponseDto(searchResponse);
    List<TagTypeIndexSearchResponseDto> expectedList = new ArrayList<>();
    List<TagIndexSearchResponseDto> expectedTagList = new ArrayList<>();
    expectedTagList.add(new TagIndexSearchResponseDto(TAG_CODE, TAG_CODE_COUNT, TAG_DISPLAY_NAME));
    expectedList.add(new TagTypeIndexSearchResponseDto(TAG_TYPE_CODE, expectedTagList, TAG_TYPE_DISPLAY_NAME));

    assertEquals(expectedList, resultList);
}