Hey guys I have this cloud function that mirrors data to algolia on firebase write.
It works fine but now I wanted to add this:
https://www.algolia.com/doc/guides/managing-results/optimize-search-results/override-search-engine-defaults/how-to/how-can-i-make-queries-within-the-middle-of-a-word/?client=javascript
Possibility to query the words in the middle, and the way this is achieved is by breaking down property into substring array. With the following proposed method:
let objects = require("./products.json");
objects = objects.map(record => {
let reference = record.product_reference;
record.product_reference_suffixes = [];
while (reference.length > 1) {
reference = reference.substr(1);
record.product_reference_suffixes.push(reference);
}
return record;
});
index.saveObjects(objects).then(() => {
// done
});
So here is my attempt to add it to the existing function of mirroring to post-process property called title. I have it commented out because it breaks the function completely and I am really unsure how to debug this. Is there something obvious I am doing wrong, with the commented-out part?
exports.indexentry = functions.database.ref('/products/{blogid}').onWrite(
async (data, context) => {
// let reference = data.after.title;
// record.product_reference_suffixes = [];
// while (reference.length > 1) {
// reference = reference.substr(1);
// data.after.product_reference_suffixes.push(reference);
// }
const index = client.initIndex(ALGOLIA_POSTS_INDEX_NAME);
const firebaseObject = {
data: data.after.val(),
objectID: context.params.blogid
};
if (!firebaseObject.data) {
return index.deleteObject(firebaseObject.objectID)}
await index.saveObject(firebaseObject);
});
How can I match queries with records that contain the query text in the middle or the end of a word?