gpt4 book ai didi

ElasticSearch:部分更新或删除文档。 (与upsert相反)

转载 作者:行者123 更新时间:2023-11-29 02:56:07 24 4
gpt4 key购买 nike

在 ElasticSearch 中,我使用 upsert 来更新可能不存在的文档:

POST /website/pageviews/1/_update
{
"script" : "ctx._source.online+=1",
"upsert": {
"online": 1
}
}

由于我的数据会经常更改,如果online == 0,我想删除我的文档。

如果我每次都需要获取文档并检查online值,那么使用update是没有用的,我不想积累很多垃圾文档。

online == 0 时删除我的文档的最佳方法是什么?像这样的东西:

POST /website/pageviews/1/_update
{
"script" : "ctx._source.online-=1",
"remove_doc": "ctx._source.online == 0"
}

最佳答案

您可以使用 delete operation像这样:

POST /website/pageviews/1/_update
{
"script" : "if (online == 0) { ctx.op = 'delete' } else { ctx._source.online += 1 }",
"upsert": {
"online": 1
}
}

关于ElasticSearch:部分更新或删除文档。 (与upsert相反),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38100875/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com