Skip to content Skip to sidebar Skip to footer

Cloud Functions For Firebase To Index Firebase Database Objects In Algolia

I went through docs, github repositories but nothing worked for me yet. My datastructure: App { posts : { : { auth_name : 'name',

Solution 1:

Okay so I went ahead to create a Firebase Cloud function in order to index all objects in the Algolia index. This is the solution:

What you were doing is something like this:

exports.indexentry = functions.database.ref('/blog-posts/{blogid}/text').onWrite(event => {

What you should do is the following:

exports.indexentry = functions.database.ref('/blog-posts/{blogid}').onWrite(event => {
  const index = client.initIndex(ALGOLIA_POSTS_INDEX_NAME);
  var firebaseObject = event.data.val();
  firebaseObject.objectID = event.params.blogid;

  return index.saveObject(firebaseObject).then(
      () => event.data.adminRef.parent.child('last_index_timestamp').set(
          Date.parse(event.timestamp)));
});

The difference is in the first line: In the first case, you only listen to text changes, hence you only get the data containing the text change. In the second case, you get the whole object since you listen to changes in all of the blog object (notice how /text was removed).

I tested it and it works for me: whole object including author was indexed in Algolia.

Post a Comment for "Cloud Functions For Firebase To Index Firebase Database Objects In Algolia"