From f0d003c5b33affcc4c25b06b0f0e77a2b267e6a2 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Tue, 26 Mar 2024 21:25:28 -0300 Subject: search: move from src/data/things to src root --- package.json | 2 +- src/data/things/search.js | 99 ----------------------------------------------- src/search.js | 99 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 100 deletions(-) delete mode 100644 src/data/things/search.js create mode 100644 src/search.js diff --git a/package.json b/package.json index d40d9295..a08be2dd 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "#node-utils": "./src/util/node-utils.js", "#repl": "./src/write/build-modes/repl.js", "#replacer": "./src/util/replacer.js", - "#search": "./src/data/things/search.js", + "#search": "./src/search.js", "#serialize": "./src/data/serialize.js", "#sort": "./src/util/sort.js", "#sugar": "./src/util/sugar.js", diff --git a/src/data/things/search.js b/src/data/things/search.js deleted file mode 100644 index 7dcf5cf9..00000000 --- a/src/data/things/search.js +++ /dev/null @@ -1,99 +0,0 @@ -'use strict'; - -import {mkdir, writeFile} from 'node:fs/promises'; -import * as path from 'node:path'; - -import FlexSearch from 'flexsearch'; - -import {logError, logInfo, logWarn} from '#cli'; -import Thing from '#thing'; - -export async function writeSearchIndex({ - wikiCachePath, - wikiData, -}) { - if (!wikiCachePath) { - throw new Error(`Expected wikiCachePath to write into`); - } - - // 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"], - }), - }; - - for (const album of wikiData.albumData) { - indexes.albums.add({ - reference: Thing.getReference(album), - name: album.name, - groups: album.groups.map(group => group.name), - }); - - for (const track of album.tracks) { - 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), - }); - } - } - - for (const artist of wikiData.artistData) { - if (artist.isAlias) { - continue; - } - - indexes.artists.add({ - reference: Thing.getReference(artist), - names: [artist.name, ...artist.aliasNames], - }); - } - - // Export indexes to json - const searchData = {} - - await Promise.all( - Object.entries(indexes) - .map(([indexName, index]) => { - searchData[indexName] = {}; - return index.export((key, data) => { - searchData[indexName][key] = data - }); - })); - - const outputDirectory = - path.join(wikiCachePath, 'search'); - - const outputFile = - path.join(outputDirectory, 'index.json'); - - await mkdir(outputDirectory, {recursive: true}); - await writeFile(outputFile, JSON.stringify(searchData)); - - logInfo`Search index successfully written.`; -} diff --git a/src/search.js b/src/search.js new file mode 100644 index 00000000..7dcf5cf9 --- /dev/null +++ b/src/search.js @@ -0,0 +1,99 @@ +'use strict'; + +import {mkdir, writeFile} from 'node:fs/promises'; +import * as path from 'node:path'; + +import FlexSearch from 'flexsearch'; + +import {logError, logInfo, logWarn} from '#cli'; +import Thing from '#thing'; + +export async function writeSearchIndex({ + wikiCachePath, + wikiData, +}) { + if (!wikiCachePath) { + throw new Error(`Expected wikiCachePath to write into`); + } + + // 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"], + }), + }; + + for (const album of wikiData.albumData) { + indexes.albums.add({ + reference: Thing.getReference(album), + name: album.name, + groups: album.groups.map(group => group.name), + }); + + for (const track of album.tracks) { + 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), + }); + } + } + + for (const artist of wikiData.artistData) { + if (artist.isAlias) { + continue; + } + + indexes.artists.add({ + reference: Thing.getReference(artist), + names: [artist.name, ...artist.aliasNames], + }); + } + + // Export indexes to json + const searchData = {} + + await Promise.all( + Object.entries(indexes) + .map(([indexName, index]) => { + searchData[indexName] = {}; + return index.export((key, data) => { + searchData[indexName][key] = data + }); + })); + + const outputDirectory = + path.join(wikiCachePath, 'search'); + + const outputFile = + path.join(outputDirectory, 'index.json'); + + await mkdir(outputDirectory, {recursive: true}); + await writeFile(outputFile, JSON.stringify(searchData)); + + logInfo`Search index successfully written.`; +} -- cgit 1.3.0-6-gf8a5