Search part of word in elasticsearch using nGram – auto-complete search

If we have documents of city information, in elasticsearch we can implement auto-complete search cartier nail bracelet
using nGram filter.

Add index fake cartier bracelets
mapping as following bracelets

curl -X PUT "http://localhost:9200/cities" -d '{
"mappings" : {
"city" : {
"properties" : {
"name" : {
"type" : "string",
"search_analyzer" : "apps_search",
"index_analyzer" : "apps_index"
},
"state": {"type" : "string"},
"pin": {"type" : "string"},
"location": {"type": "geo_point"}
}
}
},
"settings" : {
"analysis" : {
"analyzer" : {
"apps_search" : {
"tokenizer" : "keyword",
"filter" : ["lowercase"]
},

"apps_index" : {
"tokenizer" : "keyword",
"filter" : ["lowercase", "substring"]
}
},

"filter" : {
"substring" : {
"type" : "nGram",
"min_gram" : 1,
"max_gram" : 20
}
}
}
}
}';

You can search like following …

{
"size" : 100,
"query" : {
"match" : {
"name" : "a"
}
}
}

You will get result of documents with started “A”

{
"size" : 100,
"query" : {
"match" : {
"name" : "pun"
}
}
}

You will get result of documents with started as “pun”

 

Categories: Elasticsearch