用于 Firebase 的 Cloud Functions 为 Algolia 中的 Firebase 数据库对象编制索引 [英] Cloud Functions for Firebase to index Firebase Database Objects in Algolia

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

问题描述

我浏览了文档、github 存储库,但还没有对我有用.

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

我的数据结构:

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

1) Github 存储库:如果我使用这个,我只从一个函数中获取一个字段,如果我需要所有字段,我需要为每个字段编写单独的函数,这是一种糟糕的方法.

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 官方Node.js 文档:这不能部署为云功能,但它可以实现我的意图.

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

如何编写一个可以部署在 Firebase 上的函数,并在 Algolia 中使用其键对整个对象进行索引?

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

推荐答案

好的,所以我继续创建一个 Firebase Cloud 函数,以便为 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 => {

你应该做的是:

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)));
});

区别在于第一行:在第一种情况下,您只听text 更改,因此您只能获得包含文本更改的数据.在第二种情况下,您将获得整个对象,因为您监听了所有博客对象的变化(注意 /text 是如何被删除的).

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).

我对其进行了测试,它对我有用:包括作者在内的整个对象都在 A​​lgolia 中建立了索引.

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

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

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