-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.html
74 lines (69 loc) · 2.67 KB
/
search.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
---
layout: default
title: Search
---
<script src="https://cdn.jsdelivr.net/jquery/1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/algoliasearch/3/algoliasearch.jquery.min.js"></script>
<section class="panel">
<header class="panel-heading">
<div class="search_box">
<form action="#" method="get">
<input autocomplete="off" class="autocomplete" id="q" placeholder="Start typing" type="text" spellcheck="false" />
<div class="searchbutton">
<i class="icon-search icon-large"></i>
</div>
</form>
<div>Powered by <a href="https://www.algolia.com/">Algolia</a></div>
</div>
</header>
</section>
<h1>Results</h1>
<div id="hits"></div>
<script type="text/javascript">
function searchCallback(content) {
if (content.query !== $('#q').val()) {
// do not take out-dated answers into account
return;
}
if (content.hits.length === 0) {
// no results
$('#hits').empty();
return;
}
// Scan all hits and display them
var html = '';
for (var i = 0; i < content.hits.length; ++i) {
var hit = content.hits[i];
html += '<div class="hit">';
for (var attribute in hit._highlightResult) {
html += '<div class="attribute">' +
'<span>' + attribute + ': </span>' +
hit._highlightResult[attribute].value +
'</div>';
}
html += '</div>';
}
$('#hits').html(html);
}
$(document).ready(function() {
var $inputfield = $('#q');
// Replace the following values by your ApplicationID and ApiKey.
var client = $.algolia.Client('04FUY5Q1FI', '08e7b63928c6311452cebe616e31c40c');
// Replace the following value by the name of the index you want to query.
var index = client.initIndex('jekyll');
$inputfield.keyup(function() {
index.search($inputfield.val(), { hitsPerPage: 5 })
.done(searchCallback)
.fail(function(content) { console.log('Error', content); });
}).focus().closest('form').on('submit', function() {
// on form submit, store the query string in the anchor
location.replace('#q=' + encodeURIComponent($inputfield.val()));
return false;
});
// check if there is a query in the anchor: http://example.org/#q=my+query
if (location.hash && location.hash.indexOf('#q=') === 0) {
var q = decodeURIComponent(location.hash.substring(3));
$inputfield.val(q).trigger('keyup');
}
});
</script>