Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix import and document update functions #71

Open
wants to merge 2 commits into
base: node-18-upgrade
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions functions/src/backfillToTypesenseFromFirestore.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ module.exports = functions.firestore.document(config.typesenseBackfillTriggerDoc
const typesense = createTypesenseClient();

const querySnapshot =
await admin.firestore().collection(config.firestoreCollectionPath).get();
await admin.firestore().collection(config.firestoreCollectionPath).get();
let currentDocumentNumber = 0;
let currentDocumentsBatch = [];
for (const firestoreDocument of querySnapshot.docs) {
Expand All @@ -55,7 +55,9 @@ module.exports = functions.firestore.document(config.typesenseBackfillTriggerDoc
await typesense
.collections(encodeURIComponent(config.typesenseCollectionName))
.documents()
.import(currentDocumentsBatch);
.import(currentDocumentsBatch,
utils.importDocumentsConfig(currentDocumentsBatch),
);
currentDocumentsBatch = [];
functions.logger.info(`Imported ${currentDocumentNumber} documents into Typesense`);
} catch (error) {
Expand All @@ -68,7 +70,7 @@ module.exports = functions.firestore.document(config.typesenseBackfillTriggerDoc
await typesense
.collections(encodeURIComponent(config.typesenseCollectionName))
.documents()
.import(currentDocumentsBatch);
.import(utils.importDocumentsConfig(currentDocumentsBatch));
functions.logger.info(`Imported ${currentDocumentNumber} documents into Typesense`);
} catch (error) {
functions.logger.error("Import error", error);
Expand Down
7 changes: 3 additions & 4 deletions functions/src/indexToTypesenseOnFirestoreWrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ module.exports = functions.firestore.document(config.firestoreCollectionPath)
const typesense = createTypesenseClient();

if (snapshot.after.data() == null) {
// Delete
// Delete
const documentId = snapshot.before.id;
functions.logger.debug(`Deleting document ${documentId}`);
return await typesense
.collections(encodeURIComponent(config.typesenseCollectionName))
.documents(encodeURIComponent(documentId))
.delete();
} else {
// Create / update
// Create / update

// snapshot.after.ref.get() will refetch the latest version of the document
const latestSnapshot = await snapshot.after.ref.get();
Expand All @@ -25,7 +25,6 @@ module.exports = functions.firestore.document(config.firestoreCollectionPath)
functions.logger.debug(`Upserting document ${JSON.stringify(typesenseDocument)}`);
return await typesense
.collections(encodeURIComponent(config.typesenseCollectionName))
.documents()
.upsert(typesenseDocument);
.documents().upsert(typesenseDocument, {"action": "emplace", "dirty_values": "coerce_or_drop"});
}
});
7 changes: 7 additions & 0 deletions functions/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,10 @@ exports.typesenseDocumentFromSnapshot = async (

return typesenseDocument;
};

exports.importDocumentsConfig = (batchSize) => ({
action: "emplace",
dirty_values: "coerce_or_drop",
return_doc: false,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good improvement, I've seen users miss the error returned because we log the document as well.

But could you instead set return_id: true, so at least the document ID is logged for users to investigate further?

batch_size: batchSize.length,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The batch_size here controls server-side batching, and we do not want to change this from the default in most cases.

Could you remove this?

});
Loading