Skip to content

Commit

Permalink
delete stale Algolia records after index update (#10058)
Browse files Browse the repository at this point in the history
  • Loading branch information
a-hariti committed May 16, 2024
1 parent a843665 commit 5354049
Showing 1 changed file with 44 additions and 15 deletions.
59 changes: 44 additions & 15 deletions scripts/algolia.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable no-console */

/**
* This script is used to index the static docs HTML files generated by Next.js into Algolia.
*
Expand All @@ -21,7 +23,7 @@ import fs from 'fs';
import {join} from 'path';

import {extrapolate, htmlToAlgoliaRecord} from '@sentry-internal/global-search';
import algoliasearch from 'algoliasearch';
import algoliasearch, {SearchIndex} from 'algoliasearch';

import {getDocsFrontMatter} from '../src/mdx';
import {FrontMatter} from '../src/types';
Expand Down Expand Up @@ -55,19 +57,47 @@ async function indexAndUpload() {
// as they are used directly by generateStaticParams() on [[..path]] page
const pageFrontMatters = await getDocsFrontMatter();
const records = await generateAlogliaRecords(pageFrontMatters);
// eslint-disable-next-line no-console
console.log('πŸ”₯ Generated %d Algolia records.', records.length);
// eslint-disable-next-line no-console
console.log('πŸ”₯ Saving records ...');
await index
.saveObjects(records, {
batchSize: 10000,
autoGenerateObjectIDIfNotExist: true,
})
.then(result => {
// eslint-disable-next-line no-console
console.log('πŸ”₯ Saved %d Algolia records', result.objectIDs.length);
});
console.log('πŸ”₯ Generated %d new Algolia records.', records.length);
const existingRecordIds = await fetchExistingRecordIds(index);
console.log(
'πŸ”₯ Found %d existing Algolia records in `%s`',
existingRecordIds.length,
indexName
);
console.log('πŸ”₯ Saving new records to `%s`...', indexName);
const saveResult = await index.saveObjects(records, {
batchSize: 10000,
autoGenerateObjectIDIfNotExist: true,
});
const newRecordIDs = new Set(saveResult.objectIDs);
console.log('πŸ”₯ Saved %d records', newRecordIDs.size);

const recordsToDelete = existingRecordIds.filter(id => !newRecordIDs.has(id));
if (recordsToDelete.length === 0) {
console.log('πŸ”₯ No stale records to delete');
return;
}
console.log('πŸ”₯ Deleting old (stale) records ...');
const deleteResult = await index.deleteObjects(recordsToDelete);
console.log(
'πŸ”₯ Deleted %d stale records from `%s`',
deleteResult.objectIDs.length,
indexName
);
}

async function fetchExistingRecordIds(algoliaIndex: SearchIndex) {
console.log('πŸ”₯ fetching existing records ids ...');
const existingRecordIds = new Set<string>();
await algoliaIndex.browseObjects({
attributesToRetrieve: ['objectID'],
batch: chunk => {
chunk.forEach(record => {
existingRecordIds.add(record.objectID);
});
},
});
return Array.from(existingRecordIds);
}

async function generateAlogliaRecords(pageFrontMatters: FrontMatter[]) {
Expand All @@ -77,7 +107,6 @@ async function generateAlogliaRecords(pageFrontMatters: FrontMatter[]) {
frontMatter => !frontMatter.draft && !frontMatter.noindex && frontMatter.title
)
.map(pageFm => {
// eslint-disable-next-line no-console
console.log('processing:', pageFm.slug);

const htmlFile = join(staticHtmlFilesPath, pageFm.slug + '.html');
Expand Down

0 comments on commit 5354049

Please sign in to comment.