How to create a mapping for dynamic keys in elasticsearch

267 views Asked by At

If I put a document like this:

$ curl -XPOST "http://localhost:9200/test/test/" -d '
{
  "books": {
    "id1": {
        "name": "Hello World!"
    },
    "id2": {
        "name": "Hitchhiker Guide To The Galaxy"
    }
  }
}'

Then it creates a mapping like this:

$ curl -XGET "http://localhost:9200/test/test/_mapping"
{
  "test":{
    "mappings":{
      "test":{
        "properties":{
          "books":{
            "properties":{
              "id1":{
                "properties":{
                  "name":{
                    "type":"string"
                  }
                }
              },
              "id2":{
                "properties":{
                  "name":{
                    "type":"string"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

The properties / keys in the "books" object are dynamic and the mapping will grow endlessly. How can I make ES not look at the keys for "books" and make it understand that each value in "books" is of the same type? E.g. the mapping should not contain a new entry for each book id.

1

There are 1 answers

0
NikoNyrh On

You can store only one document at a time, unless you use bulk upload. A single document could look like this:

$ curl -XPOST "http://localhost:9200/test/test/" -d '
{
    "id": "id1",
    "name": "Hello World!"
}'

Or:

$ curl -XPOST "http://localhost:9200/test/test/id1" -d '
{
    "name": "Hello World!"
}'