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/data/things/search.js | 90 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 src/data/things/search.js (limited to 'src/data/things') diff --git a/src/data/things/search.js b/src/data/things/search.js new file mode 100644 index 00000000..df177071 --- /dev/null +++ b/src/data/things/search.js @@ -0,0 +1,90 @@ +#!/usr/bin/env node + +'use strict'; + +import { + writeFile, +} from 'node:fs/promises'; + +import { + logWarn, + logInfo, + logError, +} from '#cli'; + +import Thing from '#thing'; + +import FlexSearch from 'flexsearch'; + +export async function writeSearchIndex(search_index_path, wikiData) { + + // Basic flow is: + // 1. Define schema for type + // 2. Add documents to index + // 3. Save index to exportable json + + // Copy this block directly into clientSearch.js + const 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"], + }) + } + + wikiData.albumData.forEach((album) => { + indexes.albums.add({ + reference: Thing.getReference(album), + name: album.name, + groups: album.groups.map(group => group.name), + }) + + album.tracks.forEach((track) => { + indexes.tracks.add({ + reference: Thing.getReference(track), + album: album.name, + track: track.name, + artists: [ + track.artistContribs.map(contrib => contrib.artist.name), + ...track.artistContribs.map(contrib => contrib.artist.aliasNames) + ], + additionalNames: track.additionalNames.map(entry => entry.name) + }) + }) + }); + + wikiData.artistData + .filter(artist => !artist.isAlias) + .forEach((artist) => { + indexes.artists.add({ + reference: Thing.getReference(artist), + names: [ + artist.name, + ...artist.aliasNames + ] + }) + }) + + // Export indexes to json + let searchData = {} + + await Promise.all( + Object.entries(indexes) + .map(pair => { + const [index_name, index] = pair + searchData[index_name] = {} + return index.export((key, data) => { + searchData[index_name][key] = data + }); + }) + ) + + writeFile(search_index_path, JSON.stringify(searchData)) +} -- cgit 1.3.0-6-gf8a5