-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathindexing-worker.js
37 lines (33 loc) · 1.21 KB
/
indexing-worker.js
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
importScripts('../../dist/fuzzy-search.umd.js');
self.fuzzySearch = fuzzySearch;
/**
* Make sure to build the searcher in the exact same way in both the main thread (index.js) and the worker thread
* (worker.js).
*/
function createSearcher() {
return fuzzySearch.SearcherFactory.createDefaultSearcher();
// If your dataset contains non-latin characters, build the searcher in the following way instead:
/* const config = fuzzySearch.Config.createDefaultConfig();
config.normalizerConfig.allowCharacter = (c) => true;
return fuzzySearch.SearcherFactory.createSearcher(config); */
}
onmessage = async (e) => {
const searcher = createSearcher();
const indexingMeta = indexData(searcher, e.data);
const memento = new fuzzySearch.Memento();
searcher.save(memento);
postMessage({
mementoObjects: memento.objects,
indexingMeta: indexingMeta.allEntries
});
};
function indexData(searcher, data) {
console.log(`indexing ${data.persons.length} persons`);
const indexingMeta = searcher.indexEntities(
data.persons,
(person) => person.id,
(person) => [person.firstName, person.lastName, `${person.firstName} ${person.lastName}`]
);
console.log('finished indexing');
return indexingMeta;
}