I am trying to add a search function for users in my app. When I type the word "Josh" in the search bar people with the name "Joshua" do not show up. I have type the full name "Joshua". How can I add this look-ahead functionality?
Here is my current query:
"bool": {
"must": [
{
"multi_match": {
"fields": [
"first",
"first.edge",
"last",
"last.edge",
"title",
"title.edge"
],
"query": search,
"fuzziness": 1,
}
}
]
}
One way to solve it is using ngram.
PUT ngram_custom_example
{
"settings": {
"analysis": {
"analyzer": {
"ngram_analyzer": {
"tokenizer": "standard",
"filter": [ "3_5_grams" ]
}
},
"filter": {
"3_5_grams": {
"type": "edge_ngram",
"min_gram": 2,
"max_gram": 5
}
}
}
},
"mappings": {
"properties": {
"name":{
"type": "text",
"analyzer": "ngram_analyzer"
}
}
}
}
POST ngram_custom_example/_bulk
{"index":{}}
{"name":"josh"}
{"index":{}}
{"name":"joshua"}
GET ngram_custom_example/_search
{
"query": {
"bool": {
"must": [
{
"multi_match": {
"fields": [
"name"
],
"query": "josh"
}
}
]
}
}
}