如何执行sql“LIKE”在firebase上操作? [英] How to perform sql "LIKE" operation on firebase?

查看:146
本文介绍了如何执行sql“LIKE”在firebase上操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase进行数据存储。数据结构如下:

  products:{
product1:{
name:chocolate ,
}
product2:{
name:chochocho,
}
}

我想对这些数据执行自动完成操作,通常我会这样写查询:

pre > 从产品中选择名称,其中productname LIKE'%+ keyword +%';所以,对于我的情况,例如,如果用户键入町,我需要把两个都带结果是巧克力和胆汁酸。我想将产品下的所有数据块都带上,然后在客户端进行查询,但是这对于大型数据库可能需要大量内存。那么,我该如何执行sql LIKE操作?



谢谢 解决方案

更新:随着Firebase云端功能的发布,还有另外一个优雅的方法可以做到这一点通过函数将Firebase链接到Algolia。这里的折衷是函数/ Algolia几乎不需要维护,但可能会比增加自己的成本Node。



目前在Firebase中没有内容搜索。很多更常见的搜索场景,比如按属性搜索,都会随着API的不断扩展而被加入到Firebase中。同时,当然也有可能增长你自己。然而,搜索是一个很大的话题(想想创建一个实时数据存储空间巨大),大大低估了,并且是应用程序的一个关键特性 - 并不是您想要特别设置的,甚至不需要像Firebase这样的人员代表您提供。因此,使用可扩展的第三方工具来处理索引,搜索,标签/模式匹配,模糊逻辑,加权排名等等通常比较简单。 $ b

Firebase博客功能使用ElasticSearch建立索引的博客文章概述一个简单的方法来将一个快速,但非常强大的搜索引擎整合到您的Firebase后端。

实质上,它分两步完成。监控数据并将其编入索引:

  var Firebase = require('firebase'); 
var ElasticClient = require('elasticsearchclient')

//初始化ElasticSearch API
var client = new ElasticClient({host:'localhost',port:9200});

//监听Firebase数据的变化
var fb = new Firebase('< INSTANCE> .firebaseio.com / widgets');
fb.on('child_added',createOrUpdateIndex);
fb.on('child_changed',createOrUpdateIndex);
fb.on('child_removed',removeIndex);
$ b $函数createOrUpdateIndex(snap){
client.index(this.index,this.type,snap.val(),snap.name())
.on('数据',函数(数据){console.log('indexed',snap.name());})
.on('error',function(err){/ * handle errors * /});


函数removeIndex(snap){
client.deleteDocument(this.index,this.type,snap.name(),function(error,data){
if(error)console.error('failed to delete',snap.name(),error); $ b $ else else console.log('deleted',snap.name());
} );

当您想搜索时查询索引:

 < 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'))
},函数(错误,响应){
//句柄响应
});
< / script>

这里有一个例子和一个第三方的lib来简化整合。


I am using firebase for data storage. The data structure is like this:

products:{
   product1:{
      name:"chocolate",
   }
   product2:{
      name:"chochocho",
   }
}

I want to perform an auto complete operation for this data, and normally i write the query like this:

"select name from PRODUCTS where productname LIKE '%" + keyword + "%'";

So, for my situation, for example, if user types "cho", i need to bring both "chocolate" and "chochocho" as result. I thought about bringing all data under "products" block, and then do the query at the client, but this may need a lot of memory for a big database. So, how can i perform sql LIKE operation?

Thanks

解决方案

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. The tradeoff here is that the Functions/Algolia is pretty much zero maintenance, but probably at increased cost over roll-your-own in Node.

There are no content searches in Firebase at present. Many of the more common search scenarios, such as searching by attribute will be baked into Firebase as the API continues to expand.

In the mean time, 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. 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.

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.

这篇关于如何执行sql“LIKE”在firebase上操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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