更新时间:2023-02-19 09:21:15
您可以使用通过查询插件更新为了做到这一点。这个想法是选择所有没有类别
的文档,其 url
匹配某个字符串并添加所需的类别。
curl -XPOST'localhost:9200 / webproxylog / _update_by_query'-d'
{
查询:{
filtered:{
filter:{
bool:{
must:[
{
term :{
url:***.com
}
},
{
missing:{
field类别
}
}
]
}
}
}
},
script:ctx._source .category = \10\;
}'
运行该文件后,所有文档都有 url :没有类别的***.com
将获得类别:10
。您可以稍后再次运行相同的查询,以修复在此期间已编入索引的新的 ***.com
文档。
还要确保在 elasticsearch.yml
中启用脚本,然后重新启动ES:
script.inline:on
script.indexed:on
在脚本中,你可以***添加任意多个字段,例如
...
script:ctx ._source.category1 = \10\; ctx._source.category2 = \20 \;
更新
ES 2.3现在将更新为查询功能。您仍然可以按照原样使用上述查询,它将工作(除了过滤
和缺少
已被弃用,但仍然工作;)。
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://***.com/users/4005632/mehmet-yener-yilmaz",
"respsize":"500"
}
I have an external data and I know "***.com" belongs to category 10, And I need to update the doc, and make it like:
{
"url":"http://***.com/users/4005632/mehmet-yener-yilmaz",
"respsize":"500",
"category":"10"
}
Of course I will do this all documents which url fields has "***.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?
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' -d '
{
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"term": {
"url": "***.com"
}
},
{
"missing": {
"field": "category"
}
}
]
}
}
}
},
"script" : "ctx._source.category = \"10\";"
}'
After running this, all your documents with url: ***.com
that don't have a category, will get category: 10
. You can run the same query again later to fix new ***.com
documents that have been indexed in the meantime.
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\";"
UPDATE
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 ;).