首页 > 解决方案 > Elasticsearch POST /delete_by_query 不起作用

问题描述

我尝试使用 POST /delete_by_query 删除 Elasticsearch 中的文档,但它在执行时没有删除文档,但没有引发错误。有趣的是它适用于某些索引,但不适用于其他索引。发生这种情况有什么原因吗?

POST /my-index/_delete_by_query?slices=1&requests_per_second=-1&ignore_unavailable=false&expand_wildcards=open&allow_no_indices=true&ignore_throttled=true&refresh=true&conflicts=proceed&wait_for_completion=true
{
  "size": 1000,
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "must": [
              {
                "terms": {
                  "myAttributeKey1": [
                    "myAttributeValue1"
                  ],
                  "boost": 1
                }
              }
            ],
            "adjust_pure_negative": true,
            "boost": 1
          }
        },
        {
          "query_string": {
            "query": "myAttributeValue2",
            "fields": [
              "myAttributeKey2"
            ],
            "type": "best_fields",
            "default_operator": "and",
            "max_determinized_states": 10000,
            "enable_position_increments": true,
            "fuzziness": "AUTO",
            "fuzzy_prefix_length": 0,
            "fuzzy_max_expansions": 50,
            "phrase_slop": 0,
            "escape": false,
            "auto_generate_synonyms_phrase_query": true,
            "fuzzy_transpositions": true,
            "boost": 1
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1
    }
  },
  "_source": false
}

标签: elasticsearch

解决方案


事实证明,索引映射是问题所在。下面是索引映射。我应该使用 termmyAttributeKey1.keyword而不是myAttributeKey1可以通过修复索引映射来解决。

{
  "mapping": {
    "_doc": {
      "properties": {
        "myAttributeKey1": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        }
      }
    }
  }
}

这里作为您可能想要的映射的比较

{
  "mapping": {
    "_doc": {
      "properties": {
        "_class": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "myAttributeKey1": {
          "type": "keyword"
        }
      }
    }
  }
}

推荐阅读