其他分享
首页 > 其他分享> > ElasticSearch的模糊匹配,过滤(按照属性,分类,品牌,价格区间,库存),排序,分页,高亮,聚合分析

ElasticSearch的模糊匹配,过滤(按照属性,分类,品牌,价格区间,库存),排序,分页,高亮,聚合分析

作者:互联网

ElasticSearch的模糊匹配,过滤(按照属性,分类,品牌,价格区间,库存),排序,分页,高亮,聚合分析

GET mall_product/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "skuTitle": "华为"
          }
        }
      ],
      "filter": [
        {
          "term": {
            "catalogId": "225"
          }
        },
        {
          "terms": {
            "brandId": [
              "1",
              "2",
              "3"
            ]
          }
        },
        {
          "nested": {
            "path": "attrs",
            "query": {
              "bool": {
                "must": [
                  {
                    "term": {
                      "attrs.attrId": {
                        "value": "1"
                      }
                    }
                  },
                  {
                    "terms": {
                      "attrs.attrValue": [
                        "2018",
                        "2018"
                      ]
                    }
                  }
                ]
              }
            }
          }
        },
        {
          "term": {
            "hasStock": {
              "value": "true"
            }
          }
        },
        {
          "range": {
            "skuPrice": {
              "gte": 0,
              "lte": 6000
            }
          }
        }
      ]
    }
  },
  "sort": [
    {
      "skuPrice": {
        "order": "desc"
      }
    }
  ],
  "from": 0,
  "size": 5,
  "highlight": {
    "fields": {"skuTitle":{}}, 
    "pre_tags": "<b style='color:red'>",
    "post_tags": "</b>"
  },
   "aggs": {
    "brand_agg": {
      "terms": {
        "field": "brandId",
        "size": 100
      },
      "aggs": {
        "brand_name_agg": {
          "terms": {
            "field": "brandName",
            "size": 100
          }
        },
        "brand_img_agg":{
          "terms": {
            "field": "brandImg",
            "size": 100
          }
        }
        
      }
    },
    "catalog_agg":{
      "terms": {
        "field": "catalogId",
        "size": 10
      },
      "aggs": {
        "catalog-name_agg": {
          "terms": {
            "field": "catalogName",
            "size": 10
          }
        }
      }
    },
    "attr_agg":{
      "nested": {
        "path": "attrs"
      },
      "aggs": {
        "attr_id_agg": {
          "terms": {
            "field": "attrs.attrId",
            "size": 10
          },
          "aggs": {
            "attr_name_agg": {
              "terms": {
                "field": "attrs.attrName",
                "size": 10
              }
            },
            "attr_value_agg":{
              "terms": {
                "field": "attrs.attrValue",
                "size": 10
              }
            }
          }
        }
      }
    }
  }
}

代码执行结果展示

{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "mall_product",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : null,
        "_source" : {
          "attrs" : [
            {
              "attrId" : 1,
              "attrName" : "入网年份",
              "attrValue" : "2018"
            },
            {
              "attrId" : 2,
              "attrName" : "上市年份",
              "attrValue" : "2018"
            }
          ],
          "brandId" : 2,
          "brandImg" : "https://mall-xiyoufusu.oss-cn-chengdu.aliyuncs.com/2021-09-06/7e07bf31-c30e-4c01-8ee3-2ae6fe4f11ed_huawei.png",
          "brandName" : "华为",
          "catalogId" : 225,
          "catalogName" : "手机",
          "hasStock" : true,
          "hotScore" : 0,
          "saleCount" : 0,
          "skuId" : 1,
          "skuImg" : "",
          "skuPrice" : 5900.0,
          "skuTitle" : "华为Mate30 黑色 8+168",
          "spuId" : 5
        },
        "highlight" : {
          "skuTitle" : [
            "<b style='color:red'>华为</b>Mate30 黑色 8+168"
          ]
        },
        "sort" : [
          "5900.0"
        ]
      }
    ]
  },
  "aggregations" : {
    "catalog_agg" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : 225,
          "doc_count" : 1,
          "catalog-name_agg" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "手机",
                "doc_count" : 1
              }
            ]
          }
        }
      ]
    },
    "attr_agg" : {
      "doc_count" : 2,
      "attr_id_agg" : {
        "doc_count_error_upper_bound" : 0,
        "sum_other_doc_count" : 0,
        "buckets" : [
          {
            "key" : 1,
            "doc_count" : 1,
            "attr_name_agg" : {
              "doc_count_error_upper_bound" : 0,
              "sum_other_doc_count" : 0,
              "buckets" : [
                {
                  "key" : "入网年份",
                  "doc_count" : 1
                }
              ]
            },
            "attr_value_agg" : {
              "doc_count_error_upper_bound" : 0,
              "sum_other_doc_count" : 0,
              "buckets" : [
                {
                  "key" : "2018",
                  "doc_count" : 1
                }
              ]
            }
          },
          {
            "key" : 2,
            "doc_count" : 1,
            "attr_name_agg" : {
              "doc_count_error_upper_bound" : 0,
              "sum_other_doc_count" : 0,
              "buckets" : [
                {
                  "key" : "上市年份",
                  "doc_count" : 1
                }
              ]
            },
            "attr_value_agg" : {
              "doc_count_error_upper_bound" : 0,
              "sum_other_doc_count" : 0,
              "buckets" : [
                {
                  "key" : "2018",
                  "doc_count" : 1
                }
              ]
            }
          }
        ]
      }
    },
    "brand_agg" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : 2,
          "doc_count" : 1,
          "brand_img_agg" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "https://mall-xiyoufusu.oss-cn-chengdu.aliyuncs.com/2021-09-06/7e07bf31-c30e-4c01-8ee3-2ae6fe4f11ed_huawei.png",
                "doc_count" : 1
              }
            ]
          },
          "brand_name_agg" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "华为",
                "doc_count" : 1
              }
            ]
          }
        }
      ]
    }
  }
}

标签:count,upper,高亮,agg,doc,sum,ElasticSearch,key,排序
来源: https://blog.csdn.net/weixin_51020546/article/details/120236298