分类页

parent cd2cb182
Showing with 36 additions and 34 deletions
......@@ -15,25 +15,28 @@ https://ichunt.com/optimum-9987_1.html?ev=brand_12042^2660_128036||128037
@param attrs post 参数
@param types int 返回统计还是结果: 1单独返回统计 2单独返回查询结果
*/
func GetOptimumAttrQuery(ctx *gin.Context,REQUEST_ATTR *map[string][]string,types int) (param string, err error){
*/
func GetOptimumAttrQuery(ctx *gin.Context, REQUEST_ATTR *map[string][]string, types int) (param string, err error) {
query := elastic.NewBoolQuery()
//query1 := elastic.NewBoolQuery()
//query2 := elastic.NewBoolQuery()
//todo 2023.1.12 暂时去掉自营数据
query.MustNot(elastic.NewTermQuery("supplier_name", "a"))
//一级分类查询
class_id1 := ctx.Request.FormValue("class_id1")
if class_id1 != "" {
query.Must(elastic.NewTermQuery("class_id1",class_id1))
query.Must(elastic.NewTermQuery("class_id1", class_id1))
}
//二级分类查询
class_id2 := ctx.Request.FormValue("class_id2")
if class_id2 != "" {
query.Must(elastic.NewTermQuery("class_id2",class_id2))
query.Must(elastic.NewTermQuery("class_id2", class_id2))
}
if class_id1 == "" && class_id2 == "" {
return "",errors.New("分类参数不得为空")
return "", errors.New("分类参数不得为空")
}
if ctx.Request.FormValue("avail_rank") == "1" { //只显示有货
......@@ -41,22 +44,22 @@ func GetOptimumAttrQuery(ctx *gin.Context,REQUEST_ATTR *map[string][]string,type
}
goods_name := common.DrawLetterNum(ctx.Request.FormValue("goods_name/condition"))
if goods_name != "" { //型号搜索
query.Must(elastic.NewMatchQuery("goods_name",goods_name).MinimumShouldMatch("30%")) //模糊匹配
query.Must(elastic.NewMatchQuery("goods_name", goods_name).MinimumShouldMatch("30%")) //模糊匹配
}
//存在属性查询
if len(*REQUEST_ATTR) >0 { //存在属性搜索
if len(*REQUEST_ATTR) > 0 { //存在属性搜索
redisr := gredis.Conn("search_r")
for attr_name_id, attr_value_ids := range *REQUEST_ATTR {
if attr_name_id == "brand" {
for _,attr_value_id := range attr_value_ids{
query.Must(elastic.NewTermQuery("brand_id",attr_value_id))
for _, attr_value_id := range attr_value_ids {
query.Must(elastic.NewTermQuery("brand_id", attr_value_id))
}
}else{
for _,attr_value_id := range attr_value_ids{
attr_name,_ := gredis.String(redisr.Do("HGET",config.Get("redis_all.class_attr_by_id").String(),attr_name_id)) //查询唯一值,反查sku_id
attr_value_name,_ := gredis.String(redisr.Do("HGET",config.Get("redis_all.class_attr_value_by_id").String(),attr_value_id)) //查询唯一值,反查sku_id
query.Must(elastic.NewNestedQuery("attrs",elastic.NewBoolQuery().Must(elastic.NewTermQuery("attrs.attr_name",attr_name),elastic.NewTermQuery("attrs.attr_value",attr_value_name))))
} else {
for _, attr_value_id := range attr_value_ids {
attr_name, _ := gredis.String(redisr.Do("HGET", config.Get("redis_all.class_attr_by_id").String(), attr_name_id)) //查询唯一值,反查sku_id
attr_value_name, _ := gredis.String(redisr.Do("HGET", config.Get("redis_all.class_attr_value_by_id").String(), attr_value_id)) //查询唯一值,反查sku_id
query.Must(elastic.NewNestedQuery("attrs", elastic.NewBoolQuery().Must(elastic.NewTermQuery("attrs.attr_name", attr_name), elastic.NewTermQuery("attrs.attr_value", attr_value_name))))
}
}
}
......@@ -64,10 +67,10 @@ func GetOptimumAttrQuery(ctx *gin.Context,REQUEST_ATTR *map[string][]string,type
source := elastic.NewSearchSource()
if types == 1 { //单独返回属性统计
source.Aggregation("brand_id",elastic.NewTermsAggregation().Field("brand_id").Size(1000))
source.Aggregation("attrs",elastic.NewNestedAggregation().Path("attrs").
SubAggregation("attr_name",elastic.NewTermsAggregation().Field("attrs.attr_name").Size(5000).
SubAggregation("attr_value",elastic.NewTermsAggregation().Field("attrs.attr_value").Size(5000))))
source.Aggregation("brand_id", elastic.NewTermsAggregation().Field("brand_id").Size(1000))
source.Aggregation("attrs", elastic.NewNestedAggregation().Path("attrs").
SubAggregation("attr_name", elastic.NewTermsAggregation().Field("attrs.attr_name").Size(5000).
SubAggregation("attr_value", elastic.NewTermsAggregation().Field("attrs.attr_value").Size(5000))))
source.Size(0)
}
if types == 2 { //不需要统计,返回分页数据
......@@ -75,38 +78,38 @@ func GetOptimumAttrQuery(ctx *gin.Context,REQUEST_ATTR *map[string][]string,type
page_size := ctx.Request.FormValue("page_size")
page_from := 0
if page == "" {
page = "1";
page = "1"
}
if page_size == "" {
page_size = "10";
page_size = "10"
}
if page != "1" {
page_from = (common.MyInt(page)-1)*common.MyInt(page_size)
if page_from >10000 {
page_from = 10000;
page_from = (common.MyInt(page) - 1) * common.MyInt(page_size)
if page_from > 10000 {
page_from = 10000
}
}
source.FetchSourceContext(elastic.NewFetchSourceContext(true).Include("goods_id","attrs")) //显示字段
source.FetchSourceContext(elastic.NewFetchSourceContext(true).Include("goods_id", "attrs")) //显示字段
source.Size(common.MyInt(page_size))
source.From(page_from)
source.Sort("supplier_name",true) //true asc,自营数据在前
source.Sort("sort",false) //true asc,可购买在前
source.Sort("supplier_name", true) //true asc,自营数据在前
source.Sort("sort", false) //true asc,可购买在前
//排序
if ctx.Request.FormValue("stock_rank") == "1" { //库存排序,最多库存最前
source.Sort("stock",false) //true asc
source.Sort("stock", false) //true asc
}
if ctx.Request.FormValue("stock_rank") == "2" { //库存排序,最少库存最前
source.Sort("stock",true) //true asc
source.Sort("stock", true) //true asc
}
if ctx.Request.FormValue("single_rank") == "1" { //价格最高在前
source.Sort("lower_price",false) //true asc
source.Sort("lower_price", false) //true asc
}
if ctx.Request.FormValue("single_rank") == "2" { //价格最高在后
source.Sort("lower_price",true) //true asc
source.Sort("lower_price", true) //true asc
}
}
......@@ -115,9 +118,10 @@ func GetOptimumAttrQuery(ctx *gin.Context,REQUEST_ATTR *map[string][]string,type
searchRequest := elastic.NewSearchRequest().Source(source)
param, _ = searchRequest.Body()
common.PrintDebugHtml(ctx,param)
common.PrintDebugHtml(ctx, param)
return
}
/*
上面函数最终拼接的es查询:
{
......@@ -239,6 +243,4 @@ func GetOptimumAttrQuery(ctx *gin.Context,REQUEST_ATTR *map[string][]string,type
"size": 10
}
*/
*/
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment