【问题标题】:Must_not in bool query?Must_not 在布尔查询中?
【发布时间】:2017-06-25 18:36:28
【问题描述】:

用例:我想删除所有没有特定 id 的文档。

映射:

"bookList": {
                  "properties": {
                     "bookname": {
                        "type": "string"
                     },
                     "bookId": {
                        "type": "long"
                     },
}

每个文档中的内容都是这样的:有些有内部列表,让它成为DOC1

"bookList": [
                  {
                     "bookname": "HITLER",
                     "bookId": 3163
                   },
                   {
                       "bookname": "MARTIAN", 
                       "bookId": 1210
                   }
                   ]

有些只有一个这样的列表让它成为DOC2

"bookList": [
                  {
                     "bookname": "WASHINGTON",
                     "bookId": 3163
                   }
                   ]

还有另一个文档 DOC3:

 "bookList": [
                      {
                         "bookname": "SHELDON",
                         "bookId": 3163
                       },
                       {
                           "bookname": "MARVELS", 
                           "bookId": 1219
                       }
                       ]

我在 ES 2.4.0 中使用 delete_by_query 插件来删除没有 bookId : 3163 在 bool query 中使用 MUST_NOT 的文档。对于内部列表文档(即 DOC1 和 DOC3),它不会删除它们,因为如果该术语存在于任一内部列表中,则 MUST_NOT 找到确切的术语,那么它不会删除该文档。

我的查询是这样的:

{
    "query" : {
    "bool" : {
        "must_not" : {
            "term" : {
        "bookList.bookId" :3163 
            }
    }
    }
}
}

此查询仅删除 DOC2,但不删除 DOC1。 我怎样才能删除内部列表文档?

【问题讨论】:

  • 改用通配符查询
  • 你的意思是book_Id的通配符查询?因为对于 book_ID,我必须使用唯一 ID
  • 您想一次删除多个唯一的 bookId 吗?
  • 是的,我想删除多个 bookIds

标签: elasticsearch


【解决方案1】:

我认为你应该使用过滤器上下文。

在过滤器上下文中,查询子句回答“这样做 文档匹配这个查询子句?”答案很简单,是或 否 — 不计算分数。 (来自:documentation

请参阅Elastic 上的文档。

PUT _template/stackoverflow
{
  "template": "stackoverflow",
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "bookList": {
      "properties": {
        "bookname": {
          "type": "text"
        },
        "bookId": {
          "type": "long"
        }
      }
    }
  }
}


POST stackoverflow/bookList/1
{
  "bookList": [
    {
      "bookname": "HITLER",
      "bookId": 3163
    },
    {
      "bookname": "MARTIAN",
      "bookId": 1210
    }
  ]
}

POST stackoverflow/bookList/2
{
  "bookList": [
    {
      "bookname": "WASHINGTON",
      "bookId": 3163
    }
  ]
}

POST stackoverflow/bookList/3
{
  "bookList": [
    {
      "bookname": "SHELDON",
      "bookId": 3163
    },
    {
      "bookname": "MARVELS",
      "bookId": 1219
    }
  ]
}

POST stackoverflow/bookList/4
{
  "bookList": [
    {
      "bookname": "SHELDON",
      "bookId": 3164
    },
    {
      "bookname": "MARVELS",
      "bookId": 1220
    }
  ]
}

执行:

GET stackoverflow/bookList/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "bool": {
          "must_not": {
            "term": {
              "bookList.bookId": 3163
            }
          }
        }
      }
    }
  }
}

结果:

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "stackoverflow",
        "_type": "bookList",
        "_id": "4",
        "_score": 1,
        "_source": {
          "bookList": [
            {
              "bookname": "SHELDON",
              "bookId": 3164
            },
            {
              "bookname": "MARVELS",
              "bookId": 1220
            }
          ]
        }
      }
    ]
  }
}

执行:

GET stackoverflow/_search
{
  "query": {
    "bool": {
      "must": {
        "term": {
          "bookList.bookId": 3163
        }
      }
    }
  }
}

结果:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
      {
        "_index": "stackoverflow",
        "_type": "bookList",
        "_id": "1",
        "_score": 1,
        "_source": {
          "bookList": [
            {
              "bookname": "HITLER",
              "bookId": 3163
            },
            {
              "bookname": "MARTIAN",
              "bookId": 1210
            }
          ]
        }
      },
      {
        "_index": "stackoverflow",
        "_type": "bookList",
        "_id": "2",
        "_score": 1,
        "_source": {
          "bookList": [
            {
              "bookname": "WASHINGTON",
              "bookId": 3163
            }
          ]
        }
      },
      {
        "_index": "stackoverflow",
        "_type": "bookList",
        "_id": "3",
        "_score": 1,
        "_source": {
          "bookList": [
            {
              "bookname": "SHELDON",
              "bookId": 3163
            },
            {
              "bookname": "MARVELS",
              "bookId": 1219
            }
          ]
        }
      }
    ]
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-18
    • 2010-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-06
    • 2020-06-22
    相关资源
    最近更新 更多