From e0bcd0ced4220f0488de81132b70afc6609216a1 Mon Sep 17 00:00:00 2001 From: macro Date: Sat, 8 Jun 2019 10:32:08 +0800 Subject: [PATCH] =?UTF-8?q?elasticsearch=20=E8=81=9A=E5=90=88=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E7=BB=93=E6=9E=9C=E9=9B=86=E6=98=A0=E5=B0=84=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/EsProductServiceImpl.java | 39 ++++++++++--------- 1 file changed, 20 insertions(+), 19 deletions(-) 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; } }