Skip to content

Commit

Permalink
Merge pull request #1598 from kiminkim724/typesense-collection-backfi…
Browse files Browse the repository at this point in the history
…ll-fix

Typesense collection backfill fix
  • Loading branch information
Mephistic authored Aug 21, 2024
2 parents 26661e9 + 83a126c commit bd84f77
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion functions/src/search/SearchIndexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export class SearchIndexer {
if (!this.collection) {
const collection = this.client.collections(this.collectionName)
const exists = await collection.exists()
console.log("Collection exists", exists)
if (!exists) await this.createCollection()
this.collection = collection
}
Expand All @@ -104,7 +105,15 @@ export class SearchIndexer {
currentBatch++
if (numBatches && currentBatch > numBatches) return

const docs = batch.map(d => convert(d.data()))
const docs = batch.reduce((acc, d) => {
try {
const doc = convert(d.data())
acc.push(doc)
} catch (error: any) {
console.error(`Failed to convert document: ${error.message}`)
}
return acc
}, [] as any[])
const collection = await this.getCollection()
try {
await collection.documents().import(docs, { action: "upsert" })
Expand All @@ -131,7 +140,14 @@ export class SearchIndexer {

private async upgradeAlias() {
const { alias } = this.config
console.log("Upgrading alias", alias)
const obsoleteCollection = await this.getCurrentCollectionName()
console.log(
"Upgrading collection",
obsoleteCollection,
"to",
this.collectionName
)
await this.client
.aliases()
.upsert(alias, { collection_name: this.collectionName })
Expand Down

0 comments on commit bd84f77

Please sign in to comment.