Skip to content

Instantly share code, notes, and snippets.

@JasonStoltz
Last active August 30, 2018 12:20
Show Gist options
  • Save JasonStoltz/f03a3c03e9f875f346a8d97c4c0b01c9 to your computer and use it in GitHub Desktop.
Save JasonStoltz/f03a3c03e9f875f346a8d97c4c0b01c9 to your computer and use it in GitHub Desktop.
App Search Autocomplete Examples
<html>
<!--
This example demonstrates Swiftype's App Search using a jQuery based
auto-complete library, https://github.com/devbridge/jQuery-Autocomplete.
-->
<head>
<style>
body {
display: flex;
align-items: center;
justify-content: center;
}
.autocomplete-suggestions {
border: 1px solid #999;
background: #FFF;
overflow: auto;
}
.autocomplete-suggestion {
padding: 2px 5px;
white-space: nowrap;
overflow: hidden;
}
.autocomplete-selected {
background: #F0F0F0;
}
.autocomplete-suggestions strong {
font-weight: normal;
color: #3399FF;
}
.autocomplete-group {
padding: 2px 5px;
}
.autocomplete-group strong {
display: block;
border-bottom: 1px solid #000;
}
</style>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.devbridge-autocomplete/1.4.9/jquery.autocomplete.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/swiftype_app_search.umd.js"></script>
</head>
<body>
<input type="text" id="typeahead" class="typeahead" />
<script>
var client = SwiftypeAppSearch.createClient({
hostIdentifier: "host-n16af4",
apiKey: "search-foueoyusuprj187kv7hyfof9",
engineName: "node-modules"
});
$('#typeahead').autocomplete({
lookup: function (query, done) {
client
.search(query, {
search_fields: {
name: {}
},
result_fields: {
name: {
raw: {},
snippet: {
size: 100,
fallback: true
}
}
}
})
.then(resultList => {
done({
suggestions: resultList.results.map(function (result) {
return {
data: result.getSnippet("name"),
value: result.getRaw("name")
};
})
});
});
},
formatResult: function (suggestion, value) {
return suggestion.data;
}
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment