使用 Firebase 自动完成 [英] Autocomplete with Firebase

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

问题描述

如何使用 Firebase 进行基本的自动完成/文本预览?

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

例如,假设有一个由 Firebase 支持的博客,博主可以在其中使用标签标记帖子.当博主正在标记新帖子时,如果他们能够看到与他们输入的前几次击键匹配的所有当前存在的标签,将会很有帮助.因此,如果blog"、black"、blazing saddles"和bulldogs"是标签,如果用户输入bl",他们会得到前三个而不是bulldogs".

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').limitToFirst(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 -- 但我会喜欢尽可能多地保留在 Firebase 中.)

(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.)

编辑

正如 Kato 所建议的,这是一个具体的例子.我们有 20,000 个用户,他们的名字是这样存储的:

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

/users/$userId/name

通常,用户会按名称查找另一个用户.当用户正在查找他们的好友时,我们想要一个下拉列表来填充名称以搜索者输入的字母开头的用户列表.因此,如果我输入Ja",我希望在下拉菜单中看到Jake Heller"、jake gyllenhaal"、Jack Donaghy"等.

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.

推荐答案

我知道这是一个古老的话题,但它仍然具有相关性.根据上面 Neil 的回答,您可以更轻松地进行以下搜索:

I know this is an old topic, but it's still relevant. Based on Neil's answer above, you more easily search doing the following:

fb.child('tags').startAt(queryString).endAt(queryString + 'uf8ff').limit(5)

请参阅Firebase 检索数据.

上面查询中使用的 uf8ff 字符是一个非常高的代码点在 Unicode 范围内.因为它在大多数常规字符之后Unicode,查询匹配所有以 queryString 开头的值.

The uf8ff character used in the query above is a very high code point in the Unicode range. Because it is after most regular characters in Unicode, the query matches all values that start with queryString.

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

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