Specifying Field Analyzers using Index Templates in Elasticsearch

I’ve been playing around with ElasticSearch recently, and I must say I’m quite impressed with it. However, I’ve had my fare share of poring over the internet to deal with specific challenges I’ve faced in making my application content easily searchable, and one was with preventing certain fields from being analyzed.

These fields are typically ids that should be stored as they are and not broken down. Some of these ids are UUIDs, and other applications specific ones with hyphen (“-“) separations. To enable it to match both possibilities of the whole id being supplied or some portions being supplied (which will required a wildcard search), these kinds of fields should not be analyzed but handled as is.

Since all my ids typically have 1 or more variations of the word “id” somewhere in the mix, the simple solution was to provide an index template that says not to analyze any such fields. The saviour was Elasticsearch’s index templates, and here is mine.

{
    "carewex_template": {
        "template": "*",
        "settings": {
            "index.number_of_shards": 2
        },
        "mappings": {
            "_default_": {
                "_all": {"enabled": false},
                "_source": {"compress": true},
                "dynamic_templates": [
                    {
                        "primarykey_template": {
                            "match": "*id",
                            "include_in_all": true,
                            "mapping": {"type": "string", "index": "not_analyzed"},
                            "match_mapping_type": "string"
                        }
                    },
                    {
                        "otherIdkey_template": {
                            "match": "*Id",
                            "include_in_all": true,
                            "mapping": {"type": "string", "index": "not_analyzed"},
                            "match_mapping_type": "string"
                        }
                    },
                    {
                        "thirdIdkey_template": {
                            "match": "*ID",
                            "include_in_all": true,
                            "mapping": {"type": "string", "index": "not_analyzed"},
                            "match_mapping_type": "string"
                        }
                    }
                ]
            }
        }
    }
}

Placing this file as my_template.json in the config/templates directory was all I needed. Whenever I indexed a document, all matching fields were prevented from being analyzed, and my problem was solved.

So, have fun with Elasticsearch.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s