gpt4 book ai didi

elasticsearch - 如果该属性不存在,则将附加属性添加到现有文档中

转载 作者:行者123 更新时间:2023-12-03 01:40:50 26 4
gpt4 key购买 nike

我有一个特殊要求,我必须向具有n个文档的 Elasticsearch 索引添加一个附加属性。仅当文档不包含该属性时,才需要这样做。此任务主要涉及2个步骤

1)搜寻
2)更新

我知道如何使用多个查询来执行此操作。但是,如果我能在一个查询中做到这一点,那就太好了。可能吗?如果是,有人可以告诉我如何做到这一点。

最佳答案

您可以结合使用update by queryexists查询来更新新字段并将其添加到不包含该属性的那些文档中。

例如,您只有一个文档包含attrib2字段,而其他文档则没有该字段。

curl -XPUT "http://localhost:9200/my_test_index/doc/1" -H 'Content-Type: application/json' -d'
{
"attrib1": "value1"
}'
curl -XPUT "http://localhost:9200/my_test_index/doc/2" -H 'Content-Type: application/json' -d'
{
"attrib1": "value21"
}'
curl -XPUT "http://localhost:9200/my_test_index/doc/3" -H 'Content-Type: application/json' -d'
{
"attrib1": "value31",
"attrib2": "value32"
}'

以下 update by query将完成此工作。
curl -XPOST "http://localhost:9200/my_test_index/_update_by_query" -H 'Content-Type: application/json' -d'
{
"script": {
"lang": "painless",
"source": "ctx._source.attrib2 = params.attrib2",
"params": {
"attrib2": "new_value_for_attrib2"
}
},
"query": {
"bool": {
"must_not": [
{
"exists": {
"field": "attrib2"
}
}
]
}
}
}'

它将仅在那些没有该字段的文档上将新值 new_value_for_attrib2设置为 attrib2字段。

关于elasticsearch - 如果该属性不存在,则将附加属性添加到现有文档中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47793865/

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