如何在弹性搜索中将元素插入到已存在的列表中

How to insert an element into already present list in elastic search

假设我有如下存储的文件。

document 1
    {
        id : '1',
        title : "This is a test document1",
        list : ["value1" , "value2"],
        ...
    }
document 2
    {
        id : '2',
        title : "This is a test document2",
        valueList : ["value1" , "value2"],
        ...
        }

我需要使用批量 api 将更多元素添加到包含文档 ID 列表的文档中的 valueList。结果应该看起来像

document 1
    {
        id : '1',
        title : "This is a test document1",
        list : ["value1" , "value2", "value3"],
        ...
    }
document 2

    {
        id : '2',
        title : "This is a test document2",
        valueList : ["value1" , "value2" , "value3"],
        ...
        }

我该怎么做才能实现这一目标? 我尝试使用脚本,但它只更新了一个文档。

抱歉,我是弹性搜索的新手。在这个问题上我什至可能是愚蠢的。请原谅并让我清楚这个问题。

参见Updating Document。它应该是直截了当的。你需要使用 _update 并且只是为了给你一个想法,即使文档近乎完美,它可能看起来像这样:

POST /your_index/your_type/document1/_update

{
    id : '1',
    title : "This is a test document1",
    list : ["value1" , "value2", "value3"]
 }

这将更新 document1

如果是批量更新,您应该阅读 Batch Processing and have a look at the Bulk API

来自文档:

POST /your_index/your_type/_bulk
{ "update" : {"_id" : "document1", "_type" : "your_type", "_index" : "your_index"}}
{ "doc" : {"myfield" : "newvalue"} }
{ "update" : {"_id" : "document2", "_type" : "your_type", "_index" : "your_index"}}
{ "doc" : {"myfield" : "newvalue"} }

请注意,您可以只使用 _update 作为 Partial Updates

The simplest form of the update request accepts a partial document as the doc parameter, which just gets merged with the existing document. Objects are merged together, existing scalar fields are overwritten, and new fields are added.