forked from cockroachdb/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.html
74 lines (66 loc) · 2.51 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
---
title: Search results
toc: false
contribute: false
build_for: [cockroachdb, cockroachcloud]
---
<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]));
}
}
query_string.q = query_string.q.replace(/\+/g, ' ');
return query_string;
}();
document.title = 'Search - ' + QueryString.q;
$('#search-input').val(QueryString.q.replace('+', ' '));
var client = algoliasearch('BH4D9OD16A', '5a93998460e4910a8769500d325250cb');
var index = client.initIndex('cockroachlabs');
// search against numeric version instead of version tag to ensure accurate results
var siteVersions = {{ site.versions | jsonify }};
var currentVersionCookie = getCookie('currentVersion') || 'stable';
var currentVersionNumeric = currentVersionCookie in siteVersions ? siteVersions[currentVersionCookie] : currentVersionCookie;
var searchConfig = {
query: QueryString.q,
hitsPerPage: 10,
facetFilters: ['version:' + currentVersionNumeric]
};
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>