我正在尝试索引包含Algolia子集的Firestore集合,并且在执行初始索引以及正在进行的同步(在创建/更新/删除时)时需要一些索引子集的帮助。
收集的示例是:“ collection1 / subcollection”
第一个任务是将集合和子集合初始同步到Algoia
第二项任务是正在进行的同步(在创建/更新/删除时)。
我将这段代码用于从Firestore到Algolia的初始索引:
const algoliasearch = require('algoliasearch')
const dotenv = require('dotenv')
const firebase = require('firebase');
const firestore = require('firebase/firestore');
// load values from the .env file in this directory into process.env
dotenv.config();
// initializes the firebase database.
firebase.initializeApp({
projectId: process.env.FIREBASE_PROJECT_ID,
databaseURL: process.env.FIREBASE_DATABASE_URL
})
const db = firebase.firestore();
// configure algolia
const algolia = algoliasearch(
process.env.ALGOLIA_APP_ID,
process.env.ALGOLIA_API_KEY
);
const index = algolia.initIndex(process.env.ALGOLIA_INDEX_NAME);
var docRef = db.collection(process.env.ALGOLIA_INDEX_NAME);
const records = [];
db.collection(process.env.ALGOLIA_INDEX_NAME).get()
.then((snapshot) => {
snapshot.forEach((doc) => {
// get the key and data from the snapshot
const childKey = doc.id;
const childData = doc.data();
// We set the Algolia objectID as the Firebase .key
childData.objectID = childKey;
// Add object for indexing
records.push(childData);
console.log(doc.id, '=>', doc.data());
});
// Add or update new objects
index.saveObjects(records).then(() => {
console.log('Documents imported into Algolia');
process.exit(0);
})
.catch(error => {
console.error('Error when importing documents into Algolia', error);
process.exit(1);
});
})
.catch((err) => {
console.error('Error getting documents', error);
});
然后是onCreate代码的示例:
exports.indexCollection1 = functions.firestore
.document('collection1/{ID}')
.onCreate((snap, context) => {
const data = snap.data();
const objectID = snap.id;
// Add the data to the algolia index
return index.addObject({
objectID,
...data
});
});
我可以对collection1和onCreate / Update / Delete进行初始索引,但是我无法为collection1的子集合(collection1 / subcollection)建立索引,也无法通过云函数创建/更新/删除。
任何帮助,建议,链接或建议,将不胜感激。