为什么es中nested数据must_not不生效?

一、问题描述

在使用ElasticSearch进行查询的时候,发现nested数据在使用must_not时不生效,可以查出所有数据。

index中的mapping

{
  "product" : {
    "mappings" : {
      "properties" : {
        "attrs" : {
          "type" : "nested",
          "properties" : {
            "attrId" : {
              "type" : "long"
            },
            "attrName" : {
              "type" : "keyword"
            },
            "attrValue" : {
              "type" : "keyword"
            }
          }
        },
        "brandId" : {
          "type" : "long"
        },
        "brandImg" : {
          "type" : "keyword"
        },
        "brandName" : {
          "type" : "keyword"
        },
        "catalogId" : {
          "type" : "long"
        },
        "catalogName" : {
          "type" : "keyword"
        },
        "catelogId" : {
          "type" : "long"
        },
        "hasStock" : {
          "type" : "boolean"
        },
        "hotScore" : {
          "type" : "long"
        },
        "saleCount" : {
          "type" : "long"
        },
        "skuId" : {
          "type" : "long"
        },
        "skuImg" : {
          "type" : "keyword"
        },
        "skuPrice" : {
          "type" : "keyword"
        },
        "skuTitle" : {
          "type" : "text",
          "analyzer" : "ik_smart"
        },
        "spuId" : {
          "type" : "keyword"
        }
      }
    }
  }
}

index中的数据

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 9,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "product",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "attrs" : [
            {
              "attrId" : 2,
              "attrName" : "上市年份",
              "attrValue" : "2020"
            },
            {
              "attrId" : 3,
              "attrName" : "颜色",
              "attrValue" : "流光幻镜"
            },
            {
              "attrId" : 1,
              "attrName" : "入网参数",
              "attrValue" : "5G"
            },
            {
              "attrId" : 9,
              "attrName" : "电池容量",
              "attrValue" : "5000mAh"
            },
            {
              "attrId" : 10,
              "attrName" : "机身长度(mm)",
              "attrValue" : "168"
            },
            {
              "attrId" : 7,
              "attrName" : "CPU型号",
              "attrValue" : "麒麟990"
            },
            {
              "attrId" : 8,
              "attrName" : "CPU工艺",
              "attrValue" : "5nm"
            },
            {
              "attrId" : 12,
              "attrName" : "CPU品牌",
              "attrValue" : "海思(Hisilicon)"
            }
          ],
          "brandId" : 2,
          "brandImg" : "https://caiflower.oss-cn-beijing.aliyuncs.com/2022-04-26/32830b43-4eee-45e0-8acc-a165d51f65cb_Huawei.svg.png",
          "brandName" : "华为",
          "catalogId" : 225,
          "catalogName" : "手机",
          "hasStock" : true,
          "hotScore" : 0,
          "saleCount" : 2654,
          "skuId" : 1,
          "skuImg" : "https://mall-fire.oss-cn-shenzhen.aliyuncs.com/2020-06-05/e5ba78ec-9cf5-4bbd-86a7-ad2f611e2512_图集1.jpg",
          "skuPrice" : 8888.0,
          "skuTitle" : "华为 HUAWEI P40 Pro+ 麒麟990 5G  流光幻镜 套餐二 麒麟990 5G SoC芯片 5000万超感知徕卡五摄 100倍双目变焦 全网通5G",
          "spuId" : 3
        }
      },
      {
        "_index" : "product",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "attrs" : [
            {
              "attrId" : 2,
              "attrName" : "上市年份",
              "attrValue" : "2020"
            },
            {
              "attrId" : 3,
              "attrName" : "颜色",
              "attrValue" : "流光幻镜"
            },
            {
              "attrId" : 1,
              "attrName" : "入网参数",
              "attrValue" : "5G"
            },
            {
              "attrId" : 9,
              "attrName" : "电池容量",
              "attrValue" : "5000mAh"
            },
            {
              "attrId" : 10,
              "attrName" : "机身长度(mm)",
              "attrValue" : "168"
            },
            {
              "attrId" : 7,
              "attrName" : "CPU型号",
              "attrValue" : "麒麟990"
            },
            {
              "attrId" : 8,
              "attrName" : "CPU工艺",
              "attrValue" : "5nm"
            },
            {
              "attrId" : 12,
              "attrName" : "CPU品牌",
              "attrValue" : "海思(Hisilicon)"
            }
          ],
          "brandId" : 2,
          "brandImg" : "https://caiflower.oss-cn-beijing.aliyuncs.com/2022-04-26/32830b43-4eee-45e0-8acc-a165d51f65cb_Huawei.svg.png",
          "brandName" : "华为",
          "catalogId" : 225,
          "catalogName" : "手机",
          "hasStock" : true,
          "hotScore" : 0,
          "saleCount" : 1454,
          "skuId" : 2,
          "skuImg" : "https://mall-fire.oss-cn-shenzhen.aliyuncs.com/2020-06-05/9915d4a7-9f73-4c71-85cc-65c0b7f644dd_图集2.jpg",
          "skuPrice" : 8888.0,
          "skuTitle" : "华为 HUAWEI P40 Pro+ 麒麟990 5G  流光幻镜 套餐二 麒麟990 5G SoC芯片 5000万超感知徕卡五摄 100倍双目变焦 全网通5G",
          "spuId" : 3
        }
      },
      {
        "_index" : "product",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "attrs" : [
            {
              "attrId" : 2,
              "attrName" : "上市年份",
              "attrValue" : "2020"
            },
            {
              "attrId" : 3,
              "attrName" : "颜色",
              "attrValue" : "流光幻镜"
            },
            {
              "attrId" : 1,
              "attrName" : "入网参数",
              "attrValue" : "5G"
            },
            {
              "attrId" : 9,
              "attrName" : "电池容量",
              "attrValue" : "5000mAh"
            },
            {
              "attrId" : 10,
              "attrName" : "机身长度(mm)",
              "attrValue" : "168"
            },
            {
              "attrId" : 7,
              "attrName" : "CPU型号",
              "attrValue" : "麒麟990"
            },
            {
              "attrId" : 8,
              "attrName" : "CPU工艺",
              "attrValue" : "5nm"
            },
            {
              "attrId" : 12,
              "attrName" : "CPU品牌",
              "attrValue" : "海思(Hisilicon)"
            }
          ],
          "brandId" : 2,
          "brandImg" : "https://caiflower.oss-cn-beijing.aliyuncs.com/2022-04-26/32830b43-4eee-45e0-8acc-a165d51f65cb_Huawei.svg.png",
          "brandName" : "华为",
          "catalogId" : 225,
          "catalogName" : "手机",
          "hasStock" : true,
          "hotScore" : 0,
          "saleCount" : 1673,
          "skuId" : 3,
          "skuImg" : "https://mall-fire.oss-cn-shenzhen.aliyuncs.com/2020-06-05/4d2ed8b5-5504-4bad-8620-75db9c29d3ea_图集4.jpg",
          "skuPrice" : 5988.0,
          "skuTitle" : "华为 HUAWEI P40 Pro+ 麒麟990 5G  流光幻镜 套餐二 麒麟990 5G SoC芯片 5000万超感知徕卡五摄 100倍双目变焦 全网通5G",
          "spuId" : 3
        }
      },
      {
        "_index" : "product",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "attrs" : [
            {
              "attrId" : 2,
              "attrName" : "上市年份",
              "attrValue" : "2020"
            },
            {
              "attrId" : 3,
              "attrName" : "颜色",
              "attrValue" : "流光幻镜"
            },
            {
              "attrId" : 1,
              "attrName" : "入网参数",
              "attrValue" : "5G"
            },
            {
              "attrId" : 9,
              "attrName" : "电池容量",
              "attrValue" : "5000mAh"
            },
            {
              "attrId" : 10,
              "attrName" : "机身长度(mm)",
              "attrValue" : "168"
            },
            {
              "attrId" : 7,
              "attrName" : "CPU型号",
              "attrValue" : "麒麟990"
            },
            {
              "attrId" : 8,
              "attrName" : "CPU工艺",
              "attrValue" : "5nm"
            },
            {
              "attrId" : 12,
              "attrName" : "CPU品牌",
              "attrValue" : "海思(Hisilicon)"
            }
          ],
          "brandId" : 2,
          "brandImg" : "https://caiflower.oss-cn-beijing.aliyuncs.com/2022-04-26/32830b43-4eee-45e0-8acc-a165d51f65cb_Huawei.svg.png",
          "brandName" : "华为",
          "catalogId" : 225,
          "catalogName" : "手机",
          "hasStock" : true,
          "hotScore" : 0,
          "saleCount" : 3587,
          "skuId" : 4,
          "skuImg" : "https://mall-fire.oss-cn-shenzhen.aliyuncs.com/2020-06-05/55871b9f-589b-4957-8eb3-9d2c8698406b_图集6.jpg",
          "skuPrice" : 5988.0,
          "skuTitle" : "华为 HUAWEI P40 Pro+ 麒麟990 5G  霓影紫 套餐二 麒麟990 5G SoC芯片 5000万超感知徕卡四摄 50倍数字变焦  全网通5G手机",
          "spuId" : 3
        }
      },
      {
        "_index" : "product",
        "_type" : "_doc",
        "_id" : "5",
        "_score" : 1.0,
        "_source" : {
          "attrs" : [
            {
              "attrId" : 2,
              "attrName" : "上市年份",
              "attrValue" : "2020"
            },
            {
              "attrId" : 3,
              "attrName" : "颜色",
              "attrValue" : "流光幻镜"
            },
            {
              "attrId" : 1,
              "attrName" : "入网参数",
              "attrValue" : "5G"
            },
            {
              "attrId" : 9,
              "attrName" : "电池容量",
              "attrValue" : "5000mAh"
            },
            {
              "attrId" : 10,
              "attrName" : "机身长度(mm)",
              "attrValue" : "168"
            },
            {
              "attrId" : 7,
              "attrName" : "CPU型号",
              "attrValue" : "麒麟990"
            },
            {
              "attrId" : 8,
              "attrName" : "CPU工艺",
              "attrValue" : "5nm"
            },
            {
              "attrId" : 12,
              "attrName" : "CPU品牌",
              "attrValue" : "海思(Hisilicon)"
            }
          ],
          "brandId" : 2,
          "brandImg" : "https://caiflower.oss-cn-beijing.aliyuncs.com/2022-04-26/32830b43-4eee-45e0-8acc-a165d51f65cb_Huawei.svg.png",
          "brandName" : "华为",
          "catalogId" : 225,
          "catalogName" : "手机",
          "hasStock" : true,
          "hotScore" : 0,
          "saleCount" : 1235,
          "skuId" : 5,
          "skuImg" : "https://mall-fire.oss-cn-shenzhen.aliyuncs.com/2020-06-05/0bb896b8-fee8-4933-877c-2c4615c802d6_图集7.jpg",
          "skuPrice" : 5988.0,
          "skuTitle" : "华为 HUAWEI P40 Pro+ 麒麟990 5G  霓影紫 套餐二 麒麟990 5G SoC芯片 5000万超感知徕卡四摄 50倍数字变焦  全网通5G手机",
          "spuId" : 3
        }
      },
      {
        "_index" : "product",
        "_type" : "_doc",
        "_id" : "6",
        "_score" : 1.0,
        "_source" : {
          "attrs" : [
            {
              "attrId" : 2,
              "attrName" : "上市年份",
              "attrValue" : "2020"
            },
            {
              "attrId" : 3,
              "attrName" : "颜色",
              "attrValue" : "流光幻镜"
            },
            {
              "attrId" : 1,
              "attrName" : "入网参数",
              "attrValue" : "5G"
            },
            {
              "attrId" : 9,
              "attrName" : "电池容量",
              "attrValue" : "5000mAh"
            },
            {
              "attrId" : 10,
              "attrName" : "机身长度(mm)",
              "attrValue" : "168"
            },
            {
              "attrId" : 7,
              "attrName" : "CPU型号",
              "attrValue" : "麒麟990"
            },
            {
              "attrId" : 8,
              "attrName" : "CPU工艺",
              "attrValue" : "5nm"
            },
            {
              "attrId" : 12,
              "attrName" : "CPU品牌",
              "attrValue" : "海思(Hisilicon)"
            }
          ],
          "brandId" : 2,
          "brandImg" : "https://caiflower.oss-cn-beijing.aliyuncs.com/2022-04-26/32830b43-4eee-45e0-8acc-a165d51f65cb_Huawei.svg.png",
          "brandName" : "华为",
          "catalogId" : 225,
          "catalogName" : "手机",
          "hasStock" : true,
          "hotScore" : 0,
          "saleCount" : 1456,
          "skuId" : 6,
          "skuImg" : "https://mall-fire.oss-cn-shenzhen.aliyuncs.com/2020-06-05/72b3962b-6a79-4d16-8c83-2f3f61dba176_图集5.jpg",
          "skuPrice" : 5988.0,
          "skuTitle" : "华为 HUAWEI P40 Pro+ 麒麟990 5G  霓影紫 套餐二 麒麟990 5G SoC芯片 5000万超感知徕卡四摄 50倍数字变焦  全网通5G手机",
          "spuId" : 3
        }
      },
      {
        "_index" : "product",
        "_type" : "_doc",
        "_id" : "7",
        "_score" : 1.0,
        "_source" : {
          "attrs" : [
            {
              "attrId" : 2,
              "attrName" : "上市年份",
              "attrValue" : "2020"
            },
            {
              "attrId" : 3,
              "attrName" : "颜色",
              "attrValue" : "流光幻镜"
            },
            {
              "attrId" : 1,
              "attrName" : "入网参数",
              "attrValue" : "5G"
            },
            {
              "attrId" : 9,
              "attrName" : "电池容量",
              "attrValue" : "5000mAh"
            },
            {
              "attrId" : 10,
              "attrName" : "机身长度(mm)",
              "attrValue" : "168"
            },
            {
              "attrId" : 7,
              "attrName" : "CPU型号",
              "attrValue" : "麒麟990"
            },
            {
              "attrId" : 8,
              "attrName" : "CPU工艺",
              "attrValue" : "5nm"
            },
            {
              "attrId" : 12,
              "attrName" : "CPU品牌",
              "attrValue" : "海思(Hisilicon)"
            }
          ],
          "brandId" : 2,
          "brandImg" : "https://caiflower.oss-cn-beijing.aliyuncs.com/2022-04-26/32830b43-4eee-45e0-8acc-a165d51f65cb_Huawei.svg.png",
          "brandName" : "华为",
          "catalogId" : 225,
          "catalogName" : "手机",
          "hasStock" : true,
          "hotScore" : 0,
          "saleCount" : 2354,
          "skuId" : 7,
          "skuImg" : "https://mall-fire.oss-cn-shenzhen.aliyuncs.com/2020-06-05/9915d4a7-9f73-4c71-85cc-65c0b7f644dd_图集2.jpg",
          "skuPrice" : 5988.0,
          "skuTitle" : "华为 HUAWEI P40 Pro+ 麒麟990 5G  钛空银 套餐二 【白条12期免息送碎屏险】华为 P40 Pro 5G手机 分期",
          "spuId" : 3
        }
      },
      {
        "_index" : "product",
        "_type" : "_doc",
        "_id" : "8",
        "_score" : 1.0,
        "_source" : {
          "attrs" : [
            {
              "attrId" : 2,
              "attrName" : "上市年份",
              "attrValue" : "2020"
            },
            {
              "attrId" : 3,
              "attrName" : "颜色",
              "attrValue" : "流光幻镜"
            },
            {
              "attrId" : 1,
              "attrName" : "入网参数",
              "attrValue" : "5G"
            },
            {
              "attrId" : 9,
              "attrName" : "电池容量",
              "attrValue" : "5000mAh"
            },
            {
              "attrId" : 10,
              "attrName" : "机身长度(mm)",
              "attrValue" : "168"
            },
            {
              "attrId" : 7,
              "attrName" : "CPU型号",
              "attrValue" : "麒麟990"
            },
            {
              "attrId" : 8,
              "attrName" : "CPU工艺",
              "attrValue" : "5nm"
            },
            {
              "attrId" : 12,
              "attrName" : "CPU品牌",
              "attrValue" : "海思(Hisilicon)"
            }
          ],
          "brandId" : 2,
          "brandImg" : "https://caiflower.oss-cn-beijing.aliyuncs.com/2022-04-26/32830b43-4eee-45e0-8acc-a165d51f65cb_Huawei.svg.png",
          "brandName" : "华为",
          "catalogId" : 225,
          "catalogName" : "手机",
          "hasStock" : true,
          "hotScore" : 0,
          "saleCount" : 2546,
          "skuId" : 8,
          "skuImg" : "https://mall-fire.oss-cn-shenzhen.aliyuncs.com/2020-06-05/0bb896b8-fee8-4933-877c-2c4615c802d6_图集7.jpg",
          "skuPrice" : 5988.0,
          "skuTitle" : "华为 HUAWEI P40 Pro+ 麒麟990 5G  钛空银 套餐三 【白条12期免息送碎屏险】华为 P40 Pro 5G手机 分期",
          "spuId" : 3
        }
      },
      {
        "_index" : "product",
        "_type" : "_doc",
        "_id" : "9",
        "_score" : 1.0,
        "_source" : {
          "attrs" : [
            {
              "attrId" : 2,
              "attrName" : "上市年份",
              "attrValue" : "2020"
            },
            {
              "attrId" : 3,
              "attrName" : "颜色",
              "attrValue" : "流光幻镜"
            },
            {
              "attrId" : 1,
              "attrName" : "入网参数",
              "attrValue" : "5G"
            },
            {
              "attrId" : 9,
              "attrName" : "电池容量",
              "attrValue" : "5000mAh"
            },
            {
              "attrId" : 10,
              "attrName" : "机身长度(mm)",
              "attrValue" : "168"
            },
            {
              "attrId" : 7,
              "attrName" : "CPU型号",
              "attrValue" : "麒麟990"
            },
            {
              "attrId" : 8,
              "attrName" : "CPU工艺",
              "attrValue" : "5nm"
            },
            {
              "attrId" : 12,
              "attrName" : "CPU品牌",
              "attrValue" : "海思(Hisilicon)"
            }
          ],
          "brandId" : 2,
          "brandImg" : "https://caiflower.oss-cn-beijing.aliyuncs.com/2022-04-26/32830b43-4eee-45e0-8acc-a165d51f65cb_Huawei.svg.png",
          "brandName" : "华为",
          "catalogId" : 225,
          "catalogName" : "手机",
          "hasStock" : true,
          "hotScore" : 0,
          "saleCount" : 957,
          "skuId" : 9,
          "skuImg" : "https://mall-fire.oss-cn-shenzhen.aliyuncs.com/2020-06-05/e5ba78ec-9cf5-4bbd-86a7-ad2f611e2512_图集1.jpg",
          "skuPrice" : 4188.0,
          "skuTitle" : "华为 HUAWEI P40 Pro+ 麒麟990 5G  钛空银 套餐一 【白条12期免息送碎屏险】华为 P40 Pro 5G手机 分期",
          "spuId" : 3
        }
      }
    ]
  }
}

二、错误示例

GET product/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "attrs",
            "query": {
              "bool": {
                "must_not": [
                  {
                    "term": {
                      "attrs.attrId": 7
                    }
                  },
                  {
                    "term": {
                      "attrs.attrValue": "麒麟990"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

结果数据全部命中

三、正确示例

GET product/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "nested": {
            "path": "attrs",
            "query": {
              "bool": {
                "must": [
                  {
                    "term": {
                      "attrs.attrId": 7
                    }
                  },
                  {
                    "term": {
                      "attrs.attrValue": "麒麟990"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

无数据命中

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值