From 1a5cb05b2743907c3924a4b0ed90067c284036ce Mon Sep 17 00:00:00 2001 From: Gio Date: Tue, 26 Mar 2024 21:29:06 -0500 Subject: search: refactor index generation with standardized reader function --- src/search.js | 100 ++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 69 insertions(+), 31 deletions(-) diff --git a/src/search.js b/src/search.js index 4a711b23..b83bb0e0 100644 --- a/src/search.js +++ b/src/search.js @@ -10,43 +10,81 @@ import Thing from '#thing'; import {makeSearchIndexes} from './util/searchSchema.js'; +const DEBUG_DOC_GEN = true; + async function populateSearchIndexes(indexes, wikiData) { - // Albums, tracks - 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), - name: track.name, - album: album.name, - - artists: [ - track.artistContribs.map(contrib => contrib.artist.name), - ...track.artistContribs.map(contrib => contrib.artist.aliasNames) - ], - - additionalNames: - track.additionalNames.map(entry => entry.name), - }); + + const haveLoggedDocOfThing = {}; // debugging only + + function readCollectionIntoIndex( + collection, + index, + mapper + ) { + // Add a doc for mapper(thing) to index for each thing in collection. + for (const thing of collection) { + const reference = Thing.getReference(thing); + try { + const doc = { + reference, + ...mapper(thing) + }; + // Print description of output doc, if debugging enabled. + if (DEBUG_DOC_GEN && !haveLoggedDocOfThing[thing.constructor.name]) { + logInfo(JSON.stringify(doc, null, 2)); + haveLoggedDocOfThing[thing.constructor.name] = true; + } + index.add(doc); + } catch (e) { + // Enrich error context + logError`Failed to write searchable doc for thing ${reference}`; + const thingSchemaSummary = Object.fromEntries( + Object.entries(thing) + .map(([k, v]) => [k, v ? (v.constructor.name || typeof v) : v]) + ); + logError("Availible properties: " + JSON.stringify(thingSchemaSummary, null, 2)); + throw e; + } } } - // Artists - for (const artist of wikiData.artistData) { - if (artist.isAlias) { - continue; - } + // Albums + readCollectionIntoIndex( + wikiData.albumData, + indexes.albums, + album => ({ + name: album.name, + groups: album.groups.map(group => group.name), + }) + ); + + // Tracks + readCollectionIntoIndex( + wikiData.trackData, + indexes.tracks, + track => ({ + name: track.name, + album: track.album.name, + artists: [ + track.artistContribs.map(contrib => contrib.artist.name), + ...track.artistContribs.map(contrib => contrib.artist.aliasNames) + ].flat(), + additionalNames: track.additionalNames.map(entry => entry.name), + }) + ); - indexes.artists.add({ - reference: Thing.getReference(artist), + // Artists + const realArtists = + wikiData.artistData + .filter(artist => !artist.isAlias); + + readCollectionIntoIndex( + realArtists, + indexes.artists, + artist => ({ names: [artist.name, ...artist.aliasNames], - }); - } + }) + ); async function exportIndexesToJson(indexes) { -- cgit 1.3.0-6-gf8a5