Firebase的云端函数在Algolia中为Firebase数据库对象建立索引 [英] Cloud Functions for Firebase to index Firebase Database Objects in Algolia

查看:251
本文介绍了Firebase的云端函数在Algolia中为Firebase数据库对象建立索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的数据结构

 应用程序{
文章:{
< post_keys> :{
auth_name:name,
text:some text// and other other fields
}
}
}

1)

2) Algolia官方文档为Node.js :这不能作为一个云功能部署,但它做我打算做的事。



如何编写一个可以在Firebase上部署的函数,并使用Algolia中的密钥获取整个对象的索引?

解决方案

好的,我开始创建一个Firebase Cloud功能以索引Algolia指数中的所有对象。这是解决方案:

您正在做的是这样的:

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

您应该做的是以下几点:

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

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

不同之处在于第一行:在第一种情况下,您只听 text 变化,因此您只能得到包含文字变化的数据
在第二种情况下,你得到了wh ole对象,因为你听所有的博客对象的变化(注意如何 / text 被删除)。

我测试了它,它适用于我:包括作者在内的整个对象被索引在Algolia中。

I went through docs, github repositories but nothing worked for me yet.

My datastructure:

App {
    posts : {
        <post_keys> : {
            auth_name : "name",
            text : "some text" //and many other fields
                      }
            }
    }

1) Github repository : If I use this, I only get one field from one function, if I need all the fields, I would need to write separate functions for each, which is a bad approach.

2) Algolia Official Docs for Node.js : This cannot be deployed as a cloud function, but it does what I intend to do.

How can I write a function that can be deployed on Firebase and gets the whole object indexed with its key in Algolia?

解决方案

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.

这篇关于Firebase的云端函数在Algolia中为Firebase数据库对象建立索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆