问题描述
我有一个问题,我正在尝试更新ES中的对象,因此,每次查询它时,我都会获取所有更新的信息.我有一个像这样的对象:
I have a question, I am trying to update an object in ES, so, every time I query it I will get all the updated info. I have an object like this:
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 768,
"successful": 768,
"failed": 0
},
"hits": {
"total": 456,
"max_score": 1,
"hits": [
{
"_index": "sometype_1",
"_type": "sometype",
"_id": "12312321312312",
"_score": 1,
"_source": {
"readModel": {
"id": "asdfqwerzcxv",
"status": "active",
"hidden": false,
"message": "hello world",
},
"model": {
"id": "asdfqwerzcxv",
"content": {
"objectId": "421421312312",
"content": {
"@type": "text",
"text": "hello world"
}
..... //the rest of the object...
我想更新消息(读取模型的一部分),所以我做了这样的事情:
And I want to update the message (part of the read model), so I made something like this:
PUT test/readModel.id/ID123
{
"message" : "hello"
}
但是每次我查询ID123时,我都会得到相同的信息(更糟糕的是,我制作的PUT越多,返回的对象也就越多(具有相同的信息)
But everytime I query looking for ID123 I get same info (and even worse, the more PUTs I make the more objects I get back (with same info)
有什么想法吗?
推荐答案
如果只需要更新一个文档,则可以使用更新API 如下:
If there's only a single document that you need to update, you can use the Update API like this:
POST sometype_1/sometype/12312321312312/_update
{
"doc": {
"model.message": { ... your JSON object here... }
}
}
如果多个文档可以具有 readModel.id:asdfqwerzcxv
,并且您要使用相同的 message
更新所有文档,则需要使用通过查询API更新,例如__>
If several documents can have readModel.id: asdfqwerzcxv
and you want to update all of them with the same message
, then you need to use the Update by query API like this_
POST sometype_1/_update_by_query
{
"script": {
"source": "ctx._source.message = params.message",
"lang": "painless",
"params": {
"message": "hello"
}
},
"query": {
"match": {
"readModel.id": "asdfqwerzcxv"
}
}
}
这篇关于更新Elastic Search中的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!