« get me outta code hell

search.js « things « data « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data/things/search.js
blob: df17707141efbea2b2560b61c77149fc402c1400 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env node

'use strict';

import {
  writeFile,
} from 'node:fs/promises';

import {
  logWarn,
  logInfo,
  logError,
} from '#cli';

import Thing from '#thing';

import FlexSearch from 'flexsearch';

export async function writeSearchIndex(search_index_path, wikiData) {

  // Basic flow is:
  // 1. Define schema for type
  // 2. Add documents to index
  // 3. Save index to exportable json

  // Copy this block directly into clientSearch.js
  const indexes = {
    albums: new FlexSearch.Document({
      id: "reference",
      index: ["name", "groups"],
    }),
    tracks: new FlexSearch.Document({
      id: "reference",
      index: ["track", "album", "artists", "directory", "additionalNames"],
    }),
    artists: new FlexSearch.Document({
      id: "reference",
      index: ["names"],
    })
  }

  wikiData.albumData.forEach((album) => {
    indexes.albums.add({
      reference: Thing.getReference(album),
      name: album.name,
      groups: album.groups.map(group => group.name),
    })

    album.tracks.forEach((track) => {
      indexes.tracks.add({
        reference: Thing.getReference(track),
        album: album.name,
        track: track.name,
        artists: [
          track.artistContribs.map(contrib => contrib.artist.name),
          ...track.artistContribs.map(contrib => contrib.artist.aliasNames)
        ],
        additionalNames: track.additionalNames.map(entry => entry.name)
      })
    })
  });

  wikiData.artistData
  .filter(artist => !artist.isAlias)
  .forEach((artist) => {
    indexes.artists.add({
      reference: Thing.getReference(artist),
      names: [
        artist.name,
        ...artist.aliasNames
      ]
    })
  })

  // Export indexes to json
  let searchData = {}

  await Promise.all(
    Object.entries(indexes)
    .map(pair => {
      const [index_name, index] = pair
      searchData[index_name] = {}
      return index.export((key, data) => {
        searchData[index_name][key] = data
      });
    })
  )

  writeFile(search_index_path, JSON.stringify(searchData))
}