Let’s say I have the following mapping:

{
    "mappings": {
        "_doc": {
            "properties": {
                "id":       {"type": "keyword"},
                "params":   {
                    "type": "object",
                },
            }
        }
    }
}

params has a lot of fields with numeric and textual values. I don’t want to specify all the fields of it but I would like to specify that all the text fields should be of keyword type instead of text which is the default and is analysed (which I want to avoid).

How can I do this? I’m using ElasticSearch 6.7

2

This is called Dynamic Templates (See https://www.elastic.co/guide/en/elasticsearch/reference/6.7/dynamic-templates.html)

For your question you can define the mapping as follows:

PUT my_index
{
  "mappings": {
    "_doc": {
      "dynamic_templates": [
        {
          "strings": {
            "match_mapping_type": "string",
            "mapping": {
              "type": "keyword"
            }
          }
        }
      ]
    }
  }
}

This transforms all the detected string fields to keyword. If you want to apply only to a subset of fields, you can use match, unmatch, path_match or path_unmatch clauses. (See the docs for differences between them).

1

Adding to what @banuj mentioned:

It seems that it is possible to combine match_mapping_type and path_match to achieve what I wanted.

PUT my_index
{
  "mappings": {
    "_doc": {
      "dynamic_templates": [
        {
          "keyword params": {
            "mapping": {
              "type": "keyword"
            },
            "match_mapping_type": "string",
            "path_match": "params.*"
          }
        }
      ]
    }
  }
}

0