简体   繁体   English

在Elasticsearch中更新索引文档

[英]Updating indexed document in Elasticsearch

I am trying to understand how you update an indexed document in Elasticsearch. 我试图了解如何更新Elasticsearch中的索引文档。 I don't understand how it works? 我不明白它是如何工作的? What is the ctx that the API is referring to doing? API指的是什么ctx Let say you have a document with nested documents what do you have to do to update it? 假设您有一个包含嵌套文档的文档,您需要做些什么来更新它?

And what is the difference between deleting the document and then index the "updated" version, vs a plain update? 删除文档然后索引“更新”版本与普通更新之间有什么区别?

The update request retrieve source from Elasticsearch, modifies it and indexes it back to Elasticsearch. 更新请求从Elasticsearch检索源,修改它并将其索引回Elasticsearch。 If you already have a copy of the document using update makes little sense. 如果您已经拥有使用更新的文档副本没有多大意义。 It would be generally faster to just index the new version. 将新版本编入索引通常会更快。 However, if you don't have the document readily available but you know which changes you would like to make to the document, it might be more efficient to use update. 但是,如果您没有随时可用的文档但是您知道要对文档进行哪些更改,则使用更新可能更有效。

For example, if I don't have a copy of the car document, but I want to add a new creator I can do something like this: 例如,如果我没有汽车文档的副本,但我想添加一个新的创建者,我可以这样做:

curl -XDELETE localhost:9200/test

curl -XPUT localhost:9200/test -d '{
    "settings": {
        "index.number_of_shards": 1,
        "index.number_of_replicas": 0
    },
    "mappings": {
        "car": {
            "properties": {
                "creators" : {
                    "type": "nested",
                    "properties": {
                        "name": {"type":"string"}
                    }
                }
            }
        }
    }
}
'

curl -XPOST localhost:9200/test/car/1 -d '{
    "creators": [{
        "name": "Steve"
    }]
}
'

echo
curl -XPOST localhost:9200/test/car/1/_update -d '{
    "script" : "ctx._source.creators += new_creator",
    "params" : {
        "new_creator" : {"name": "John"}
    }
}'

echo
curl "localhost:9200/test/car/1?pretty=true"
echo

In the update script ctx is a special variable that allows you to access the source of the object that you want to update. 在更新脚本中, ctx是一个特殊变量,允许您访问要更新的对象的源。 The ctx._source is a writable version of the source. ctx._source是源的可写版本。 You can modify this document in the script and the modified source will be persisted as the new version of the document. 您可以在脚本中修改此文档,修改后的源将作为新版本的文档保留。

Here is how to update a document with nested customer documents in ElasticSearch 7.3: 以下是如何使用ElasticSearch 7.3中的嵌套客户文档更新文档:

POST /myindex/_doc/mypartid/_update
{
    "script" : {
        "source": "ctx._source.customers.add(params.newcust)",
        "params" : {
            "newcust" : {"customer": "cust3"}
        }
    }
}

Results in: 结果是:

GET /myindex/_doc/mypartid
{
    "_index": "myindex2",
    "_type": "_doc",
    "_id": "mypartid",
    ...
    "_source": {
        "part": "my part",
        "customers": [
            {"customer": "cust1"},
            {"customer": "cust3"}
        ],
        "machines": [
            {"machine": "mach7"},
            {"machine": "mach2"}
        ]
    }
}

So, for ES7+ the URL has changed as have the way you do scripts and which operations an array can have ( += does not work). 因此,对于ES7 +,URL已经改变,就像你编写脚本的方式和数组可以拥有的操作一样( +=不起作用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM