I use Elasticsearch and Typeahead in a Rails application to do autocomplete. I got this idea from here.
https://shellycloud.com/blog/2013/10/adding-search-and-autocomplete-to-a-rails-app-with-elasticsearch
I have elasticsearch autocomplete configured correctly because it works when I access it directly through a browser. However, when I try to use typeahead to call the displayed data from an autocomplete request, it does not even start in my debugger. Here is my form and javascript where typeahead is called
The form
<script>
$('#autcomplete_search').typeahead({
highlight: true
},
{
name: 'apple_game',
remote: "/search/autocomplete?query=%QUERY"
});
</script>
<h1>Keyword</h1>
<form action="/search/keyword">
<div>
<%= text_field_tag :query, params[:query], class: "form-control", id: "autcomplete_search" %>
<br/>
<br/>
</div>
<div>
<input type="submit">/</input>
</div>
</form>
controller
def autocomplete
es = ESClient.get_client
games = es.suggest index: 'games',
body: {
apple_game: {
text: params[:keyword],
completion: {
field: "title"}
}
}
render json: games
end
Example browser result from controller method
{
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"apple_game": [
{
"text": "ma",
"offset": 0,
"length": 2,
"options": [
{
"text": "Macabre Mysteries: Curse of the Nightingale Collector Edition HD",
"score": 1
},
{
"text": "Mad Cop - Police Car Race and Drift (Ads Free)",
"score": 1
},
{
"text": "Mad Freebording (Snowboarding)",
"score": 1
},
{
"text": "Mad Merx: Nemesis",
"score": 1
},
{
"text": "Mad River Whitewater Kayak Rush",
"score": 1
}
]
}
]
}
EDIT
I also noticed the following error in the console when starting typeahead
Uncaught Error: missing source