From 3b2f391d291e37e0c39a3fc4c7a406c4b6da12b1 Mon Sep 17 00:00:00 2001 From: Gio Date: Mon, 25 Mar 2024 20:47:21 -0500 Subject: upd8: Search implementation --- src/static/clientSearch.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/static/clientSearch.js (limited to 'src/static') diff --git a/src/static/clientSearch.js b/src/static/clientSearch.js new file mode 100644 index 00000000..4d01cfd9 --- /dev/null +++ b/src/static/clientSearch.js @@ -0,0 +1,42 @@ +/* eslint-env browser */ + +async function initSearch() { + const FlexSearch = window.FlexSearch; + + // 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"], + }) + } + + let searchData = await fetch('/media/search_index.json').then(resp => resp.json()) + + Object.entries(searchData).forEach(key_index_pair => { + const [index_key, index_data] = key_index_pair + Object.entries(index_data).forEach(key_value_pair => { + const [key, value] = key_value_pair + window.indexes[index_key].import(key, value); + }) + }) +} + +function searchAll(query, options) { + options = options || {} + return Object.entries(window.indexes).reduce((a, pair) => { + const [k, v] = pair + a[k] = v.search(query, options) + return a + }, {}) +} + +document.addEventListener('DOMContentLoaded', initSearch); -- cgit 1.3.0-6-gf8a5