IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 大数据 -> 为什么es中nested数据must_not不生效? -> 正文阅读

[大数据]为什么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"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

无数据命中

  大数据 最新文章
实现Kafka至少消费一次
亚马逊云科技:还在苦于ETL?Zero ETL的时代
初探MapReduce
【SpringBoot框架篇】32.基于注解+redis实现
Elasticsearch:如何减少 Elasticsearch 集
Go redis操作
Redis面试题
专题五 Redis高并发场景
基于GBase8s和Calcite的多数据源查询
Redis——底层数据结构原理
上一篇文章      下一篇文章      查看所有文章
加:2022-04-28 11:56:15  更:2022-04-28 11:57:40 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 1:19:11-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码