Elasticsearch,无法删除嵌套字段中的字段

nx7onnlm  于 2023-10-17  发布在  ElasticSearch
关注(0)|答案(2)|浏览(150)

我有Map

{
  "candidate-index" : {
    "mappings" : {
      "properties" : {
        "provider_candidates" : {
          "type" : "nested",
          "properties" : {
            "foo" : {
              "type" : "object"
            },
            "group_key" : {
              "type" : "keyword"
            }
          }
        }
      }
    }
}

我要删除foo字段

POST /candidate-index/_update_by_query
 {
   "script" : "ctx._source.remove(\"provider_candidates.foo\")",
   "query": {
     "nested": {
       "path": "provider_candidates",
       "query": {
         "bool": {
           "must": [
             {
               "exists": {
                 "field": "provider_candidates.foo"
               }
             }
           ]

         }
       }
     }
   }
 }

它不工作。它不会生成错误,但不会删除该字段。
我知道查询部分是正确的,因为如果我把它转换成_search,它就能正确地找到文档
我也试

POST /candidate-index/_update_by_query
 {
   "script" : "ctx._source.provider_candidates.remove(\"foo\")",
   "query": {
     "nested": {
       "path": "provider_candidates",
       "query": {
         "bool": {
           "must": [
             {
               "exists": {
                 "field": "provider_candidates.foo"
               }
             }
           ]

         }
       }
     }
   }
 }

它说

{
  "error" : {
    "root_cause" : [
      {
        "type" : "script_exception",
        "reason" : "runtime error",
        "script_stack" : [
          "ctx._source.provider_candidates.remove(\"foo\")",
          "                               ^---- HERE"
        ],
        "script" : "ctx._source.provider_candidates.remove(\"foo\")",
        "lang" : "painless"
      }
    ],
    "type" : "script_exception",
    "reason" : "runtime error",
    "script_stack" : [
      "ctx._source.provider_candidates.remove(\"foo\")",
      "                               ^---- HERE"
    ],
    "script" : "ctx._source.provider_candidates.remove(\"foo\")",
    "lang" : "painless",
    "caused_by" : {
      "type" : "wrong_method_type_exception",
      "reason" : "cannot convert MethodHandle(List,int)Object to (Object,String)Object"
    }
  },
  "status" : 400
}
iyzzxitl

iyzzxitl1#

您需要循环provider_candidates字段,然后删除其中的字段

POST /index51/_update_by_query
 {
  "script" : "for (int i = 0; i < ctx._source.provider_candidates.length; ++i) { ctx._source.provider_candidates[i].remove(\"foo\") }",
   "query": {
     "nested": {
       "path": "provider_candidates",
       "query": {
         "bool": {
           "must": [
             {
               "exists": {
                 "field": "provider_candidates.foo"
               }
             }
           ]
         }
       }
     }
   }
 }
jhdbpxl9

jhdbpxl92#

忽略查询部分;您可以从索引中删除嵌套字段,如下所示:

POST index_name/_update_by_query
{
  "script": {
    "source": "ctx._source['field'].remove('nested_field')",
    "lang": "painless"
  },
  "query": {
    "match_all": {}
  }
}

相关问题