org.elasticsearch.search.aggregations.bucket.terms.LongTerms Java Examples

The following examples show how to use org.elasticsearch.search.aggregations.bucket.terms.LongTerms. 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: SearchService.java    From leyou with Apache License 2.0 6 votes vote down vote up
/**
 * 解析品牌聚合结果
 *
 * @param aggregation
 * @return
 */
private List<Brand> getBrandAggResult(Aggregation aggregation) {
    try {
        // 处理聚合结果集
        LongTerms brandAgg = (LongTerms) aggregation;
        List<Long> bids = new ArrayList<>();
        for (LongTerms.Bucket bucket : brandAgg.getBuckets()) {
            bids.add(bucket.getKeyAsNumber().longValue());
        }
        // 根据id查询品牌
        return this.brandClient.queryBrandByIds(bids);
    } catch (Exception e) {
        logger.error("品牌聚合出现异常:", e);
        return null;
    }
}
 
Example #2
Source File: SearchService.java    From leyou with Apache License 2.0 6 votes vote down vote up
/**
 * 解析商品分类聚合结果
 *
 * @param aggregation
 * @return
 */
private List<Map<String, Object>> getCategoryAggResult(Aggregation aggregation) {
    // 处理聚合结果集
    LongTerms terms = (LongTerms) aggregation;
    // 获取所有的分类id桶
    List<LongTerms.Bucket> buckets = terms.getBuckets();
    // 定义一个品牌集合,搜集所有的品牌对象
    List<Map<String, Object>> categories = new ArrayList<>();
    List<Long> cids = new ArrayList<>();
    // 解析所有的id桶,查询品牌
    buckets.forEach(bucket -> {
        cids.add(bucket.getKeyAsNumber().longValue());
    });
    List<String> names = this.categoryClient.queryNameByIds(cids);
    for (int i = 0; i < cids.size(); i++) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", cids.get(i));
        map.put("name", names.get(i));
        categories.add(map);
    }
    return categories;
}
 
Example #3
Source File: SearchResult.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
private Object covenValue(Aggregation value) throws SqlParseException {
	if (value instanceof InternalNumericMetricsAggregation.SingleValue) {
		return ((InternalNumericMetricsAggregation.SingleValue) value).value();
	} else if (value instanceof InternalValueCount) {
		return ((InternalValueCount) value).getValue();
	} else if (value instanceof InternalTopHits) {
		return (value);
	} else if (value instanceof LongTerms) {
		return value;
	} else {
		throw new SqlParseException("unknow this agg type " + value.getClass());
	}
}
 
Example #4
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 #5
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 #6
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 #7
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 #8
Source File: EsProductServiceImpl.java    From macrozheng-mall with 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 #9
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);
	}
}