« get me outta code hell

clientSearch.js « static « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/static/clientSearch.js
blob: 13a34528a4aaded7de83589d53b093acf53d4615 (plain)
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
/* eslint-env browser */

async function initSearch() {
  const {FlexSearch} = window;

  // Copied directly from server search.js
  window.indexes = {
    albums: new FlexSearch.Document({
      id: "reference",
      index: ["name", "groups"],
    }),

    tracks: new FlexSearch.Document({
      id: "reference",
      index: ["track", "album", "artists", "directory", "additionalNames"],
    }),

    artists: new FlexSearch.Document({
      id: "reference",
      index: ["names"],
    }),
  };

  const searchData =
    await fetch('/media/search_index.json')
      .then(resp => resp.json());

  for (const [indexName, indexData] of Object.entries(searchData)) {
    for (const [key, value] of Object.entries(indexData)) {
      window.indexes[index_key].import(key, value);
    }
  }
}

function searchAll(query, options = {}) {
  const results = {};

  for (const [indexName, index] of Object.entries(window.indexes)) {
    results[indexName] = index.search(query, options);
  }

  return results;
}

document.addEventListener('DOMContentLoaded', initSearch);