index letter by letter with elasticsearch in rails app

Viewed 151

does anyone knows what is the best way to index data letter by letter in elasticsearch . i have rails app witch i've use elasticsearch as search engine in my app. in my rails app i have alot of content that contain articles , in my implementation i can search through articles and return reult perfectly , i've multiple filter like ngram - edge gram - whitespace and ... . if user type exact full word every things fine and with my design i have some filter like ngram ,at this state i want to help user if he/she type one letter he/she could return some result i can handle this ponit with where command but i want to do it with elastics. Im looking through elastices tutorials and best practices but none of them cant help me , at the best state if user type at least three or four letter elastices could return results . I`m useing ngram filter edge gram filter regex and ... but none of them was useful.

1 Answers

you need to use suggest query in elasticsearch as below:

{
 "suggest": {
 "auto-complete-suggest": {
 "prefix": "Your prefix text",
 "completion": {
  "size" : 5,
  "field": "text_completion"
 
      }
    }
  }
}

and your mapping would be something like this:

{
  "mappings": {
    "properties": {
      "text_completion": {
        "type": "completion",
        "analyzer": "Your analyzer"
      }
    }
  }
}
Related