Make index checking and creation parallel

refactor(Index.tsx): configure MeiliSearch indexes in parallel to improve initialization performance
This commit is contained in:
2025-08-26 11:27:46 +02:00
parent 79a80f0588
commit 5d2beb8c7a

View File

@@ -1058,16 +1058,23 @@ const Index = () => {
console.log('Starting initialization...'); console.log('Starting initialization...');
addDebugInfo('Starting initialization...'); addDebugInfo('Starting initialization...');
// Initialize notes index // Check all indexes in parallel for faster initialization
const notesIndexStart = Date.now(); addDebugInfo('Checking all indexes in parallel...');
addDebugInfo('Checking notes index existence...'); const indexCheckStart = Date.now();
const notesExists = await indexExists(NOTE_INDEX);
addDebugInfo(`Notes index check completed: ${Date.now() - notesIndexStart}ms`);
const [notesExists, scratchExists, settingsExists] = await Promise.all([
indexExists(NOTE_INDEX),
indexExists(SCRATCH_INDEX),
indexExists(SETTINGS_INDEX)
]);
addDebugInfo(`All index checks completed: ${Date.now() - indexCheckStart}ms`);
// Create missing indexes
const createPromises = [];
if (!notesExists) { if (!notesExists) {
const indexStart = Date.now();
addDebugInfo('Creating notes index...'); addDebugInfo('Creating notes index...');
await fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes`, { createPromises.push(fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes`, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@@ -1077,65 +1084,12 @@ const Index = () => {
uid: NOTE_INDEX, uid: NOTE_INDEX,
primaryKey: 'id', primaryKey: 'id',
}), }),
}, 'Initialize Notes Index'); }, 'Create Notes Index'));
addDebugInfo(`Notes index created: ${Date.now() - indexStart}ms`);
} else {
addDebugInfo('Notes index already exists');
} }
// Configure notes index settings
addDebugInfo('Configuring notes sortable attributes...');
const sortableStart = Date.now();
await fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes/${NOTE_INDEX}/settings/sortable-attributes`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${MEILISEARCH_API_KEY}`,
},
body: JSON.stringify(['date']),
}, 'Configure Notes Sortable Attributes');
addDebugInfo(`Sortable attributes configured: ${Date.now() - sortableStart}ms`);
// Set ranking rules to prioritize sorting
addDebugInfo('Configuring notes ranking rules...');
const rankingStart = Date.now();
await fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes/${NOTE_INDEX}/settings/ranking-rules`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${MEILISEARCH_API_KEY}`,
},
body: JSON.stringify(['sort', 'words', 'typo', 'proximity', 'attribute', 'exactness']),
}, 'Configure Notes Ranking Rules');
addDebugInfo(`Ranking rules configured: ${Date.now() - rankingStart}ms`);
addDebugInfo('Configuring notes filterable attributes...');
const filterableStart = Date.now();
await fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes/${NOTE_INDEX}/settings/filterable-attributes`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${MEILISEARCH_API_KEY}`,
},
body: JSON.stringify(['date', 'topLetter', 'letterCount', 'topLetterFrequency']),
}, 'Configure Notes Filterable Attributes');
addDebugInfo(`Filterable attributes configured: ${Date.now() - filterableStart}ms`);
// Gap timing
const gap1Start = Date.now();
addDebugInfo('Starting scratch index setup...');
// Initialize scratch index
const scratchIndexStart = Date.now();
addDebugInfo('Checking scratch index existence...');
const scratchExists = await indexExists(SCRATCH_INDEX);
addDebugInfo(`Scratch index check completed: ${Date.now() - scratchIndexStart}ms`);
addDebugInfo(`Gap before scratch setup: ${Date.now() - gap1Start}ms`);
if (!scratchExists) { if (!scratchExists) {
const scratchStart = Date.now();
addDebugInfo('Creating scratch index...'); addDebugInfo('Creating scratch index...');
await fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes`, { createPromises.push(fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes`, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@@ -1145,52 +1099,12 @@ const Index = () => {
uid: SCRATCH_INDEX, uid: SCRATCH_INDEX,
primaryKey: 'id', primaryKey: 'id',
}), }),
}, 'Initialize Scratch Index'); }, 'Create Scratch Index'));
addDebugInfo(`Scratch index created: ${Date.now() - scratchStart}ms`);
} else {
addDebugInfo('Scratch index already exists');
} }
// Configure scratch index settings
addDebugInfo('Configuring scratch sortable attributes...');
const scratchSortableStart = Date.now();
await fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes/${SCRATCH_INDEX}/settings/sortable-attributes`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${MEILISEARCH_API_KEY}`,
},
body: JSON.stringify(['date']),
}, 'Configure Scratch Sortable Attributes');
addDebugInfo(`Scratch sortable attributes configured: ${Date.now() - scratchSortableStart}ms`);
addDebugInfo('Configuring scratch filterable attributes...');
const scratchFilterableStart = Date.now();
await fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes/${SCRATCH_INDEX}/settings/filterable-attributes`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${MEILISEARCH_API_KEY}`,
},
body: JSON.stringify(['date']),
}, 'Configure Scratch Filterable Attributes');
addDebugInfo(`Scratch filterable attributes configured: ${Date.now() - scratchFilterableStart}ms`);
// Gap timing
const gap2Start = Date.now();
addDebugInfo('Starting settings index setup...');
// Initialize settings index
const settingsIndexStart = Date.now();
addDebugInfo('Checking settings index existence...');
const settingsExists = await indexExists(SETTINGS_INDEX);
addDebugInfo(`Settings index check completed: ${Date.now() - settingsIndexStart}ms`);
addDebugInfo(`Gap before settings setup: ${Date.now() - gap2Start}ms`);
if (!settingsExists) { if (!settingsExists) {
const settingsStart = Date.now();
addDebugInfo('Creating settings index...'); addDebugInfo('Creating settings index...');
await fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes`, { createPromises.push(fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes`, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@@ -1200,24 +1114,78 @@ const Index = () => {
uid: SETTINGS_INDEX, uid: SETTINGS_INDEX,
primaryKey: 'key', primaryKey: 'key',
}), }),
}, 'Initialize Settings Index'); }, 'Create Settings Index'));
addDebugInfo(`Settings index created: ${Date.now() - settingsStart}ms`);
} else {
addDebugInfo('Settings index already exists');
} }
// Configure settings index if (createPromises.length > 0) {
addDebugInfo('Configuring settings filterable attributes...'); await Promise.all(createPromises);
const settingsFilterableStart = Date.now(); addDebugInfo('All missing indexes created');
await fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes/${SETTINGS_INDEX}/settings/filterable-attributes`, { }
// Configure all indexes in parallel
addDebugInfo('Configuring all indexes in parallel...');
const configStart = Date.now();
await Promise.all([
// Notes index configurations
fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes/${NOTE_INDEX}/settings/sortable-attributes`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${MEILISEARCH_API_KEY}`,
},
body: JSON.stringify(['date']),
}, 'Configure Notes Sortable Attributes'),
fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes/${NOTE_INDEX}/settings/ranking-rules`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${MEILISEARCH_API_KEY}`,
},
body: JSON.stringify(['sort', 'words', 'typo', 'proximity', 'attribute', 'exactness']),
}, 'Configure Notes Ranking Rules'),
fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes/${NOTE_INDEX}/settings/filterable-attributes`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${MEILISEARCH_API_KEY}`,
},
body: JSON.stringify(['date', 'topLetter', 'letterCount', 'topLetterFrequency']),
}, 'Configure Notes Filterable Attributes'),
// Scratch index configurations
fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes/${SCRATCH_INDEX}/settings/sortable-attributes`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${MEILISEARCH_API_KEY}`,
},
body: JSON.stringify(['date']),
}, 'Configure Scratch Sortable Attributes'),
fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes/${SCRATCH_INDEX}/settings/filterable-attributes`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${MEILISEARCH_API_KEY}`,
},
body: JSON.stringify(['date']),
}, 'Configure Scratch Filterable Attributes'),
// Settings index configurations
fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes/${SETTINGS_INDEX}/settings/filterable-attributes`, {
method: 'PUT', method: 'PUT',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'Authorization': `Bearer ${MEILISEARCH_API_KEY}`, 'Authorization': `Bearer ${MEILISEARCH_API_KEY}`,
}, },
body: JSON.stringify(['key', 'value']), body: JSON.stringify(['key', 'value']),
}, 'Configure Settings Filterable Attributes'); }, 'Configure Settings Filterable Attributes'),
addDebugInfo(`Settings filterable attributes configured: ${Date.now() - settingsFilterableStart}ms`); ]);
addDebugInfo(`All index configurations completed: ${Date.now() - configStart}ms`);
setIsInitialized(true); setIsInitialized(true);
const totalTime = Date.now() - initStartTime; const totalTime = Date.now() - initStartTime;