🏡 Home 📖 Chapter Home 👈 Prev 👉 Next

⚡  ElasticsearchBook is crafted by Jozef Sorocin (🟢 Book a consulting hour) and powered by:

Use Case

My document contains

How can I auto-generate mappings for these 3 field varieties?

Approach

You already know you should be utilizing match_mapping_type along with match and/or unmatch so you may be tempted to use

That's not going to work because unmatch can only be a pattern string, not an array thereof.

Use match instead and order the templates from the most specific restriction to the least:

PUT myindex
{
  "mappings": {
	  "dynamic_templates": [
	    {
	      "timeSuffix": {
	        "match_mapping_type": "*",
	        "match_pattern": "regex",
	        "match": "^(.*Time)|(.*At)$",
	        "mapping": {
	          "type": "date",
            "format": "yyyy-MM-dd HH:mm:ss"
	        }
	      }
	    },
	    {
	      "isPrefix": {
	        "match_mapping_type": "*",
	        "match": "is_*",
	        "mapping": {
	          "type": "boolean"
	        }
	      }
	    },
	    {
	      "justKeywords": {
	        "match_mapping_type": "*",
	        "mapping": {
	          "type": "keyword"
	        }
	      }
	    }
	  ]
	}
}

Remarks

<aside> 💡 timeSuffix is defined by an actual regex so you'll need to specify "match_pattern": "regex" in order to support full Java regular expression syntax.

In contrast, isPrefix is a simple wildcard pattern that doesn't require any more config.

</aside>