elastic-search suggester
Use kibana sense to create your mappings
Note:Dont forget to add suggest: filed with your mapping as shown below
eg:
{
"mappings": {
"User": {
"properties": {
"fName": {
"type": "string",
"index": "not_analyzed"
},
"suggest" : {
"input": [ "Nevermind", "Nirvana" ],//on these input you will get the suggestions
"output": "Nirvana - Nevermind",//this will be your auto complete return result
"payload" : { "artistId" : 2321 },//you can include the payload too with return resule
"weight" : 34 //preference
}
}
}
}
Then to use the suggester
you need to do like shown below
On the head object like in this case "music" is parent object which has "songs"
you need to add suggester and point to suggest field
curl -X POST 'localhost:9200/music/_suggest?pretty' -d '{
"song-suggest" : {
"text" : "n",
"completion" : {
"field" : "suggest"
}
}
}'
To add fuzzy suggester
curl -X POST 'localhost:9200/music/_suggest?pretty' -d '{
"song-suggest" : {
"text" : "n",
"completion" : {
"field" : "suggest",
"fuzzy" : {
"fuzziness" : 2
}
}
}
}'
that's it you will get your suggester working
NOTE: this search might also show you deleted documents .
if you dont want this to happen then you need to index your page every time you add or update the data
As shown below :
$ curl -XPOST 'http://localhost:9200/_optimize?only_expunge_deletes=true'
for more info: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-completion.html