Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

商品搜索增加属性筛选过滤功能 #336

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.search.domain.EsProduct;
import com.macro.mall.search.domain.EsProductRelatedInfo;
import com.macro.mall.search.dto.AggProduct;
import com.macro.mall.search.dto.QueryProduct;
import com.macro.mall.search.service.EsProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
Expand All @@ -26,6 +28,13 @@ public class EsProductController {
@Autowired
private EsProductService esProductService;

@ApiOperation(value = "初始化Index")
@RequestMapping(value = "/initIndex", method = RequestMethod.POST)
public CommonResult<Boolean> initIndex() {
Boolean result = esProductService.initIndex();
return CommonResult.success(result);
}

@ApiOperation(value = "导入所有数据库中商品到ES")
@RequestMapping(value = "/importAll", method = RequestMethod.POST)
@ResponseBody
Expand Down Expand Up @@ -104,4 +113,18 @@ public CommonResult<EsProductRelatedInfo> searchRelatedInfo(@RequestParam(requir
EsProductRelatedInfo productRelatedInfo = esProductService.searchRelatedInfo(keyword);
return CommonResult.success(productRelatedInfo);
}

@ApiOperation(value = "综合搜索、筛选、排序")
@RequestMapping(value = "/search", method = RequestMethod.POST)
public CommonResult<CommonPage<EsProduct>> search(@RequestBody QueryProduct query) {
Page<EsProduct> pages = esProductService.search(query);
return CommonResult.success(CommonPage.restPage(pages));
}

@ApiOperation(value = "聚合条件查询, 获取搜索的相关品牌、分类及筛选属性 (包含数量)")
@RequestMapping(value = "/search/agg", method = RequestMethod.POST)
public CommonResult<AggProduct> aggWithCount(@RequestBody QueryProduct query) {
AggProduct productRelatedInfo = esProductService.aggProduct(query);
return CommonResult.success(productRelatedInfo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public class EsProduct implements Serializable {
@Field(type = FieldType.Keyword)
private String brandName;
private Long productCategoryId;
/**
* 用于保存多分类以及父分类,支持搜索父分类下所有商品
*/
private List<Long> productCategoryIds;
@Field(type = FieldType.Keyword)
private String productCategoryName;
private String pic;
Expand Down Expand Up @@ -83,6 +87,14 @@ public void setProductCategoryId(Long productCategoryId) {
this.productCategoryId = productCategoryId;
}

public List<Long> getProductCategoryIds() {
return productCategoryIds;
}

public void setProductCategoryIds(List<Long> productCategoryIds) {
this.productCategoryIds = productCategoryIds;
}

public String getProductCategoryName() {
return productCategoryName;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.macro.mall.search.dto;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.util.ArrayList;
import java.util.List;

@ApiModel("商品搜索统计结果")
@lombok.Data
public class AggProduct{

//Buckets

//产品属性 brandName, productCategoryName
@ApiModelProperty("产品共用属性,如brandName, productCategoryName")
private List<ProductAttr> productProps = new ArrayList<>();

//扩展属性 productAttrsBuckets
@ApiModelProperty("产品扩展属性,来自商品类型中属性和参数")
private List<ProductAttr> productAttrs = new ArrayList<>();

@lombok.Data
public static class ProductAttr{
@ApiModelProperty("属性ID,来源于商品类型的属性和参数")
private Long attrId;
@ApiModelProperty("属性名称")
private String attrName;
@ApiModelProperty("属性值统计")
private List<AttrValue> attrValues= new ArrayList<>();
}

@lombok.Data
public static class AttrValue{
@ApiModelProperty("属性值")
private String attrValue;
@ApiModelProperty("统计数")
private Long count;
public AttrValue(){
}

public AttrValue(String attrValue, Long count){
this.attrValue = attrValue;
this.count = count;
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.macro.mall.search.dto;

import com.google.common.collect.Lists;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.math.BigDecimal;
import java.util.List;

@ApiModel("商品搜索参数")
@lombok.Data
public class QueryProduct{
/**
* 关键词
*/
@ApiModelProperty("搜索keyword")
private String keyword;

@ApiModelProperty("商品ID")
private Long id;
@ApiModelProperty("商品SN")
private String productSn;

@ApiModelProperty("品牌ID")
private Long brandId;
@ApiModelProperty("品牌名称")
private String brandName;

@ApiModelProperty("分类ID")
private Long productCategoryId;
@ApiModelProperty("分类名称")
private String productCategoryName;

/*
* @Name: 规格参数
* @Example: [
{
"attrName": "颜色",
"attrValues": ["黑色"]
},
{
"attrName": "内存",
"attrValues": ["64G", "128G"]
}
]
* @Description:
*/
@ApiModelProperty("扩展属性")
private List<ProductAttr> productAttrs = Lists.newArrayList();

@ApiModelProperty("价格区间-最低")
private BigDecimal minPrice;
@ApiModelProperty("价格区间-最高")
private BigDecimal maxPrice;

@ApiModelProperty("排序字段:0->按相关度;1->按新品;2->按销量;3->价格从低到高;4->价格从高到低")
private Integer sort = 0;

@ApiModelProperty("分页数据-页数")
private Integer page = 1;
@ApiModelProperty("分页数据-页大小")
private Integer size = 10;

@lombok.Data
public static class ProductAttr{
@ApiModelProperty("属性ID,来源于商品类型的属性和参数")
private Long attrId;
@ApiModelProperty("属性名称")
private String attrName;
@ApiModelProperty("属性值,可以多个")
private List<String> attrValues;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.macro.mall.search.domain.EsProduct;
import com.macro.mall.search.domain.EsProductRelatedInfo;
import com.macro.mall.search.dto.AggProduct;
import com.macro.mall.search.dto.QueryProduct;
import org.springframework.data.domain.Page;

import java.util.List;
Expand Down Expand Up @@ -50,4 +52,24 @@ public interface EsProductService {
* 获取搜索词相关品牌、分类、属性
*/
EsProductRelatedInfo searchRelatedInfo(String keyword);

/**
* 商品搜索
* @param query
* @return
*/
Page<EsProduct> search(QueryProduct query);

/**
* 商品聚合分析(包含数量统计)
* @param query
* @return
*/
AggProduct aggProduct(QueryProduct query);

/**
* 初始化index
* @return
*/
public boolean initIndex();
}