更新Elasticsearch中的索引文档 [英] Updating indexed document in Elasticsearch

查看:142
本文介绍了更新Elasticsearch中的索引文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解如何更新Elasticsearch中的索引文档。我不明白它是如何工作的? API指的是什么是 ctx ?让我们说你有一个嵌套文件的文档你要做什么来更新它?

I am trying to understand how you update an indexed document in Elasticsearch. I don't understand how it works? What is the ctx that the API is referring to doing? 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?

推荐答案

更新请求从elasticsearch检索源,修改它并将其重新指向弹性搜索。如果您已经有了使用更新的文档的副本没有意义。只是索引新版本通常会更快。但是,如果您没有文档容易使用,但您知道要对文档进行哪些更改,则使用更新可能会更有效。例如,如果我没有汽车文档的副本,但是我想添加一个新的创建者,我可以这样做:

The update request retrieve source from elasticsearch, modifies it and indices it back to elasticsearch. If you already have a copy of the document using update make little sense. It would be generally faster to just index the new version. However, if you don't have the document readily avilable 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

在更新脚本<​​code> ctx 是一个特殊变量,允许您访问要更新的对象的源。 ctx._source 是源的可写版本。您可以在脚本中修改此文档,修改的源将作为新版本的文档持久化。

In the update script ctx is a special variable that allows you to access the source of the object that you want to update. The ctx._source is writable version of the source. You can modify this document in the script and the modified source will be persisted as the new version of the document.

这篇关于更新Elasticsearch中的索引文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆