-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathroute.ts
More file actions
76 lines (68 loc) · 2.72 KB
/
Copy pathroute.ts
File metadata and controls
76 lines (68 loc) · 2.72 KB
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
import { MeiliSearch } from 'meilisearch';
import { NextResponse } from 'next/server';
import { mapSortableAttributes } from 'schema/maps';
import { getEnvVars } from 'services/env';
import { getLog } from 'services/logging/server_logger';
import { MapsRepo } from 'services/maps/maps_repo';
import { MeilisearchIndex, SearchableMap } from 'services/search/meilisearch';
const TEMP_MAPS_INDEX_UID = 'maps-new';
export async function GET() {
await rebuildMeilisearchIndex();
return new NextResponse('Success', { status: 200 });
}
export async function rebuildMeilisearchIndex() {
const log = getLog(['maps', 'search', 'rebuild']);
const { meilisearchHost, meilisearchKey } = getEnvVars();
const client = new MeiliSearch({ host: meilisearchHost, apiKey: meilisearchKey });
log.info('Creating new indexes');
await client.createIndex(TEMP_MAPS_INDEX_UID).waitTask();
log.info('Getting index');
const tempNewIndex = await client.getIndex<SearchableMap>(TEMP_MAPS_INDEX_UID);
const searchIndex = new MeilisearchIndex(tempNewIndex);
const mapsRepo = new MapsRepo(searchIndex);
// TODO: paginate / stream this
const mapsResult = await mapsRepo.findMaps({ by: 'all' });
if (!mapsResult.success) {
throw new Error(JSON.stringify(mapsResult.errors));
}
log.info('Setting up attribute fields');
const updateRanking = await searchIndex._index.updateRankingRules([
'sort',
'words',
'typo',
'proximity',
'attribute',
'exactness',
]);
const updateSearch = await searchIndex._index.updateSearchableAttributes([
'title',
'artist',
'author',
'description',
]);
const updateFilters = await searchIndex._index.updateFilterableAttributes([
'artist',
'author',
'uploader',
]);
log.info('Adding sortable attributes: ' + mapSortableAttributes.join(', '));
const updateSorts = await searchIndex._index.updateSortableAttributes([...mapSortableAttributes]);
const [_1, _2, _3, _4] = await client.tasks.waitForTasks(
[updateRanking, updateSearch, updateFilters, updateSorts].map((t) => t.taskUid),
{ timeout: 60000 }
);
log.info(`Added ${mapsResult.value.length} maps, waiting for tasks to complete...`);
log.info('Adding data');
try {
await searchIndex.updateDocuments(mapsResult.value);
} catch (e) {
log.info(`Error: ${JSON.stringify(e)}`);
}
log.info('Swapping indexes');
// If the index already exists, then do a proper swap - otherwise, just rename the new temp index
const rename = (await client.getIndexes()).results.find((i) => i.uid === 'maps') == null;
await client.swapIndexes([{ indexes: [TEMP_MAPS_INDEX_UID, 'maps'], rename }]).waitTask();
log.info('Deleting old index');
await client.deleteIndexIfExists(TEMP_MAPS_INDEX_UID);
log.info('Done!');
}