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

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

问题描述

我正在使用 Firebase 进行数据存储.数据结构是这样的:

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 + "%'";

因此,对于我的情况,例如,如果用户输入cho",结果我需要同时带上chocolate"和chochocho".我想过将所有数据都放在products"块下,然后在客户端进行查询,但这对于大数据库来说可能需要大量内存.那么,我如何执行 sql LIKE 操作?

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?

谢谢

推荐答案

更新:随着 Cloud Functions for Firebase 的发布,通过 Functions 将 Firebase 链接到 Algolia. 这里的权衡是 Functions/Algolia 很漂亮零维护,但可能比在 Node 中自己动手会增加成本.

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.

目前 Firebase 中没有内容搜索.随着 API 的不断扩展,许多更常见的搜索场景(例如按属性搜索)将融入 Firebase.

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.

与此同时,您当然可以自己种植.然而,搜索是一个庞大的主题(想想创建一个庞大的实时数据存储),被大大低估了,并且是您的应用程序的一个关键功能——不是您想要临时的,甚至不是依赖于像 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. 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.

Firebase 博客提供了一篇关于索引的博客文章ElasticSearch 概述了将快速但极其强大的搜索引擎集成到 Firebase 后端的简单方法.

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

想要搜索时查询索引:

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

这里有一个示例和一个简化集成的第三方库.

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

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