如何更新与elasticsearch中的查询匹配的多个文档 [英] How to update multiple documents that match a query in elasticsearch
问题描述
我的文档最初只包含url"(分析)和respsize"(not_analyzed)字段.我想更新与 url 匹配的文档并添加新字段类别"我是说;首先是 doc1:
I have documents which contains only "url"(analyzed) and "respsize"(not_analyzed) fields at first. I want to update documents that match the url and add new field "category" I mean; at first doc1:
{
"url":"http://stackoverflow.com/users/4005632/mehmet-yener-yilmaz",
"respsize":"500"
}
我有一个外部数据,我知道stackoverflow.com"属于第 10 类,我需要更新文档,并使其像:
I have an external data and I know "stackoverflow.com" belongs to category 10, And I need to update the doc, and make it like:
{
"url":"http://stackoverflow.com/users/4005632/mehmet-yener-yilmaz",
"respsize":"500",
"category":"10"
}
当然,我会在所有 url 字段包含stackoverflow.com"的文档中执行此操作我需要更新每个文档一次.. 因为 url 的类别数据是不可更改的,所以不需要再次更新.我需要使用带有 _version 号的 _update api 来检查它,但无法编写 dsl 查询.编辑我运行这个,看起来工作正常:但是文件没有改变..
Of course I will do this all documents which url fields has "stackoverflow.com" and I need the update each doc oly once.. Because category data of url is not changeable, no need to update again. I need to use _update api with _version number to check it but cant compose the dsl query. EDIT I run this and looks works fine: But documents not changed..
虽然查询结果看起来是真的,但新字段没有添加到文档中,需要刷新等吗?
Although query result looks true, new field not added to docs, need refresh or etc?
推荐答案
您可以使用 更新查询插件 就是为了做到这一点.这个想法是选择所有没有 category
并且其 url
匹配某个字符串的文档并添加你想要的类别.
You could use the update by query plugin in order to do just that. The idea is to select all document without a category
and whose url
matches a certain string and add the category you wish.
curl -XPOST 'localhost:9200/webproxylog/_update_by_query' -H "Content-Type: application/json" -d '
{
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"term": {
"url": "stackoverflow.com"
}
},
{
"missing": {
"field": "category"
}
}
]
}
}
}
},
"script" : "ctx._source.category = "10";"
}'
运行此命令后,所有带有 url: stackoverflow.com
且没有类别的文档都将获得 category: 10
.您可以稍后再次运行相同的查询以修复同时已编入索引的新 stackoverflow.com
文档.
After running this, all your documents with url: stackoverflow.com
that don't have a category, will get category: 10
. You can run the same query again later to fix new stackoverflow.com
documents that have been indexed in the meantime.
还要确保在 elasticsearch.yml
中启用脚本并重新启动 ES:
Also make sure to enable scripting in elasticsearch.yml
and restart ES:
script.inline: on
script.indexed: on
在脚本中,您可以随意添加任意数量的字段,例如
In the script, you're free to add as many fields as you want, e.g.
...
"script" : "ctx._source.category1 = "10"; ctx._source.category2 = "20";"
更新
ES 2.3 现在具有 更新通过查询功能.您仍然可以完全按原样使用上述查询,它会起作用(除了 filtered
和 missing
已被弃用,但仍然有效;).
ES 2.3 now features the update by query functionality. You can still use the above query exactly as is and it will work (except that filtered
and missing
are deprecated, but still working ;).
这篇关于如何更新与elasticsearch中的查询匹配的多个文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!