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...');
addDebugInfo('Starting initialization...');
// Initialize notes index
const notesIndexStart = Date.now();
addDebugInfo('Checking notes index existence...');
const notesExists = await indexExists(NOTE_INDEX);
addDebugInfo(`Notes index check completed: ${Date.now() - notesIndexStart}ms`);
// Check all indexes in parallel for faster initialization
addDebugInfo('Checking all indexes in parallel...');
const indexCheckStart = Date.now();
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) {
const indexStart = Date.now();
addDebugInfo('Creating notes index...');
await fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes`, {
createPromises.push(fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
@@ -1077,65 +1084,12 @@ const Index = () => {
uid: NOTE_INDEX,
primaryKey: 'id',
}),
}, 'Initialize Notes Index');
addDebugInfo(`Notes index created: ${Date.now() - indexStart}ms`);
} else {
addDebugInfo('Notes index already exists');
}, 'Create Notes Index'));
}
// 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) {
const scratchStart = Date.now();
addDebugInfo('Creating scratch index...');
await fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes`, {
createPromises.push(fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
@@ -1145,52 +1099,12 @@ const Index = () => {
uid: SCRATCH_INDEX,
primaryKey: 'id',
}),
}, 'Initialize Scratch Index');
addDebugInfo(`Scratch index created: ${Date.now() - scratchStart}ms`);
} else {
addDebugInfo('Scratch index already exists');
}, 'Create Scratch Index'));
}
// 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) {
const settingsStart = Date.now();
addDebugInfo('Creating settings index...');
await fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes`, {
createPromises.push(fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
@@ -1200,24 +1114,78 @@ const Index = () => {
uid: SETTINGS_INDEX,
primaryKey: 'key',
}),
}, 'Initialize Settings Index');
addDebugInfo(`Settings index created: ${Date.now() - settingsStart}ms`);
} else {
addDebugInfo('Settings index already exists');
}, 'Create Settings Index'));
}
// Configure settings index
addDebugInfo('Configuring settings filterable attributes...');
const settingsFilterableStart = Date.now();
await fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes/${SETTINGS_INDEX}/settings/filterable-attributes`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${MEILISEARCH_API_KEY}`,
},
body: JSON.stringify(['key', 'value']),
}, 'Configure Settings Filterable Attributes');
addDebugInfo(`Settings filterable attributes configured: ${Date.now() - settingsFilterableStart}ms`);
if (createPromises.length > 0) {
await Promise.all(createPromises);
addDebugInfo('All missing indexes created');
}
// 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',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${MEILISEARCH_API_KEY}`,
},
body: JSON.stringify(['key', 'value']),
}, 'Configure Settings Filterable Attributes'),
]);
addDebugInfo(`All index configurations completed: ${Date.now() - configStart}ms`);
setIsInitialized(true);
const totalTime = Date.now() - initStartTime;