From 6a59fa7b994c815394b2f77904d8eb91ea4f320e Mon Sep 17 00:00:00 2001 From: Gio Date: Tue, 26 Mar 2024 20:37:05 -0500 Subject: search: decompose index computation --- src/search.js | 45 ++++++++++++++++++++++++++++----------------- src/upd8.js | 4 ++-- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/src/search.js b/src/search.js index 9b5d2d7a..4a711b23 100644 --- a/src/search.js +++ b/src/search.js @@ -10,21 +10,8 @@ import Thing from '#thing'; import {makeSearchIndexes} from './util/searchSchema.js'; -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 - - const indexes = makeSearchIndexes(FlexSearch); - +async function populateSearchIndexes(indexes, wikiData) { + // Albums, tracks for (const album of wikiData.albumData) { indexes.albums.add({ reference: Thing.getReference(album), @@ -35,8 +22,8 @@ export async function writeSearchIndex({ for (const track of album.tracks) { indexes.tracks.add({ reference: Thing.getReference(track), - album: album.name, name: track.name, + album: album.name, artists: [ track.artistContribs.map(contrib => contrib.artist.name), @@ -49,6 +36,7 @@ export async function writeSearchIndex({ } } + // Artists for (const artist of wikiData.artistData) { if (artist.isAlias) { continue; @@ -60,7 +48,8 @@ export async function writeSearchIndex({ }); } - // Export indexes to json + +async function exportIndexesToJson(indexes) { const searchData = {}; // Map each index to an export promise, and await all. @@ -73,6 +62,28 @@ export async function writeSearchIndex({ }); })); + return searchData; +} + +export async function writeSearchJson({ + 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 + + const indexes = makeSearchIndexes(FlexSearch); + + await populateSearchIndexes(indexes, wikiData); + + const searchData = await exportIndexesToJson(indexes); + const outputDirectory = path.join(wikiCachePath, 'search'); diff --git a/src/upd8.js b/src/upd8.js index 3dc01da8..21cabfae 100755 --- a/src/upd8.js +++ b/src/upd8.js @@ -47,7 +47,7 @@ import {bindFind, getAllFindSpecs} from '#find'; import {processLanguageFile, watchLanguageFile, internalDefaultStringsFile} from '#language'; import {isMain, traverse} from '#node-utils'; -import {writeSearchIndex} from '#search' +import {writeSearchJson} from '#search'; import {sortByName} from '#sort'; import {generateURLs, urlSpec} from '#urls'; import {identifyAllWebRoutes} from '#web-routes'; @@ -1489,7 +1489,7 @@ async function main() { timeStart: Date.now(), }); - await writeSearchIndex({ + await writeSearchJson({ wikiCachePath, wikiData, }); -- cgit 1.3.0-6-gf8a5