gpt4 book ai didi

elasticsearch - Elasticsearch从动态生成的索引中的数组对象中删除字段

转载 作者:行者123 更新时间:2023-12-03 00:37:32 25 4
gpt4 key购买 nike

我正在尝试从Elasticsearch中的数组对象中删除字段。索引已动态生成。

这是映射:

{
"mapping": {
"_doc": {
"properties": {
"age": {
"type": "long"
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"result": {
"properties": {
"resultid": {
"type": "long"
},
"resultname": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
},
"timestamp": {
"type": "date"
}
}
}
}
}
}

这是一个文档:
{
"result": [
{
"resultid": 69,
"resultname": "SFO"
},
{
"resultid": 151,
"resultname": "NYC"
}
],
"age": 54,
"name": "Jorge",
"timestamp": "2020-04-02T16:07:47.292000"
}

我的目标是删除索引的所有文档中的所有字段resultid。更新后,文档应如下所示:
{
"result": [
{
"resultname": "SFO"
},
{
"resultname": "NYC"
}
],
"age": 54,
"name": "Jorge",
"timestamp": "2020-04-02T16:07:47.292000"
}

我尝试在stackoverflow上使用以下文章,但没有运气:
Remove elements/objects From Array in ElasticSearch Followed by Matching Query
remove objects from array that satisfying the condition in elastic search with javascript api
Delete nested array in elasticsearch
Removing objects from nested fields in ElasticSearch

希望有人可以帮助我找到解决方案。

最佳答案

您应该使用_reindex API将索引重新索引到一个新索引中,并调用脚本来删除字段:

POST _reindex
{
"source": {
"index": "my-index"
},
"dest": {
"index": "my-index-reindex"
},
"script": {
"source": """
for (int i=0;i<ctx._source.result.length;i++) {
ctx._source.result[i].remove("resultid")
}
"""

}
}

删除第一个索引后:
DELETE my-index

并重新索引它:
POST _reindex
{
"source": {
"index": "my-index-reindex"
},
"dest": {
"index": "my-index"
}
}

关于elasticsearch - Elasticsearch从动态生成的索引中的数组对象中删除字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61009505/

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