Skip to content

Instantly share code, notes, and snippets.

@phillro
Created September 4, 2011 17:59
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • 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>
@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