如何仅从 Firestore 中的文档发送 Algolia 特定字段
How to only send Algolia specific fields from a document in Firestore
我刚开始使用 Algolia。我正在尝试将用户数据从我的 Firestore 发送到 Aloglia,以便稍后在我正在创建的应用程序中搜索它们。我已经成功地设置了一切,我的所有功能都通过 Firebase 云功能运行。因此,当我在 Firestore 中为 'users' 创建文档时,它会将所有字段传递给 Algolia,更新和删除此数据也会在 Algolia 中体现。
但是,为了安全起见,我不想将所有用户数据都发送到 Algolia,而是只发送几个字段。其中包括 'displayName' 和 'username'('users' 集合中的两个字段)。
所以我的问题是如何更改我的代码以仅将这两个字段发送到 Algolia?
请同时提供删除和更新的答案。
我的代码:
const functions = require("firebase-functions");
const admin = require('firebase-admin');
const algoliasearch = require('algoliasearch');
const ALGOLIA_APP_ID = "MY KEY";
const ALGOLIA_ADMIN_KEY = "MY KEY";
const ALGOLIA_INDEX_NAME = "users";
var client = algoliasearch(ALGOLIA_APP_ID, ALGOLIA_ADMIN_KEY);
admin.initializeApp(functions.config().firebase);
exports.createUser = functions.firestore
.document('users/{userID}')
.onCreate( async (snap, context) => {
const newValue = snap.data();
newValue.objectID = snap.id;
var index = client.initIndex(ALGOLIA_INDEX_NAME);
index.saveObject(newValue);
});
exports.updateUser = functions.firestore
.document('users/{userID}')
.onUpdate( async (snap, context) => {
const afterUpdate = snap.after.data();
afterUpdate.objectID = snap.after.id;
var index = client.initIndex(ALGOLIA_INDEX_NAME);
index.saveObject(afterUpdate);
})
exports.deleteUser = functions.firestore
.document('users/{userID}/')
.onDelete( async (snap, context) => {
const oldID = snap.id;
var index = client.initIndex(ALGOLIA_INDEX_NAME);
index.deleteObject(oldID);
});
Algolia 有一些关于如何对索引执行添加、更新和删除操作的 good documentation。请注意,要添加和更新,您需要传递 objectID
.
仅添加某些字段
使用您的示例中的一些代码,您可以将 Firestore 对象的某些字段仅传递给 Algolia (example from docs):
exports.createUser = functions.firestore
.document("users/{userId}")
.onCreate((snap, context) => {
const newValue = snap.data();
const user = {
objectID: context.params.userId,
displayName: newValue.displayName,
username: newValue.username,
};
return index.saveObject(user);
});
仅更新某些字段
也受到an example in the docs的启发:
exports.updateUser = functions.firestore
.document("users/{userId}")
.onUpdate(async (snap, context) => {
const afterUpdate = snap.after.data();
const updateUser = {
objectID: context.params.userId,
displayName: afterUpdate.displayName,
username: afterUpdate.username,
};
await index.partialUpdateObject(updateUser);
});
正在删除
可以 delete by filters,虽然我没有这样做,并且文档表明您应该改用 deleteObject
方法,因为它的性能更高。你可以这样做:
exports.deleteUser = functions.firestore
.document("users/{userId}")
.onDelete((snap) => {
return index.deleteObject(snap.id);
});
我刚开始使用 Algolia。我正在尝试将用户数据从我的 Firestore 发送到 Aloglia,以便稍后在我正在创建的应用程序中搜索它们。我已经成功地设置了一切,我的所有功能都通过 Firebase 云功能运行。因此,当我在 Firestore 中为 'users' 创建文档时,它会将所有字段传递给 Algolia,更新和删除此数据也会在 Algolia 中体现。
但是,为了安全起见,我不想将所有用户数据都发送到 Algolia,而是只发送几个字段。其中包括 'displayName' 和 'username'('users' 集合中的两个字段)。
所以我的问题是如何更改我的代码以仅将这两个字段发送到 Algolia? 请同时提供删除和更新的答案。
我的代码:
const functions = require("firebase-functions");
const admin = require('firebase-admin');
const algoliasearch = require('algoliasearch');
const ALGOLIA_APP_ID = "MY KEY";
const ALGOLIA_ADMIN_KEY = "MY KEY";
const ALGOLIA_INDEX_NAME = "users";
var client = algoliasearch(ALGOLIA_APP_ID, ALGOLIA_ADMIN_KEY);
admin.initializeApp(functions.config().firebase);
exports.createUser = functions.firestore
.document('users/{userID}')
.onCreate( async (snap, context) => {
const newValue = snap.data();
newValue.objectID = snap.id;
var index = client.initIndex(ALGOLIA_INDEX_NAME);
index.saveObject(newValue);
});
exports.updateUser = functions.firestore
.document('users/{userID}')
.onUpdate( async (snap, context) => {
const afterUpdate = snap.after.data();
afterUpdate.objectID = snap.after.id;
var index = client.initIndex(ALGOLIA_INDEX_NAME);
index.saveObject(afterUpdate);
})
exports.deleteUser = functions.firestore
.document('users/{userID}/')
.onDelete( async (snap, context) => {
const oldID = snap.id;
var index = client.initIndex(ALGOLIA_INDEX_NAME);
index.deleteObject(oldID);
});
Algolia 有一些关于如何对索引执行添加、更新和删除操作的 good documentation。请注意,要添加和更新,您需要传递 objectID
.
仅添加某些字段
使用您的示例中的一些代码,您可以将 Firestore 对象的某些字段仅传递给 Algolia (example from docs):
exports.createUser = functions.firestore
.document("users/{userId}")
.onCreate((snap, context) => {
const newValue = snap.data();
const user = {
objectID: context.params.userId,
displayName: newValue.displayName,
username: newValue.username,
};
return index.saveObject(user);
});
仅更新某些字段
也受到an example in the docs的启发:
exports.updateUser = functions.firestore
.document("users/{userId}")
.onUpdate(async (snap, context) => {
const afterUpdate = snap.after.data();
const updateUser = {
objectID: context.params.userId,
displayName: afterUpdate.displayName,
username: afterUpdate.username,
};
await index.partialUpdateObject(updateUser);
});
正在删除
可以 delete by filters,虽然我没有这样做,并且文档表明您应该改用 deleteObject
方法,因为它的性能更高。你可以这样做:
exports.deleteUser = functions.firestore
.document("users/{userId}")
.onDelete((snap) => {
return index.deleteObject(snap.id);
});