0

I am trying to make a simple autocomplete with typeahead.js.

The source where words are indexed is my amazon elasticsearch service cluster endpoint. I set all permissions.

Now i have this code :

$('#remote .typeahead').typeahead({
  highlight: true,
},
{
  name: 'elasticremote',
  source: function(query, syncResults, asyncResults) {
    $.get('https://xxxxxxxxxxxxxxxxx.eu-west-1.es.amazonaws.com/xxxxxxx/_search?q=' + query, function(data) {
      
      asyncResults(data)
    });
  }
})

Problem is : when i am starting typing in the search field, nothing happens, no suggestions or anything. Moreover i get an error in my console log

TypeError: c.slice is not a function

in typeahead.bundle.min.js

What's wrong with my code ?

Thanks for helping.

John doe
  • 3,680
  • 7
  • 31
  • 65
  • This looks very scary. You're exposing your Elasticsearch cluster to the internet without any security, which means anyone can access it or modify it. You should _never_ talk directly to a data store from a public webpage. At best, your server should be doing this to at least give a chance at blocking access (so ES should be firewalled off unless it comes from the server). – pickypg Jul 23 '16 at 23:23
  • @pickypg This is only for testing purposes. I just want to know how i can set up a typeahead search form by using data indexed in my amazon elasticsearch cluster. Don't want to be bothered by writing complex code for giving access to a protected cluster. This is why did it in this way. – John doe Jul 23 '16 at 23:38
  • `asyncResults` is expecting an array of data. You're providing it an object response that comes back from Elasticsearch in the form of `{ "took": 123, "hits": { "total": 123, "hits" : [ ... ] } }`. You most likely want to widdle down the inner `hits` array to an array of strings that you _then_ pass to `asyncResults`. – pickypg Jul 23 '16 at 23:55
  • @pickypg Thanks. I've tried to provide to asyncResults `data.hits` , `data.hits.hits[0]` and also `data.hits.hits[0]._source.body`. none of that worked. Still the same `TypeError: c.slice is not a function`or stating `TypeError: data.hits.hits[0] is undefined` when i'm starting typing. For example if i search the word `Hello` indexed in my cluster, i haven't the drop down list that should show up results when i start to type `H`. I even tried to follow [Completion Suggester doc](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-completion.html) without success . – John doe Jul 24 '16 at 10:01
  • @pickypg I think i solved the problem. However, now i have another problem. When i'm starting typing in the search field i get an Error (in the console log) meaning that CORS Access Control Allow Origin missing. I checked on aws doc but i cannot find how i can set CORS access to my Amazon ES cluster. – John doe Jul 24 '16 at 20:13
  • I don't believe that AWS allows you to change CORS settings, but I'm not too familiar with their setup to be honest. In fairness, it's not something that you should really enable for the reason I noted above, but your quick-test doesn't get helped by that justification. For what it's worth, Elastic (the company that makes Elasticsearch) also has an ESaaS option and it offers every version of Elasticsearch, including the most recent versions, via the "[Elastic Cloud](https://www.elastic.co/cloud)". – pickypg Jul 24 '16 at 21:37
  • What you can do, is either look at using Elastic Cloud _or_ make a simple server-side proxy that passes on your search _or_ setup your own instance(s) of Elasticsearch in AWS using EC2 without going directly through their ES service. – pickypg Jul 24 '16 at 21:38
  • @pickypg The CORS problem was an issue with typeahead.js i just fixed it. So now i have no longer any error in the console log. Yet the suggestion result drop down list still doesn't appear above my search field. I don't understand. Each time i start to type something i have a GET response in my the console log. For example when i type the first letter of the word i'm searching ('G') i get `{"took":13,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":0,"max_score" :null,"hits":[]}}`. How can i make appear all words indexed starting with a g when i type g? – John doe Jul 24 '16 at 21:50

0 Answers0