How to highlight ngram tokens in a word using elastic search How to highlight ngram tokens in a word using elastic search elasticsearch elasticsearch

How to highlight ngram tokens in a word using elastic search


The correct way to achieve what you want is using ngram as tokenizer and not filter. You can do something like this:

"analysis" : {  "filter" : {    "meeteor_stemmer" : {      "name" : "english",      "type" : "stemmer"    }  },  "tokenizer" : {    "meeteor_ngram_tokenizer" : {      "type" : "nGram",      "min_gram" : "2",      "max_gram" : "15"    }  },  "analyzer" : {    "meeteor_search_term_analyzer" : {      "filter" : [        "lowercase",        "asciifolding"      ],      "tokenizer" : "standard"    },    "meeteor_index_analyzer" : {      "filter" : [        "lowercase",        "asciifolding"      ],      "tokenizer" : "meeteor_ngram_tokenizer"    },    "meeteor_project_id_analyzer" : {      "tokenizer" : "standard"    }  }},

It will generate the highlighting by ngram for you like this:

 "...highlight" : {          "name" : [            "Sad <em>Me</em>eting"          ]        }