Update: With the release of Cloud Functions for Firebase, there's another elegant way to do this as well by linking Firebase to Algolia via Functions .
(更新:随着针对Firebase的Cloud Functions的发布,还有另一种优雅的方法也可以通过通过Functions将Firebase链接到Algolia来做到这一点。)
The tradeoff here is that the Functions/Algolia is pretty much zero maintenance, but probably at increased cost over roll-your-own in Node.(这里的权衡是,功能/算法几乎是零维护,但可能要比Node中的“滚动维护”成本更高。)
There are no content searches in Firebase at present.
(目前在Firebase中没有内容搜索。)
Many of the more common search scenarios, such as searching by attribute will be baked into Firebase as the API continues to expand.(随着API的不断扩展,许多更常见的搜索方案(例如按属性搜索)将被烘焙到Firebase中。)
In the meantime, it's certainly possible to grow your own.
(同时,当然有可能自己发展。)
However, searching is a vast topic (think creating a real-time data store vast), greatly underestimated, and a critical feature of your application--not one you want to ad hoc or even depend on someone like Firebase to provide on your behalf.(但是,搜索是一个广泛的话题(想创建一个庞大的实时数据存储区),被大大低估了,并且是应用程序的一项关键功能-不是您要临时使用的,甚至不是依靠Firebase这样的人来代表您的应用程序。)
So it's typically simpler to employ a scalable third party tool to handle indexing, searching, tag/pattern matching, fuzzy logic, weighted rankings, et al.(因此,采用可扩展的第三方工具来处理索引,搜索,标签/模式匹配,模糊逻辑,加权排名等通常更为简单。)
The Firebase blog features a blog post on indexing with ElasticSearch which outlines a straightforward approach to integrating a quick, but extremely powerful, search engine into your Firebase backend.
(Firebase博客的特色是有关使用ElasticSearch进行索引的博客文章 ,概述了一种将快速但功能强大的搜索引擎集成到Firebase后端的简单方法。)
Essentially, it's done in two steps.
(本质上,它是分两个步骤完成的。)
Monitor the data and index it:(监视数据并为其编制索引:)
var Firebase = require('firebase');
var ElasticClient = require('elasticsearchclient')
// initialize our ElasticSearch API
var client = new ElasticClient({ host: 'localhost', port: 9200 });
// listen for changes to Firebase data
var fb = new Firebase('<INSTANCE>.firebaseio.com/widgets');
fb.on('child_added', createOrUpdateIndex);
fb.on('child_changed', createOrUpdateIndex);
fb.on('child_removed', removeIndex);
function createOrUpdateIndex(snap) {
client.index(this.index, this.type, snap.val(), snap.name())
.on('data', function(data) { console.log('indexed ', snap.name()); })
.on('error', function(err) { /* handle errors */ });
}
function removeIndex(snap) {
client.deleteDocument(this.index, this.type, snap.name(), function(error, data) {
if( error ) console.error('failed to delete', snap.name(), error);
else console.log('deleted', snap.name());
});
}
Query the index when you want to do a search:
(要进行搜索时查询索引:)
<script src="elastic.min.js"></script>
<script src="elastic-jquery-client.min.js"></script>
<script>
ejs.client = ejs.jQueryClient('http://localhost:9200');
client.search({
index: 'firebase',
type: 'widget',
body: ejs.Request().query(ejs.MatchQuery('title', 'foo'))
}, function (error, response) {
// handle response
});
</script>
There's an example, and a third party lib to simplify integration, here.
(这里有一个例子,还有一个第三方库来简化集成。)