How to create a live search option using elastic search

I am new to elastic search and playing around with in it. I created an index called live_search_index then I add documents contain these title fields.

  • Western province
  • Northern province
  • Colombo district
  • Colombo city
  • Negambo city

If I run this query

{
    "query": {
        "match" : {
            "title" : "colombo"
    }
}
}

If I run this query I will get colombo district and colombo city But this query gives me nothing.

{
    "query": {
        "match" : {
            "title" : "mbo"
    }
}
}

What I want is pass "mbo" and get the result of

Colo mbo district

Colo mbo city

Nega mbo city

How do I achieve this using elastic search?

Have a read of the section on analysis first.

You'll want to configure an analyzer for the "title" field that generates edge ngrams from the back of tokens. Typically, you would

  1. reverse the token
  2. generate the edge ngram
  3. reverse the resulting edge ngram.

For example, given the text "Colombo district", the Standard tokenizer will produce tokens Colombo and district. In the token filters, we would want to

  1. lowercase to support case insensitive search: colombo and district
  2. reverse tokens in preparation for back edge ngram: obmoloc and tcirtsid
  3. edgengram to create ngram. for purposes here, min and max gram set to 3: obmoloc, obm, tci and tcirtsid
  4. reverse tokens again: colombo, mbo, ict and district

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.