forked from cockroachdb/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.html
66 lines (60 loc) · 2.09 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
---
title: Search results
toc: false
contribute: false
---
<script>
// http://stackoverflow.com/a/979995/864236
var QueryString = function () {
// This function is anonymous, is executed immediately and
// the return value is assigned to QueryString!
var query_string = {};
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
// If first entry with this name
if (typeof query_string[pair[0]] === "undefined") {
query_string[pair[0]] = decodeURIComponent(pair[1]);
// If second entry with this name
} else if (typeof query_string[pair[0]] === "string") {
var arr = [ query_string[pair[0]],decodeURIComponent(pair[1]) ];
query_string[pair[0]] = arr;
// If third or later entry with this name
} else {
query_string[pair[0]].push(decodeURIComponent(pair[1]));
}
}
return query_string;
}();
document.title = 'Search - ' + QueryString.q;
var client = algoliasearch('BH4D9OD16A', '5a93998460e4910a8769500d325250cb');
var index = client.initIndex('cockroachlabs');
var currentVersion = getCookie('currentVersion') || 'stable';
var searchConfig = {
query: QueryString.q,
hitsPerPage: 10,
facetFilters: ['version:' + currentVersion]
};
index.search(searchConfig, function searchDone(err, data) {
var div = $('<div/>');
if (data.hits.length) {
data.hits.forEach(function(hit) {
if (hit._highlightResult.content.value.length > 450) {
hit._highlightResult.content.value = hit._highlightResult.content.value.slice(0, 450) + '...'
}
var h = $('<div/>', {class: 'search-title'});
h.append($('<a/>', {href: hit.url}).html(hit.hierarchy.lvl0));
var r = $('<div/>', {class: 'search-item'});
r.append(h);
r.append($('<div/>', {class: 'search-link'}).html(hit.url));
r.append($('<div/>', {class: 'search-snippet'}).html(hit._highlightResult.content.value));
div.append(r);
});
} else {
div.append($('<p>No results.</p>'));
}
$('.post-content').html(div);
$('.post-content').append('<img class="search-by-algolia" src="{{ 'images/algolia.svg' | relative_url }}" />');
});
</script>