2014-01-24 40 views
4

在ElasticSearch中,如果给定以下文档,是否可以在不传递父属性(即消息和标记)的情况下将项添加到“Lists”子文档? 我有几个属性在父文件,我不想通过每次我想添加一个项目到子文件。如何将子文档添加到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" 
     } 
    ] 
} 

}

+0

如何你建立文件? –

+0

我正在将整个文档构建为json文档并使用Powershell进行发布。 – Yasir

回答

5

你所寻找的是,如何插入一个嵌套的文件。

对于您的情况,您可以使用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,该follwoing映射应该工作:

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" 
     } 
    } 
}' 
相关问题