Firebase 和索引/搜索
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10559191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Firebase and indexing/search
提问by Gene Golovchinsky
I am considering using Firebase for an application that should people to use full-text search over a collection of a few thousand objects. I like the idea of delivering a client-only application (not having to worry about hosting the data), but I am not sure how to handle search. The data will be static, so the indexing itself is not a big deal.
我正在考虑将 Firebase 用于一个应用程序,该应用程序应该人们对几千个对象的集合使用全文搜索。我喜欢提供仅客户端应用程序的想法(不必担心托管数据),但我不确定如何处理搜索。数据将是静态的,因此索引本身并不是什么大问题。
I assume I will need some additional service that runs queries and returns Firebase object handles. I can spin up such a service at some fixed location, but then I have to worry about its availability ad scalability. Although I don't expect too much traffic for this app, it can peak at a couple of thousand concurrent users.
我假设我需要一些额外的服务来运行查询并返回 Firebase 对象句柄。我可以在某个固定位置启动这样的服务,但随后我不得不担心它的可用性和可扩展性。虽然我不希望这个应用程序有太多的流量,但它可以达到几千个并发用户的峰值。
Architectural thoughts?
建筑思想?
回答by Michael Lehenbauer
Long-term, Firebase may have more advanced querying, so hopefully it'll support this sort of thing directly without you having to do anything special. Until then, you have a few options:
从长远来看,Firebase 可能有更高级的查询,所以希望它可以直接支持这类事情,而无需您做任何特殊的事情。在此之前,您有几个选择:
- Write server code to handle the searching.The easiest way would be to run some server code responsible for the indexing/searching, as you mentioned. Firebase has a Node.JS client, so that would be an easy way to interface the service into Firebase. All of the data transfer could still happen through Firebase, but you would write a Node.JS service that watches for client "search requests" at some designated location in Firebase and then "responds" by writing the result set back into Firebase, for the client to consume.
- Store the index in Firebase with clients automatically updating it.If you want to get really clever, you could try implementing a server-less scheme where clients automatically index their data as they write it... So the index for the full-text search would be stored in Firebase, and when a client writes a new item to the collection, it would be responsible for also updating the index appropriately. And to do a search, the client would directly consume the index to build the result set. This actually makes a lot of sense for simple cases where you want to index one field of a complex object stored in Firebase, but for full-text-search, this would probably be pretty gnarly. :-)
- Store the index in Firebase with server code updating it.You could try a hybrid approach where the index is stored in Firebase and is used directly by clients to do searches, but rather than have clients update the index, you'd have server code that updates the index whenever new items are added to the collection. This way, clients could still search for data when your server is down. They just might get stale results until your server catches up on the indexing.
- 编写服务器代码来处理搜索。正如您所提到的,最简单的方法是运行一些负责索引/搜索的服务器代码。Firebase 有一个 Node.JS 客户端,因此这是将服务连接到 Firebase 的一种简单方法。所有数据传输仍然可以通过 Firebase 进行,但是您可以编写一个 Node.JS 服务来监视 Firebase 中某个指定位置的客户端“搜索请求”,然后通过将结果集写回 Firebase 来“响应”,对于客户消费。
- 将索引存储在 Firebase 中,客户端会自动更新它。如果你想变得非常聪明,你可以尝试实现一个无服务器方案,客户端在写入数据时自动索引他们的数据......所以全文搜索的索引将存储在 Firebase 中,当客户端写入时集合中的新项目,它将负责相应地更新索引。为了进行搜索,客户端将直接使用索引来构建结果集。对于您想要索引存储在 Firebase 中的复杂对象的一个字段的简单情况,这实际上很有意义,但对于全文搜索,这可能非常粗糙。:-)
- 将索引存储在 Firebase 中,服务器代码更新它。您可以尝试一种混合方法,其中索引存储在 Firebase 中并由客户端直接使用来进行搜索,但不是让客户端更新索引,而是让服务器代码在新项目添加到集合时更新索引. 这样,当您的服务器关闭时,客户端仍然可以搜索数据。在您的服务器赶上索引编制之前,他们可能会得到陈旧的结果。
Until Firebase has more advanced querying, #1 is probably your best bet if you're willing to run a little server code. :-)
在 Firebase 有更高级的查询之前,如果您愿意运行一些服务器代码,#1 可能是您最好的选择。:-)
回答by cgenco
Google's current method to do full text search seems to be syncing with either Algoliaor BigQuerywith Cloud Functions for Firebase.
谷歌目前进行全文搜索的方法似乎是与Algolia或BigQuery与Cloud Functions for Firebase同步。
Here's Firebase's Algolia Full-text search integration example, and their BigQuery integration examplethat could be extended to support full search.
这是 Firebase 的Algolia 全文搜索集成示例,以及可以扩展以支持完整搜索的BigQuery 集成示例。

