Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
418 views
in Technique[技术] by (71.8m points)

javascript - 如何在Firebase上执行SQL“ LIKE”操作?(How to perform sql “LIKE” operation on firebase?)

I am using firebase for data storage.

(我正在使用Firebase进行数据存储。)

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.

(因此,以我的情况为例,例如,如果用户键入“ cho”,则需要同时携带“ chocolate”和“ chochocho”。)

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?

(那么,如何执行sql LIKE操作?)

Thanks

(谢谢)

  ask by yrazlik translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

(这里有一个例子,还有一个第三方库来简化集成。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...