gpt4 book ai didi

elasticsearch - 如何将子文档添加到 ElasticSearch 索引

转载 作者:行者123 更新时间:2023-11-29 02:51:48 26 4
gpt4 key购买 nike

在 ElasticSearch 中,给定以下文档,是否可以在不传递父属性(即消息和标签)的情况下将项目添加到“列表”子文档?我不想在每次要向子文档添加一项时都传递父文档中的几个属性。

{
"tweet" : {
"message" : "some arrays in this tweet...",
"tags" : ["elasticsearch", "wow"],
"lists" : [
{
"name" : "prog_list",
"description" : "programming list"
},
{
"name" : "cool_list",
"description" : "cool stuff list"
}
]
}

最佳答案

您正在寻找的是,如何插入嵌套文档。

在您的情况下,您可以使用 Update API将嵌套文档附加到您的列表。

curl -XPOST localhost:9200/index/tweets/1/_update -d '{
"script" : "ctx._source.tweet.lists += new_list",
"params" : {
"new_list" : {"name": "fun_list", "description": "funny list" }
}
}'

要支持嵌套文档,您必须定义映射,描述为here .

假设你的类型是tweets,下面的映射应该有效:

curl -XDELETE http://localhost:9200/index

curl -XPUT http://localhost:9200/index -d'
{
"settings": {
"index.number_of_shards": 1,
"index.number_of_replicas": 0
},
"mappings": {
"tweets": {
"properties": {
"tweet": {
"properties": {
"lists": {
"type": "nested",
"properties": {
"name": {
"type": "string"
},
"description": {
"type": "string"
}
}
}
}
}
}
}
}
}'

然后添加第一个条目:

curl -XPOST http://localhost:9200/index/tweets/1 -d '
{
"tweet": {
"message": "some arrays in this tweet...",
"tags": [
"elasticsearch",
"wow"
],
"lists": [
{
"name": "prog_list",
"description": "programming list"
},
{
"name": "cool_list",
"description": "cool stuff list"
}
]
}
}'

然后添加你的元素:

curl -XPOST http://localhost:9200/index/tweets/1/_update -d '
{
"script": "ctx._source.tweet.lists += new_list",
"params": {
"new_list": {
"name": "fun_list",
"description": "funny list"
}
}
}'

关于elasticsearch - 如何将子文档添加到 ElasticSearch 索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21339557/

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