0

Currently I am using dynamic template as follows, Here I am applying n-gram analyzer to all the "String" fields. However to improve efficiency I would like to apply n-gram only on specific fields only and not on all String fields.

{
        "template": "*",
        "settings": {
            "analysis": {
                "filter": {
                    "ngram_filter": {
                        "type": "ngram",
                        "min_gram": 1,
                        "max_gram": 25
                    }
                },
                "analyzer": {
                    "case_insensitive": {
                        "tokenizer": "whitespace",
                        "filter": [
                            "ngram_filter",
                            "lowercase"
                        ]
                    },
                    "search_analyzer": {
                        "type": "custom",
                        "tokenizer": "whitespace",
                        "filter": "lowercase"
                    }
                }
            }
        },
        "mappings": {
            "my_type": {
                "dynamic_templates": [
                    {
                        "strings": {
                            "match_mapping_type": "string",
                            "mapping": {
                                "type": "string",
                                "fields": {
                                    "keyword": {
                                        "type": "keyword",
                                        "ignore_above": 256
                                    }
                                },
                                "analyzer": "case_insensitive",
                                "search_analyzer": "search_analyzer"
                            }
                        }
                    }
                ]
            }
        }
    }

I have a payload like this:

{
   "userId":"abc123-pqr180-xyz124-njd212",
   "email" : "[email protected]",
   "name"  : "somename",
   .
   .
   20 more fields
}

Now I want to apply n-gram only for "email" and "userid". How can we do this ?

5
  • Is it conceivable for you to rename the email and name fields? Commented Jun 22, 2017 at 3:37
  • Thanks for reply Val ! , No, I can't rename those fields. In worst case scenario I am ok to specify mapping for each field i.e no dynamic mapping Commented Jun 22, 2017 at 3:46
  • The thing is you can use another parameter called match in which you can give a name pattern to which the template should be applied, but it doesn't take an array and email and name have no prefix/suffix in common. Commented Jun 22, 2017 at 3:47
  • ok .., can we statically define mapping for each field ? Commented Jun 22, 2017 at 4:13
  • of course, simply drop your dynamic_templates and use the proper mapping for each of your fields. Another way to do this is to duplicate the dynamic template for the two fields email and name Commented Jun 22, 2017 at 4:13

1 Answer 1

1

Since you cannot rename the fields I suggest the following solution, i.e. to duplicate the dynamic template for the name and email fields.

{
        "template": "*",
        "settings": {
            "analysis": {
                "filter": {
                    "ngram_filter": {
                        "type": "ngram",
                        "min_gram": 1,
                        "max_gram": 25
                    }
                },
                "analyzer": {
                    "case_insensitive": {
                        "tokenizer": "whitespace",
                        "filter": [
                            "ngram_filter",
                            "lowercase"
                        ]
                    },
                    "search_analyzer": {
                        "type": "custom",
                        "tokenizer": "whitespace",
                        "filter": "lowercase"
                    }
                }
            }
        },
        "mappings": {
            "my_type": {
                "dynamic_templates": [
                    {
                        "names": {
                            "match_mapping_type": "string",
                            "match": "name",
                            "mapping": {
                                "type": "string",
                                "fields": {
                                    "keyword": {
                                        "type": "keyword",
                                        "ignore_above": 256
                                    }
                                },
                                "analyzer": "case_insensitive",
                                "search_analyzer": "search_analyzer"
                            }
                        }
                    },
                    {
                        "emails": {
                            "match_mapping_type": "string",
                            "match": "email",
                            "mapping": {
                                "type": "string",
                                "fields": {
                                    "keyword": {
                                        "type": "keyword",
                                        "ignore_above": 256
                                    }
                                },
                                "analyzer": "case_insensitive",
                                "search_analyzer": "search_analyzer"
                            }
                        }
                    }
                ]
            }
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Val ! That helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.