[英]Update field in a document based on the condition in Kibana/Elasticsearch
[英]Replace document in Elasticsearch index with field condition
如果我在 Elasticsearch 中索引了一個包含日期時間參數或某種序列號的文檔,當且僅當我的新文檔中的值大於當前索引的文檔?
到目前為止,搜索已經向我展示了如何通過腳本影響特定字段的值,但我不確定是否可以使用腳本或操作作為更新標准,並在滿足時替換整個文檔。
更具體地說,我們有一個文檔對象,其中包含將其放入隊列進行處理的時間戳,並且由於我們可能有多個處理器將事物從隊列中取出,因此我們希望確保我們只索引比隊列更新的文檔我們已經在索引中的一個,丟棄任何舊的更改。
嘗試使用_update_by_query
Api。
例子:
映射
PUT my_index
{
"mappings": {
"properties": {
"user": {
"type": "keyword"
},
"timestamp": {
"type": "keyword"
}
}
}
}
索引文件
POST my_index/_doc/1
{
"user":"user1",
"timestamp":1234
}
POST my_index/_doc/2
{
"user":"user2",
"timestamp":1235
}
按查詢更新
讓我們只更新timestamp
大於1234
文檔。
POST /my_index/_update_by_query
{
"script": {
"source": "ctx._source.user='new user';", ----> updating field user
"lang": "painless"
},
"query": {
"range": {
"timestamp": {
"gt": 1234
}
}
}
}
您可以更新其他字段或插入新字段,只需使用"source": "ctx._source.user='new user';ctx._source.timestamp=456";ctx._source.new_field=value"
結果
{
"_index": "my_index",
"_type": "_doc",
"_id": "2",
"_score": 1,
"_source": {
"user": "new user",
"timestamp": 1235
}
}
希望這可以幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.