elasticsearch 5.2[script]中start\u对象的未知键请求

axr492tv  于 2021-06-10  发布在  ElasticSearch
关注(0)|答案(1)|浏览(413)

你好,我正试图通过脚本在数组的嵌套数据中搜索来删除记录。是否可以使用脚本删除数据,以便按查询删除?elasticsearch 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\u exception”,“reason”:“在[script]中启动\u对象的未知键。”,“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
        }
      ]
    }
  }
mbzjlibv

mbzjlibv1#

“按查询删除”终结点不支持任何脚本内容。您需要做的是使用updatebyquery端点和 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';
        }
      }
    """
  }
}

相关问题