« get me outta code hell

search: decompose index computation - 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 20:37:05 -0500
committer(quasar) nebula <qznebula@protonmail.com>2024-05-31 12:11:45 -0300
commit6a59fa7b994c815394b2f77904d8eb91ea4f320e (patch)
tree6ae325ce8cc7a0e1aebd5a47b66972b0d4011dd3 /src
parent8e8011d79511d68309d99ea93688d2a64d6149a6 (diff)
search: decompose index computation
Diffstat (limited to 'src')
-rw-r--r--src/search.js45
-rwxr-xr-xsrc/upd8.js4
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,
     });