« get me outta code hell

search-spec.js « util « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/util/search-spec.js
blob: b26869a2bb50f38e27cf0298fe51cd9f5c37e498 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Index structures shared by client and server, and relevant interfaces.

export const searchSpec = {
  albums: {
    query: ({albumData}) => albumData,

    process: (album) => ({
      name:
        album.name,

      groups:
        album.groups.map(group => group.name),
    }),

    index: [
      'name',
      'groups',
    ],
  },

  tracks: {
    query: ({trackData}) => trackData,

    process: (track) => ({
      name:
        track.name,

      color:
        track.color,

      album:
        track.album.name,

      albumDirectory:
        track.album.directory,

      artists:
        track.artistContribs
          .map(contrib => contrib.artist)
          .flatMap(artist => [artist.name, ...artist.aliasNames]),

      additionalNames:
        track.additionalNames
          .map(entry => entry.name),

      artworkKind:
        (track.hasUniqueCoverArt
          ? 'track'
       : track.album.hasCoverArt
          ? 'album'
          : 'none'),
    }),

    index: [
      'name',
      'album',
      'artists',
      'additionalNames',
    ],

    store: [
      'color',
      'name',
      'albumDirectory',
      'artworkKind',
    ],
  },

  artists: {
    query: ({artistData}) =>
      artistData
        .filter(artist => !artist.isAlias),

    process: (artist) => ({
      names:
        [artist.name, ...artist.aliasNames],
    }),

    index: [
      'names',
    ],
  },

  groups: {
    query: ({groupData}) => groupData,

    process: (group) => ({
      names: group.name,
      description: group.description,
      // category: group.category
    }),

    index: [
      'name',
      'description',
      // 'category',
    ],
  },

  flashes: {
    query: ({flashData}) => flashData,

    process: (flash) => ({
      name:
        flash.name,

      tracks:
        flash.featuredTracks
          .map(track => track.name),

      contributors:
        flash.contributorContribs
          .map(contrib => contrib.artist)
          .flatMap(artist => [artist.name, ...artist.aliasNames]),
    }),

    index: [
      'name',
      'tracks',
      'contributors',
    ],
  },
};

export function makeSearchIndex(descriptor, {FlexSearch}) {
  return new FlexSearch.Document({
    id: 'reference',
    index: descriptor.index,
    store: descriptor.store,
  });
}

export function populateSearchIndex(index, descriptor, {wikiData}) {
  const collection = descriptor.query(wikiData);

  for (const thing of collection) {
    const reference = thing.constructor.getReference(thing);

    let processed;
    try {
      processed = descriptor.process(thing);
    } catch (caughtError) {
      throw new Error(
        `Failed to process searchable thing ${reference}`,
        {cause: caughtError});
    }

    index.add({reference, ...processed});
  }
}