添加聚合获取商品品牌、分类、属性功能
This commit is contained in:
13
README.md
13
README.md
@@ -51,10 +51,9 @@ JWT登录、注册、获取token | ✔
|
|||||||
JTA事务处理 | ✔
|
JTA事务处理 | ✔
|
||||||
集成单元测试 | ✔
|
集成单元测试 | ✔
|
||||||
OSS上传功能 | ✔
|
OSS上传功能 | ✔
|
||||||
优化po和dto的定义使用 |
|
|
||||||
SpringSecurity权限管理功能 |
|
SpringSecurity权限管理功能 |
|
||||||
Elasticsearch搜索功能 |
|
Elasticsearch搜索功能 |
|
||||||
MongoDb 日志存储功能 |
|
Elasticsearch日志收集功能 |
|
||||||
数字型ID生成 |
|
数字型ID生成 |
|
||||||
|
|
||||||
### 后台功能
|
### 后台功能
|
||||||
@@ -240,9 +239,19 @@ MongoDb 日志存储功能 |
|
|||||||
- 排序:按新品、销量、价格进行排序
|
- 排序:按新品、销量、价格进行排序
|
||||||
- 搜索返回结果:商品ID、商品图片、名称、副标题、价格、商品销量、新品、商品的参数、品牌名称、分类名称
|
- 搜索返回结果:商品ID、商品图片、名称、副标题、价格、商品销量、新品、商品的参数、品牌名称、分类名称
|
||||||
- 接口:从数据库中查询相关数据并导入es,插入(修改)数据接口,删除数据接口
|
- 接口:从数据库中查询相关数据并导入es,插入(修改)数据接口,删除数据接口
|
||||||
|
- 品牌分类筛选:根据搜索结果聚合返回品牌、分类及属性
|
||||||
|
|
||||||
> **商品推荐功能**
|
> **商品推荐功能**
|
||||||
|
|
||||||
|
- 推荐某商品的相关商品、根据该商品的品牌(10)、分类(6)、名称(8)、关键字(2)、副标题(2)
|
||||||
|
- 根据用户一周浏览记录推荐商品,根据用户搜索记录推荐商品
|
||||||
|
|
||||||
> **商品热搜功能**
|
> **商品热搜功能**
|
||||||
|
|
||||||
|
- 根据用户搜索记录聚合生成热搜词
|
||||||
|
|
||||||
> **商品搜索提示功能**
|
> **商品搜索提示功能**
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package com.macro.mall.search.controller;
|
|||||||
|
|
||||||
import com.macro.mall.search.domain.CommonResult;
|
import com.macro.mall.search.domain.CommonResult;
|
||||||
import com.macro.mall.search.domain.EsProduct;
|
import com.macro.mall.search.domain.EsProduct;
|
||||||
|
import com.macro.mall.search.domain.EsProductRelatedInfo;
|
||||||
import com.macro.mall.search.service.EsProductService;
|
import com.macro.mall.search.service.EsProductService;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiImplicitParam;
|
import io.swagger.annotations.ApiImplicitParam;
|
||||||
@@ -84,4 +85,22 @@ public class EsProductController {
|
|||||||
Page<EsProduct> esProductPage = esProductService.search(keyword, brandId, productCategoryId, pageNum, pageSize, sort);
|
Page<EsProduct> esProductPage = esProductService.search(keyword, brandId, productCategoryId, pageNum, pageSize, sort);
|
||||||
return new CommonResult().pageSuccess(esProductPage);
|
return new CommonResult().pageSuccess(esProductPage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ApiOperation(value = "根据商品id推荐商品")
|
||||||
|
@RequestMapping(value = "/recommend/{id}",method = RequestMethod.GET)
|
||||||
|
@ResponseBody
|
||||||
|
public Object recommend(@PathVariable Long id,
|
||||||
|
@RequestParam(required = false, defaultValue = "0") Integer pageNum,
|
||||||
|
@RequestParam(required = false, defaultValue = "5") Integer pageSize){
|
||||||
|
Page<EsProduct> esProductPage = esProductService.recommend(id, pageNum, pageSize);
|
||||||
|
return new CommonResult().pageSuccess(esProductPage);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ApiOperation(value = "获取搜索的相关品牌、分类及筛选属性")
|
||||||
|
@RequestMapping(value = "/search/relate",method = RequestMethod.GET)
|
||||||
|
@ResponseBody
|
||||||
|
public Object searchRelatedInfo(@RequestParam(required = false) String keyword){
|
||||||
|
EsProductRelatedInfo productRelatedInfo = esProductService.searchRelatedInfo(keyword);
|
||||||
|
return new CommonResult().success(productRelatedInfo);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package com.macro.mall.search.domain;
|
package com.macro.mall.search.domain;
|
||||||
|
|
||||||
import com.macro.mall.model.PmsProductAttributeValue;
|
|
||||||
import org.springframework.data.annotation.Id;
|
import org.springframework.data.annotation.Id;
|
||||||
import org.springframework.data.elasticsearch.annotations.Document;
|
import org.springframework.data.elasticsearch.annotations.Document;
|
||||||
import org.springframework.data.elasticsearch.annotations.Field;
|
import org.springframework.data.elasticsearch.annotations.Field;
|
||||||
@@ -42,7 +41,8 @@ public class EsProduct implements Serializable {
|
|||||||
private Integer stock;
|
private Integer stock;
|
||||||
private Integer promotionType;
|
private Integer promotionType;
|
||||||
private Integer sort;
|
private Integer sort;
|
||||||
private List<PmsProductAttributeValue> attrValueList;
|
@Field(type =FieldType.Nested)
|
||||||
|
private List<EsProductAttributeValue> attrValueList;
|
||||||
|
|
||||||
public Long getId() {
|
public Long getId() {
|
||||||
return id;
|
return id;
|
||||||
@@ -172,11 +172,11 @@ public class EsProduct implements Serializable {
|
|||||||
this.sort = sort;
|
this.sort = sort;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<PmsProductAttributeValue> getAttrValueList() {
|
public List<EsProductAttributeValue> getAttrValueList() {
|
||||||
return attrValueList;
|
return attrValueList;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAttrValueList(List<PmsProductAttributeValue> attrValueList) {
|
public void setAttrValueList(List<EsProductAttributeValue> attrValueList) {
|
||||||
this.attrValueList = attrValueList;
|
this.attrValueList = attrValueList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,64 @@
|
|||||||
|
package com.macro.mall.search.domain;
|
||||||
|
|
||||||
|
import org.springframework.data.elasticsearch.annotations.Field;
|
||||||
|
import org.springframework.data.elasticsearch.annotations.FieldIndex;
|
||||||
|
import org.springframework.data.elasticsearch.annotations.FieldType;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 搜索中的商品属性信息
|
||||||
|
* Created by macro on 2018/6/27.
|
||||||
|
*/
|
||||||
|
public class EsProductAttributeValue implements Serializable {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
private Long id;
|
||||||
|
private Long productAttributeId;
|
||||||
|
//属性值
|
||||||
|
@Field(index = FieldIndex.not_analyzed, type = FieldType.String)
|
||||||
|
private String value;
|
||||||
|
//属性参数:0->规格;1->参数
|
||||||
|
private Integer type;
|
||||||
|
//属性名称
|
||||||
|
@Field(index = FieldIndex.not_analyzed, type = FieldType.String)
|
||||||
|
private String name;
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getProductAttributeId() {
|
||||||
|
return productAttributeId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setProductAttributeId(Long productAttributeId) {
|
||||||
|
this.productAttributeId = productAttributeId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setValue(String value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setType(Integer type) {
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
package com.macro.mall.search.domain;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 搜索相关商品品牌名称,分类名称及属性
|
||||||
|
* Created by macro on 2018/6/27.
|
||||||
|
*/
|
||||||
|
public class EsProductRelatedInfo {
|
||||||
|
private List<String> brandNames;
|
||||||
|
private List<String> productCategoryNames;
|
||||||
|
private List<ProductAttr> productAttrs;
|
||||||
|
|
||||||
|
public List<String> getBrandNames() {
|
||||||
|
return brandNames;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBrandNames(List<String> brandNames) {
|
||||||
|
this.brandNames = brandNames;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getProductCategoryNames() {
|
||||||
|
return productCategoryNames;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setProductCategoryNames(List<String> productCategoryNames) {
|
||||||
|
this.productCategoryNames = productCategoryNames;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<ProductAttr> getProductAttrs() {
|
||||||
|
return productAttrs;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setProductAttrs(List<ProductAttr> productAttrs) {
|
||||||
|
this.productAttrs = productAttrs;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class ProductAttr{
|
||||||
|
private Long attrId;
|
||||||
|
private String attrName;
|
||||||
|
private List<String> attrValues;
|
||||||
|
|
||||||
|
public Long getAttrId() {
|
||||||
|
return attrId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAttrId(Long attrId) {
|
||||||
|
this.attrId = attrId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getAttrValues() {
|
||||||
|
return attrValues;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAttrValues(List<String> attrValues) {
|
||||||
|
this.attrValues = attrValues;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAttrName() {
|
||||||
|
return attrName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAttrName(String attrName) {
|
||||||
|
this.attrName = attrName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.macro.mall.search.service;
|
package com.macro.mall.search.service;
|
||||||
|
|
||||||
import com.macro.mall.search.domain.EsProduct;
|
import com.macro.mall.search.domain.EsProduct;
|
||||||
|
import com.macro.mall.search.domain.EsProductRelatedInfo;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -39,4 +40,14 @@ public interface EsProductService {
|
|||||||
* 根据关键字搜索名称或者副标题复合查询
|
* 根据关键字搜索名称或者副标题复合查询
|
||||||
*/
|
*/
|
||||||
Page<EsProduct> search(String keyword, Long brandId, Long productCategoryId, Integer pageNum, Integer pageSize,Integer sort);
|
Page<EsProduct> search(String keyword, Long brandId, Long productCategoryId, Integer pageNum, Integer pageSize,Integer sort);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据商品id推荐相关商品
|
||||||
|
*/
|
||||||
|
Page<EsProduct> recommend(Long id, Integer pageNum, Integer pageSize);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取搜索词相关品牌、分类、属性
|
||||||
|
*/
|
||||||
|
EsProductRelatedInfo searchRelatedInfo(String keyword);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,20 +2,31 @@ package com.macro.mall.search.service.impl;
|
|||||||
|
|
||||||
import com.macro.mall.search.dao.EsProductDao;
|
import com.macro.mall.search.dao.EsProductDao;
|
||||||
import com.macro.mall.search.domain.EsProduct;
|
import com.macro.mall.search.domain.EsProduct;
|
||||||
|
import com.macro.mall.search.domain.EsProductRelatedInfo;
|
||||||
import com.macro.mall.search.repository.EsProductRepository;
|
import com.macro.mall.search.repository.EsProductRepository;
|
||||||
import com.macro.mall.search.service.EsProductService;
|
import com.macro.mall.search.service.EsProductService;
|
||||||
|
import org.elasticsearch.action.search.SearchResponse;
|
||||||
import org.elasticsearch.index.query.BoolQueryBuilder;
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
||||||
import org.elasticsearch.index.query.QueryBuilders;
|
import org.elasticsearch.index.query.QueryBuilders;
|
||||||
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
|
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
|
||||||
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
|
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
|
||||||
|
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.terms.LongTerms;
|
||||||
|
import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
|
||||||
|
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
||||||
import org.elasticsearch.search.sort.SortBuilders;
|
import org.elasticsearch.search.sort.SortBuilders;
|
||||||
import org.elasticsearch.search.sort.SortOrder;
|
import org.elasticsearch.search.sort.SortOrder;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.PageImpl;
|
||||||
import org.springframework.data.domain.PageRequest;
|
import org.springframework.data.domain.PageRequest;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
|
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
|
||||||
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
|
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
|
||||||
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
|
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@@ -25,6 +36,7 @@ import org.springframework.util.StringUtils;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -33,11 +45,13 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
public class EsProductServiceImpl implements EsProductService {
|
public class EsProductServiceImpl implements EsProductService {
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(EsProductServiceImpl.class);
|
||||||
@Autowired
|
@Autowired
|
||||||
private EsProductDao productDao;
|
private EsProductDao productDao;
|
||||||
@Autowired
|
@Autowired
|
||||||
private EsProductRepository productRepository;
|
private EsProductRepository productRepository;
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(EsProductServiceImpl.class);
|
@Autowired
|
||||||
|
private ElasticsearchTemplate elasticsearchTemplate;
|
||||||
@Override
|
@Override
|
||||||
public int importAll() {
|
public int importAll() {
|
||||||
List<EsProduct> esProductList = productDao.getAllEsProductList(null);
|
List<EsProduct> esProductList = productDao.getAllEsProductList(null);
|
||||||
@@ -106,12 +120,13 @@ public class EsProductServiceImpl implements EsProductService {
|
|||||||
//搜索
|
//搜索
|
||||||
FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery()
|
FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery()
|
||||||
.add(QueryBuilders.matchQuery("name", keyword),
|
.add(QueryBuilders.matchQuery("name", keyword),
|
||||||
ScoreFunctionBuilders.weightFactorFunction(1000))
|
ScoreFunctionBuilders.weightFactorFunction(10))
|
||||||
.add(QueryBuilders.matchQuery("subTitle", keyword),
|
.add(QueryBuilders.matchQuery("subTitle", keyword),
|
||||||
ScoreFunctionBuilders.weightFactorFunction(500))
|
ScoreFunctionBuilders.weightFactorFunction(5))
|
||||||
.add(QueryBuilders.matchQuery("keywords", keyword),
|
.add(QueryBuilders.matchQuery("keywords", keyword),
|
||||||
ScoreFunctionBuilders.weightFactorFunction(200))
|
ScoreFunctionBuilders.weightFactorFunction(2))
|
||||||
.scoreMode("sum").setMinScore(10f);
|
.scoreMode("sum")
|
||||||
|
.setMinScore(2);
|
||||||
if (StringUtils.isEmpty(keyword)) {
|
if (StringUtils.isEmpty(keyword)) {
|
||||||
nativeSearchQueryBuilder.withQuery(QueryBuilders.matchAllQuery());
|
nativeSearchQueryBuilder.withQuery(QueryBuilders.matchAllQuery());
|
||||||
} else {
|
} else {
|
||||||
@@ -137,7 +152,111 @@ public class EsProductServiceImpl implements EsProductService {
|
|||||||
nativeSearchQueryBuilder.withSort(SortBuilders.scoreSort().order(SortOrder.DESC));
|
nativeSearchQueryBuilder.withSort(SortBuilders.scoreSort().order(SortOrder.DESC));
|
||||||
nativeSearchQueryBuilder.withSort(SortBuilders.scoreSort().order(SortOrder.DESC));
|
nativeSearchQueryBuilder.withSort(SortBuilders.scoreSort().order(SortOrder.DESC));
|
||||||
NativeSearchQuery searchQuery = nativeSearchQueryBuilder.build();
|
NativeSearchQuery searchQuery = nativeSearchQueryBuilder.build();
|
||||||
// LOGGER.info("DSL:{}", searchQuery.getQuery().toString());
|
LOGGER.info("DSL:{}", searchQuery.getQuery().toString());
|
||||||
return productRepository.search(searchQuery);
|
return productRepository.search(searchQuery);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Page<EsProduct> recommend(Long id, Integer pageNum, Integer pageSize) {
|
||||||
|
Pageable pageable = new PageRequest(pageNum, pageSize);
|
||||||
|
List<EsProduct> esProductList = productDao.getAllEsProductList(id);
|
||||||
|
if (esProductList.size() > 0) {
|
||||||
|
EsProduct esProduct = esProductList.get(0);
|
||||||
|
String keyword = esProduct.getName();
|
||||||
|
Long brandId = esProduct.getBrandId();
|
||||||
|
Long productCategoryId = esProduct.getProductCategoryId();
|
||||||
|
//根据商品标题、品牌、分类进行搜索
|
||||||
|
FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery()
|
||||||
|
.add(QueryBuilders.matchQuery("name",keyword),ScoreFunctionBuilders.weightFactorFunction(8))
|
||||||
|
.add(QueryBuilders.matchQuery("subTitle",keyword),ScoreFunctionBuilders.weightFactorFunction(2))
|
||||||
|
.add(QueryBuilders.matchQuery("keywords",keyword),ScoreFunctionBuilders.weightFactorFunction(2))
|
||||||
|
.add(QueryBuilders.termQuery("brandId",brandId),ScoreFunctionBuilders.weightFactorFunction(10))
|
||||||
|
.add(QueryBuilders.matchQuery("productCategoryId",productCategoryId),ScoreFunctionBuilders.weightFactorFunction(6))
|
||||||
|
.scoreMode("sum")
|
||||||
|
.setMinScore(2);
|
||||||
|
NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
|
||||||
|
builder.withQuery(functionScoreQueryBuilder);
|
||||||
|
builder.withPageable(pageable);
|
||||||
|
NativeSearchQuery searchQuery = builder.build();
|
||||||
|
LOGGER.info("DSL:{}", searchQuery.getQuery().toString());
|
||||||
|
return productRepository.search(searchQuery);
|
||||||
|
}
|
||||||
|
return new PageImpl<>(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public EsProductRelatedInfo searchRelatedInfo(String keyword) {
|
||||||
|
NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
|
||||||
|
//搜索条件
|
||||||
|
if(StringUtils.isEmpty(keyword)){
|
||||||
|
builder.withQuery(QueryBuilders.matchAllQuery());
|
||||||
|
}else{
|
||||||
|
builder.withQuery(QueryBuilders.multiMatchQuery(keyword,"name","subTitle","keywords"));
|
||||||
|
}
|
||||||
|
//聚合搜索品牌名称
|
||||||
|
builder.addAggregation(AggregationBuilders.terms("brandNames").field("brandName"));
|
||||||
|
//集合搜索分类名称
|
||||||
|
builder.addAggregation(AggregationBuilders.terms("productCategoryNames").field("productCategoryName"));
|
||||||
|
//聚合搜索商品属性,去除type=1的属性
|
||||||
|
AbstractAggregationBuilder aggregationBuilder = AggregationBuilders.nested("allAttrValues")
|
||||||
|
.path("attrValueList")
|
||||||
|
.subAggregation(AggregationBuilders.filter("productAttrs")
|
||||||
|
.filter(QueryBuilders.termQuery("attrValueList.type",1))
|
||||||
|
.subAggregation(AggregationBuilders.terms("attrIds")
|
||||||
|
.field("attrValueList.productAttributeId")
|
||||||
|
.subAggregation(AggregationBuilders.terms("attrValues")
|
||||||
|
.field("attrValueList.value"))
|
||||||
|
.subAggregation(AggregationBuilders.terms("attrNames")
|
||||||
|
.field("attrValueList.name"))));
|
||||||
|
builder.addAggregation(aggregationBuilder);
|
||||||
|
NativeSearchQuery searchQuery = builder.build();
|
||||||
|
return elasticsearchTemplate.query(searchQuery, response -> {
|
||||||
|
LOGGER.info("DSL:{}",searchQuery.getQuery().toString());
|
||||||
|
return convertProductRelatedInfo(response);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将返回结果转换为对象
|
||||||
|
*/
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,12 @@
|
|||||||
<mapper namespace="com.macro.mall.search.dao.EsProductDao">
|
<mapper namespace="com.macro.mall.search.dao.EsProductDao">
|
||||||
<resultMap id="esProductListMap" type="com.macro.mall.search.domain.EsProduct" autoMapping="true">
|
<resultMap id="esProductListMap" type="com.macro.mall.search.domain.EsProduct" autoMapping="true">
|
||||||
<id column="id" jdbcType="BIGINT" property="id" />
|
<id column="id" jdbcType="BIGINT" property="id" />
|
||||||
<collection property="attrValueList" columnPrefix="attr_" resultMap="com.macro.mall.mapper.PmsProductAttributeValueMapper.BaseResultMap">
|
<collection property="attrValueList" columnPrefix="attr_" ofType="com.macro.mall.search.domain.EsProductAttributeValue">
|
||||||
|
<id column="id" property="id" jdbcType="BIGINT"/>
|
||||||
|
<result column="product_attribute_id" property="productAttributeId" jdbcType="BIGINT"/>
|
||||||
|
<result column="value" property="value" jdbcType="VARCHAR"/>
|
||||||
|
<result column="type" property="type"/>
|
||||||
|
<result column="name" property="name"/>
|
||||||
</collection>
|
</collection>
|
||||||
</resultMap>
|
</resultMap>
|
||||||
<select id="getAllEsProductList" resultMap="esProductListMap">
|
<select id="getAllEsProductList" resultMap="esProductListMap">
|
||||||
@@ -25,11 +30,14 @@
|
|||||||
p.promotion_type promotionType,
|
p.promotion_type promotionType,
|
||||||
P.keywords keywords,
|
P.keywords keywords,
|
||||||
p.sort sort,
|
p.sort sort,
|
||||||
a.id attr_id,
|
pav.id attr_id,
|
||||||
a.value attr_value,
|
pav.value attr_value,
|
||||||
a.product_attribute_id attr_product_attribute_id
|
pav.product_attribute_id attr_product_attribute_id,
|
||||||
|
pa.type attr_type,
|
||||||
|
pa.name attr_name
|
||||||
from pms_product p
|
from pms_product p
|
||||||
left join pms_product_attribute_value a on p.id = a.product_id
|
left join pms_product_attribute_value pav on p.id = pav.product_id
|
||||||
|
left join pms_product_attribute pa on pav.product_attribute_id= pa.id
|
||||||
where delete_status = 0 and publish_status = 1
|
where delete_status = 0 and publish_status = 1
|
||||||
<if test="id!=null">
|
<if test="id!=null">
|
||||||
and p.id=#{id}
|
and p.id=#{id}
|
||||||
|
|||||||
@@ -3,11 +3,13 @@ package com.macro.mall.search;
|
|||||||
import com.macro.mall.search.dao.EsProductDao;
|
import com.macro.mall.search.dao.EsProductDao;
|
||||||
import com.macro.mall.search.domain.EsProduct;
|
import com.macro.mall.search.domain.EsProduct;
|
||||||
import com.macro.mall.search.repository.EsProductRepository;
|
import com.macro.mall.search.repository.EsProductRepository;
|
||||||
|
import org.elasticsearch.action.search.SearchResponse;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
|
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
|
||||||
|
import org.springframework.data.elasticsearch.core.ResultsExtractor;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -29,7 +31,7 @@ public class MallSearchApplicationTests {
|
|||||||
System.out.print(esProductList);
|
System.out.print(esProductList);
|
||||||
}
|
}
|
||||||
@Test
|
@Test
|
||||||
public void testEsMapping(){
|
public void testEsProductMapping(){
|
||||||
elasticsearchTemplate.putMapping(EsProduct.class);
|
elasticsearchTemplate.putMapping(EsProduct.class);
|
||||||
Map mapping = elasticsearchTemplate.getMapping(EsProduct.class);
|
Map mapping = elasticsearchTemplate.getMapping(EsProduct.class);
|
||||||
System.out.println(mapping);
|
System.out.println(mapping);
|
||||||
|
|||||||
Reference in New Issue
Block a user