« get me outta code hell

search: refactor index generation with standardized reader function - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorGio <sethg@ipi.org>2024-03-26 21:29:06 -0500
committer(quasar) nebula <qznebula@protonmail.com>2024-05-31 12:11:45 -0300
commit1a5cb05b2743907c3924a4b0ed90067c284036ce (patch)
treeceda11e29cc563e26751090d78b87e618aa7d50b /src
parent6a59fa7b994c815394b2f77904d8eb91ea4f320e (diff)
search: refactor index generation with standardized reader function
Diffstat (limited to 'src')
-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) {