« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/search.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/search.js')
-rw-r--r--src/search.js100
1 files 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) {