Create ElasticSearch dynamic template to ensure all fields are set to not_analyzed

13,197
{
  "mappings": {
    "TypeName": {
      "dynamic_templates": [
        {
          "base": {
            "mapping": {
              "index": "not_analyzed"
            },
            "match": "*",
            "match_mapping_type": "*"
          }
        }
      ],
      "dynamic": true,
      "properties": {
        "url": {
          "index": "analyzed",
          "type": "string"
        },
        "resourceUrl": {
          "index": "analyzed",
          "type": "string"
        }
      }
    }
  }
}

Overall, index-level template:

{
  "mappings": {
    "_default_": {
      "dynamic_templates": [
        {
          "base": {
            "mapping": {
              "index": "not_analyzed"
            },
            "match": "*",
            "match_mapping_type": "*"
          }
        }
      ]
    }
  }
}
Share:
13,197
Stephen
Author by

Stephen

Updated on June 13, 2022

Comments

  • Stephen
    Stephen almost 2 years

    I have an ElasticSearch type for which I want the mapping to be set dynamically. There are a few select fields on that type that I want to be analyzed, but everything else should be set to "not_analyzed".

    I've come up with the following snippet. This sets all string fields to not be analyzed, but doesn't cover all other data types. I tried using the "generic" field shown in the documentation but it didn't help. Can anyone tell me how I can accomplish this?

    {
      "TypeName": {
        "dynamic_templates": [
          {
            "template_name": {
              "match": "*",
              "match_mapping_type": "string",
              "mapping": {
                "index": "no",
                "type": "string"
              }
            }
          }
        ],
        "dynamic": true,
        "properties": {
          "url": {
            "index": "analyzed",
            "type": "string"
          },
          "resourceUrl": {
            "index": "analyzed",
            "type": "string"
          }
        }
      }
    }