Skip to content

Instantly share code, notes, and snippets.

@phillro
Created September 4, 2011 17:59
Show Gist options
  • Save phillro/1193227 to your computer and use it in GitHub Desktop.
Save phillro/1193227 to your computer and use it in GitHub Desktop.
Elasticsearch autocomplete example with cross origin resource sharing
<html>
<head>
<link rel="stylesheet" href="/examples/stylesheets/ui-lightness/jquery-ui-1.8.16.custom.css" type="css">
<script type="text/javascript" src="/examples/javascripts/jquery.min.js"></script>
<script type="text/javascript" src="/examples/javascripts/jquery.base64.min.js"></script>
<script type="text/javascript" src="/examples/javascripts/jquery-ui-1.8.16.custom.min.js"></script>
<script type="text/javascript" src="/examples/javascripts/jquery.tmpl.min.js"></script>
</head>
<body>
<script>
$(function() {
$("#keyword").autocomplete({
source: function(request, response) {
var wildcard = { "name": "*" + request.term.toLowerCase() + "*" };
var postData = {
"query": { "wildcard": wildcard },
"fields": ["name", "_id"]
};
$.ajax({
url: "https://esh01.elasticsearchhq.com:9200/skyfetch2/location/_search",
type: "POST",
dataType: "JSON",
data: JSON.stringify(postData),
success: function(data) {
response($.map(data.hits.hits, function(item) {
return {
label: item.fields.name,
id: item.fields._id
}
}));
},
});
},
minLength: 2
})
});
</script>
<div class="demo">
<h1>Autocomplete using cross origin resource sharing with elasticsearch</h1>
<br>
<div class="ui-widget">
<label for="keyword">Keyword: </label>
<input id="keyword">
</div>
</body>
</html>
@cassianotartari
Copy link

Sorry, I'm starting with Elastica and I couldn't get it working in my code, I've made some research about it without success. Can you explain what are the parameters? I my configuration I just know how to make a search like this: http://localhost:9200/my_index/my_type/_search?q=[search_string] . What it the wildcard, name, fields,.... ? Thank you!

@sriharshakappala
Copy link

Why is it my URL getting changed while making an ajax call?

I have specified url as http://localhost:9200/company/external/_search but it is making an ajax request to http://localhost:3000/<my_current_controller>/<my_current_action>

@kamalbctg
Copy link

better to use Edge NGram Tokenizer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment