首页 > 解决方案 > ElasticSearch 5.2 [script] 中 START_OBJECT 的未知键。要求

问题描述

您好我正在尝试通过脚本搜索数组的嵌套数据来删除记录。是否可以使用 _delete_by_query 的脚本删除数据?弹性搜索 5.2 版本

我的请求看起来像

POST /test_index/_delete_by_query
{
  "query": {
    "bool": {
      "must": [
        {
          "exists": {
            "field":"userPermission"
          }
        }
      ]
    }
  },
  "script":{
    "inline":"""
    for (int i = 0; i < ctx._source.userPermission.size(); i++) {
        if(ctx._source.userPermission[i].id == '760100000-100000')
        {
          return true
        }
    }
    return false
    """
  }
}

我收到一个错误:

{ "error": { "type": "parsing_exception", "reason": "[script] 中 START_OBJECT 的未知键。", "line": 1, "col": 77 }, "status": 400 }

这是数据示例:

{
    "_index": "test_index",
    "_type": "doc",
    "_id": "AXXDZFKKgDFBfUY9kVS6",
    "_score": 1,
    "_source": {
      "test_field": "Test",
      "userPermission": [
        {
          "fullName": "Test 55",
          "id": '760100000-100000'
        },
        {
          "fullName": "Test33",
          "id": 555
        },
        {
          "fullName": "Test 1",
          "id": 444
        }
      ]
    }
  }

标签: elasticsearch

解决方案


查询端点删除不支持任何脚本内容。您需要做的是使用查询端点的更新和delete满足条件的操作:

POST /test_index/_update_by_query
{
  "query": {
    "bool": {
      "must": [
        {
          "exists": {
            "field":"userPermission"
          }
        }
      ]
    }
  },
  "script":{
    "inline":"""
      for (int i = 0; i < ctx._source.userPermission.size(); i++) {
        if(ctx._source.userPermission[i].id == '760100000-100000')
        {
          ctx.op = 'delete';
        }
      }
    """
  }
}

推荐阅读