自动完成与火力地堡 [英] Autocomplete with Firebase

查看:217
本文介绍了自动完成与火力地堡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个人如何使用火力地堡做基本的自动完成/文preVIEW?

How does one use Firebase to do basic auto-completion/text preview?

例如,想象一下火力地堡支持一个博客,博主可以标记与标签的帖子。随着博客的标记一个新的岗位,这将是有益的,如果他们能看到所有匹配他们已经进入了第几个按键当前存在的标签。因此,如果博客,黑,炽热鞍座,和牛头犬是代码,如果用户键入BL他们得到的前三个,但不是牛头犬。

For example, imagine a blog backed by Firebase where the blogger can tag posts with tags. As the blogger is tagging a new post, it would be helpful if they could see all currently-existing tags that matched the first few keystrokes they've entered. So if "blog," "black," "blazing saddles," and "bulldogs" were tags, if the user types "bl" they get the first three but not "bulldogs."

我最初的想法是,我们可以设置与标签的优先级标记,并使用startAt,这样我们的查询看起来是这样的:

My initial thought was that we could set the tag with the priority of the tag, and use startAt, such that our query would look something like:

fb.child('tags').startAt('bl').limit(5).once('value', function(snap) {
  console.log(snap.val()) 
});

但是,这也将返回斗牛犬的结果之一(不是世界末日,但不是最好的)。使用startAt('BL')。ENDAT('BL')不返回任何结果。有另一种方式做到这一点?

But this would also return "bulldog" as one of the results (not the end of the world, but not the best either). Using startAt('bl').endAt('bl') returns no results. Is there another way to accomplish this?

(我知道,一个选择是,这是我们可以使用一个搜索服务器,像ElasticSearch,为 - 看到的 https://www.firebase.com/blog/2014-01-02-queries-part-two.html - 但我爱让尽可能多的火力地堡越好。)

(I know that one option is that this is something we could use a search server, like ElasticSearch, for -- see https://www.firebase.com/blog/2014-01-02-queries-part-two.html -- but I'd love to keep as much in Firebase as possible.)

修改

由于加藤认为,这里有一个具体的例子。我们有20000名用户,他们的名字存储为这样的:

As Kato suggested, here's a concrete example. We have 20,000 users, with their names stored as such:

/users/$userId/name

通常情况下,用户将被仰视的名字其他用户。当用户正在寻找他们的伙伴,我们希望一个下拉填充用户名字开始与该搜索者输入了字母的列表。所以,如果我在JA类型我希望看到杰克海勒,杰克·吉伦哈尔,杰克·多纳西等在下拉列表中。

Oftentimes, users will be looking up another user by name. As a user is looking up their buddy, we'd like a drop-down to populate a list of users whose names start with the letters that the searcher has inputted. So if I typed in "Ja" I would expect to see "Jake Heller," "jake gyllenhaal," "Jack Donaghy," etc. in the drop-down.

推荐答案

作为灵感来自加藤的意见 - 要解决这个问题的一种方法是配置的优先级要搜索为您自动完成,并使用startAt(领域),限制(),和客户端筛选只返回你想要的结果。你要确保优先级和搜索词是小写,因为火力地堡是区分大小写的。

As inspired by Kato's comments -- one way to approach this problem is to set the priority to the field you want to search on for your autocomplete and use startAt(), limit(), and client-side filtering to return only the results that you want. You'll want to make sure that the priority and the search term is lower-cased, since Firebase is case-sensitive.

这是一个粗糙的例子来证明这一点使用用户的例子,我在这个问题奠定了:

This is a crude example to demonstrate this using the Users example I laid out in the question:

有关为JA的搜索,假定所有用户将其优先级设置为用户的姓名的小写版本

For a search for "ja", assuming all users have their priority set to the lowercased version of the user's name:

fb.child('users').
  startAt('ja'). // The user-inputted search
  limit(20).
  once('value', function(snap) {
    for(key in snap.val()){
      if(snap.val()[key].indexOf('ja') === 0) {
        console.log(snap.val()[key];
      }
    }
});

这应该只返回实际为JA(即使火力地堡实际上返回名称的字母顺序JA之后)开始的名字。

This should only return the names that actually begin with "ja" (even if Firebase actually returns names alphabetically after "ja").

我选择使用限制(20)保持反应体积小,因为,现实的是,你永远不会需要超过20个的自动完成下拉。可能有更好的方法做过滤,但是这至少应该表现出的概念。

I choose to use limit(20) to keep the response size small and because, realistically, you'll never need more than 20 for the autocomplete drop-down. There are probably better ways to do the filtering, but this should at least demonstrate the concept.

希望这可以帮助别人!而且它很可能在火力地堡家伙有一个更好的答案。

Hope this helps someone! And it's quite possible the Firebase guys have a better answer.

(注意,这是非常有限的 - 如果有人搜索的姓氏,它不会返回他们在找什么因此,最佳答案很可能是使用搜索后端的东西,如加藤的< A HREF =htt​​ps://github.com/firebase/flashlight相对=nofollow>手电筒。)

(Note that this is very limited -- if someone searches for the last name, it won't return what they're looking for. Hence the "best" answer is probably to use a search backend with something like Kato's Flashlight.)

这篇关于自动完成与火力地堡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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