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

The following examples show how to use org.elasticsearch.search.aggregations.bucket.terms.StringTerms#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: ElasticSearchTest.java    From ChengFeng1.5 with 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 File: PurchaseInfoServiceImpl.java    From ChengFeng1.5 with 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 3
Source File: EsProductServiceImpl.java    From BigDataPlatform with GNU General Public License v3.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 4
Source File: EsProductServiceImpl.java    From mall-swarm with 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 5
Source File: EsProductServiceImpl.java    From macrozheng with 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 6
Source File: EsProductServiceImpl.java    From mall with 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 7
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);
	}
}
 
Example 8
Source File: ElasticSearchHelperTest.java    From herd with 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);
}