Elasticsearch 根据搜索查询更新整个“_source”字段

Elasticsearch update the whole `_source` field based on search query

"_source": {
         "id": "5b1676493d21784208c36041",
         "label": "name",
         "properties": {
           "name": "patrick"
         },
         "updatedAt": 1528259039542
       }

我想根据 id(而不是 _id)用新文档更新此文档。

像这样:

    "_source": {
             "dataSource": "ELASTIC",
             "entity": "vertices",
             "label": "vertices",
             "id": "5b1676493d21784208c36041",
             "properties": {
                     "name": "patrick"
                  },
             "updatedAt": 1528259039542
           }

elasticsearch 版本:6.2,ES Java api:6.2

你可以使用update by query API实现你想要的,基本上是这样的:

POST index/_update_by_query
{
  "query": {
    "match": {
      "id": "5b1676493d21784208c36041"
    }
  },
  "script": {
    "source": "ctx._source = params",
    "params": {
       "dataSource": "ELASTIC",
       "entity": "vertices",
       "label": "vertices"
    }
  }
}

更新:使用 Java API

Map<String, String> params = new HashMap<>();
params.put("dataSource", "ELASTIC");
params.put("entity", "vertices");
params.put("label", "vertices");

UpdateByQueryRequestBuilder updateByQuery = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);
updateByQuery.source("index")
    .filter(QueryBuilders.matchQuery("id", "5b1676493d21784208c36041"))
    .size(1000)
    .script(new Script(ScriptType.INLINE, "painless", "ctx._source.putAll(params)", params));
BulkByScrollResponse response = updateByQuery.get();

有关使用 UpdateByQuery Java API and Java high level rest client

的更多详细信息

"ctx._source.putAll(params)" 是一个很好的尝试,但不幸的是它移动了 _source.ctx 下的所有现有字段。

所以,以下对我有用(ES 6.1):

"for (k in params.keySet()){if (!k.equals('ctx')){ctx._source.put(k, params.get(k))}}"

感谢@jetnet 提供循环遍历地图中所有条目的想法。当我使用该脚本时,它仍在替换整个对象,因此必须按照以下内容对其进行调整以保留更新操作期间未提供的字段。对于上下文,如果我首先插入一个包含三个对象的文档,每个对象有 2 个字段,如果我随后用相同的 3 个对象更新该文档,但每个对象有 1 个字段,那么输出结果是我最终得到一个包含 3 个对象的文档有 1 个字段,而不是我原来的 3 个对象,每个对象有 2 个字段。

我必须修改@jetnet 的脚本才能获得不覆盖对象属性的预期结果

POST /transaction_index/_update/33384637-3137-3132-5543-31304c4c3151
{
   "script": {
      "source": "if (ctx._source.Metadata == null || params.Metadata.Version >= ctx._source.Metadata.Version) { for (k in params.keySet()){ if (ctx._source[k] != null) { ctx._source[k].putAll(params.get(k)) } else { ctx._source.put(k, params.get(k)) } } } else { ctx.op = 'none' }",
      "params": {
         "Transaction": {
            "TransactionId": "33384637-3137-3132-5543-31304c4c3151",
            "TransactionKey": "Key1"
         },
         "Message": {
            "MessageId": "505a5953-374a-385a-4a48-52353549445a",
            "Context": "This is a test context"
         },
         "MessageDefinition": {
            "MessageDefinitionId": "a1c05e06-fa6b-40ce-992f-d083ff6c0243",
            "Code": 1010101010
         },
         "Metadata": {
            "Version": 1,
            "CreateTime": "2020-09-04T14:27:51.1986439+01:00",
            "IsLatest": true,
            "IsDummy": false,
            "VersionString": "20200903111111"
         }
    }
 },
 "scripted_upsert": true,
 "upsert": {}
}