How to add a subset to the ElasticSearch index

In ElasticSearch, given the following document, can I add elements to the Lists subdocument without passing parent attributes (i.e. message and tags)? I have several attributes in the parent document that I don’t want to pass every time I want to add one element to the subdocument.

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

}

+4
source share
1 answer

What you are looking for is how to insert attached documents.

In your case, you can use the update API to add a subdocument to the list.

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" }
    }
}'

To support attached documents, you must define your mapping, which is described 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"
      }
   }
}'
+5

Source: https://habr.com/ru/post/1523584/


All Articles