Mrunal Gosar
Mrunal Gosar

Reputation: 4683

Elasticsearch mapping select all fields via template to change their data type Elasticsearch

Hi All I am using elasticsearch-template.json to set data type of all of my fields to string. Below is the snippet of the template:

{
    "template": "logstash-*",
    "settings": {
        "index.refresh_interval": "5s",
        "number_of_shards": 1,
        "number_of_replicas": 0
    },
    "mappings": {
        "logs": {
            "_all": {
                "enabled": true
            },
            "properties": {
                "level1": {
                    "properties": {
                      "level2": {
                        "properties": {
                            "_all": {"type": "string"}
                        }
                        }
                    }
                }
            }
        }
    }
}

Here under level2 i have got lots of fields which get created i want to set all of them to string how can i set it. I have tried "*" character as well as "%" character to select all the fields. but unfortunately it only gets added as a new field to the mapping. How to specify in template to select all the fields under a certain level?

Upvotes: 0

Views: 444

Answers (1)

Alcanzar
Alcanzar

Reputation: 17165

I believe what you are looking for is a dynamic_templates and using path_match instead of match. This demonstrates how that might work:

curl -DELETE localhost:9200/test-*
curl -XDELETE http://localhost:9200/_template/test
curl -XPOST http://localhost:9200/_template/test -d '
{
    "template": "test-*",
    "mappings": {
        "_default_": {
            "dynamic_templates": [
            {
                "level1_level2_all": {
                    "path_match": "level1.level2.*",
                    "match_mapping_type": "*",
                    "mapping": {
                        "index": "not_analyzed",
                        "type": "string"
                    }
                }
            }
            ]
        }
    }
}
'

curl -XPOST http://localhost:9200/test-1/a -d '
{
    "level1": {
        "level2": {
            "x":1
        }
    }
}'
curl -XPOST http://localhost:9200/test-1/a -d '
{
    "level1": {
        "level2": {
            "y":1
        }
    }
}'

curl http://localhost:9200/test-1/_mapping?pretty

The output of which is:

  "test-1" : {
    "mappings" : {
      "_default_" : {
        "dynamic_templates" : [ {
          "level1_level2_all" : {
            "mapping" : {
              "index" : "not_analyzed",
              "type" : "string"
            },
            "match_mapping_type" : "*",
            "path_match" : "level1.level2.*"
          }
        } ],
        "properties" : { }
      },
      "a" : {
        "dynamic_templates" : [ {
          "level1_level2_all" : {
            "mapping" : {
              "index" : "not_analyzed",
              "type" : "string"
            },
            "match_mapping_type" : "*",
            "path_match" : "level1.level2.*"
          }
        } ],
        "properties" : {
          "level1" : {
            "properties" : {
              "level2" : {
                "properties" : {
                  "x" : {
                    "type" : "string",
                    "index" : "not_analyzed"
                  },
                  "y" : {
                    "type" : "string",
                    "index" : "not_analyzed"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Upvotes: 2

Related Questions