diff --git a/mall-search/src/main/java/com/macro/mall/search/service/impl/EsProductServiceImpl.java b/mall-search/src/main/java/com/macro/mall/search/service/impl/EsProductServiceImpl.java index 517a173..0008749 100644 --- a/mall-search/src/main/java/com/macro/mall/search/service/impl/EsProductServiceImpl.java +++ b/mall-search/src/main/java/com/macro/mall/search/service/impl/EsProductServiceImpl.java @@ -15,6 +15,7 @@ import org.elasticsearch.search.aggregations.AbstractAggregationBuilder; import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.bucket.filter.InternalFilter; +import org.elasticsearch.search.aggregations.bucket.nested.InternalNested; import org.elasticsearch.search.aggregations.bucket.terms.LongTerms; import org.elasticsearch.search.aggregations.bucket.terms.StringTerms; import org.elasticsearch.search.aggregations.bucket.terms.Terms; @@ -247,25 +248,25 @@ public class EsProductServiceImpl implements EsProductService { productRelatedInfo.setProductCategoryNames(productCategoryNameList); //设置参数 Aggregation productAttrs = aggregationMap.get("allAttrValues"); -// List attrIds = ((LongTerms) ((InternalFilter)productAttrs.getProperty("productAttrs")).getAggregations().getProperty("attrIds")).getBuckets(); -// List attrList = new ArrayList<>(); -// for (Terms.Bucket attrId : attrIds) { -// EsProductRelatedInfo.ProductAttr attr = new EsProductRelatedInfo.ProductAttr(); -// attr.setAttrId((Long) attrId.getKey()); -// List attrValueList = new ArrayList<>(); -// List attrValues = ((StringTerms) attrId.getAggregations().get("attrValues")).getBuckets(); -// List 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); + List attrIds = ((LongTerms) ((InternalFilter) ((InternalNested) productAttrs).getProperty("productAttrs")).getProperty("attrIds")).getBuckets(); + List attrList = new ArrayList<>(); + for (Terms.Bucket attrId : attrIds) { + EsProductRelatedInfo.ProductAttr attr = new EsProductRelatedInfo.ProductAttr(); + attr.setAttrId((Long) attrId.getKey()); + List attrValueList = new ArrayList<>(); + List attrValues = ((StringTerms) attrId.getAggregations().get("attrValues")).getBuckets(); + List 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; } }