Skip to content

Commit c182e9f

Browse files
committed
make search reliable
1 parent 75a7d4a commit c182e9f

File tree

2 files changed

+38
-56
lines changed

2 files changed

+38
-56
lines changed

assets/js/offline-search.js

Lines changed: 13 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,22 @@
3030
worker = new Worker('/js/worker.js');
3131
const url = '/json/lunr-index.json';
3232

33-
worker.postMessage({ type: 'init', url: url });
33+
worker.postMessage({
34+
type: 'init',
35+
lunrIndexUrl: url,
36+
rawIndexUrl: $searchInput.data('offline-search-index-json-src')
37+
});
3438

3539
worker.onerror = function (error) {
3640
console.error('Error in worker:', error);
3741
};
3842
}
39-
40-
$.ajax($searchInput.data('offline-search-index-json-src')).then(
41-
(data) => {
42-
data.forEach((doc) => {
43-
resultDetails.set(doc.ref, {
44-
version: doc.version,
45-
title: doc.title,
46-
excerpt: doc.excerpt,
47-
});
48-
});
49-
}
50-
);
5143

5244
let currentTarget = null;
5345

5446
worker.onmessage = function (event) {
5547
if (event.data.type === 'search') {
56-
const results = event.data.results
57-
console.log('Search results:', results);
48+
const docs = event.data.docs;
5849
const $html = $('<div>');
5950

6051
$html.append(
@@ -87,17 +78,19 @@
8778
});
8879
$html.append($searchResultBody);
8980

90-
if (results.length === 0) {
81+
if (docs.size === 0) {
9182
currentTarget.append(
9283
$('<p>').text(`No results found for query "${searchQuery}"`)
9384
);
9485
} else {
95-
results.forEach((r) => {
96-
const doc = resultDetails.get(r.ref);
86+
docs.forEach((doc, key) => {
87+
if (doc === undefined) {
88+
return;
89+
}
9790

9891
const href =
9992
$searchInput.data('offline-search-base-href') +
100-
r.ref.replace(/^\//, '');
93+
key.replace(/^\//, '');
10194

10295
const $entry = $('<div>').addClass('mt-4').addClass('search-result');
10396

@@ -112,7 +105,7 @@
112105
);
113106

114107
$entry.append(
115-
$('<small>').addClass('d-block text-muted').text(r.ref)
108+
$('<small>').addClass('d-block text-muted').text(key)
116109
);
117110

118111
$entry.append($('<p>').text(doc.excerpt));
@@ -150,36 +143,6 @@
150143
}
151144

152145
worker.postMessage({ type: 'search', query: searchQuery, maxResults: $targetSearchInput.data('offline-search-max-results') });
153-
154-
// const results = idx
155-
// .query((q) => {
156-
// const tokens = lunr.tokenizer(searchQuery.toLowerCase());
157-
// tokens.forEach((token) => {
158-
// const queryString = token.toString();
159-
// q.term(queryString, {
160-
// boost: 100,
161-
// });
162-
// q.term(queryString, {
163-
// wildcard:
164-
// lunr.Query.wildcard.LEADING |
165-
// lunr.Query.wildcard.TRAILING,
166-
// boost: 10,
167-
// });
168-
// q.term(queryString, {
169-
// editDistance: 2,
170-
// });
171-
// });
172-
// })
173-
// .slice(
174-
// 0,
175-
// $targetSearchInput.data('offline-search-max-results')
176-
// );
177-
178-
//
179-
// Make result html
180-
//
181-
182-
183146
};
184147

185148
//

assets/js/worker.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
importScripts('https://unpkg.com/[email protected]/lunr.min.js');
22

33
let idx;
4+
const resultDetails = new Map(); // Will hold the data for the search results (titles and summaries)
45
let indexReadyPromise;
56

67
// Initialize the index
7-
self.onmessage = async function(event) {
8+
self.onmessage = async function (event) {
89
if (event.data.type === 'init') {
910
indexReadyPromise = new Promise(async (resolve, reject) => {
1011
try {
11-
console.log(event.data);
12-
const response = await fetch(event.data.url);
13-
const data = await response.json();
14-
idx = lunr.Index.load(data);
12+
const rawIndex = await fetch(event.data.rawIndexUrl);
13+
let json = await rawIndex.json();
14+
json.forEach((doc) => {
15+
resultDetails.set(doc.ref, {
16+
version: doc.version,
17+
title: doc.title,
18+
excerpt: doc.excerpt,
19+
});
20+
});
21+
22+
const lunrIndex = await fetch(event.data.lunrIndexUrl);
23+
json = await lunrIndex.json();
24+
idx = lunr.Index.load(json);
1525
resolve();
1626
self.postMessage({ type: 'init', status: 'success' });
1727
} catch (error) {
@@ -41,7 +51,16 @@ self.onmessage = async function(event) {
4151
})
4252
.slice(0, event.data.maxResults);
4353

44-
self.postMessage({ type: 'search', results: results });
54+
const docs = new Map();
55+
results.forEach((result) => {
56+
if (resultDetails.get(result.ref) === undefined) {
57+
return;
58+
}
59+
60+
docs.set(result.ref, resultDetails.get(result.ref));
61+
});
62+
63+
self.postMessage({ type: 'search', status: 'success', docs: docs });
4564
} catch (error) {
4665
self.postMessage({ type: 'search', status: 'error', message: 'Index not ready' });
4766
}

0 commit comments

Comments
 (0)