使用 firebase 实时数据库创建搜索功能 [英] Use firebase realtime database create search function

查看:31
本文介绍了使用 firebase 实时数据库创建搜索功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 firebase 实时数据库为我的应用创建一个搜索功能,类似于 facebook 搜索.

I would like to create a search feature for my apps by using firebase real time database, which similar to facebook search.

我做了一些研究并承认 firebase real time 无法使用文本包含"进行搜索.但我相信必须有某种方法来实现它,比如结合 startAt 或 endAt.

I did some research and acknowledge that firebase real time is not able to search with "text contain". But i believe there must be some method to achieve it, like combine startAt or endAt.

我需要一些最佳实践的建议.

I need some advise the best practice to do it.

数据库结构如下:

Restaurant
-->RandomID
---->name: 'Good Restaurant'
---->address:
---->desc:
-->RandomID
---->name: 'Dixon Lee Restaurant'
---->address:
---->desc:
-->RandomID
---->name: 'MCD'
---->address:
---->desc:

已编辑我不确定 startAt 和 endAt 是如何工作的.如果我使用 startAt(3) 是否应该返回 3 及以上?如果我对字符串使用 startAt 怎么办?就像我遇到的以下问题:

Edited I not sure how the startAt and endAt work. Is it supposes to return 3 and above if i use startAt(3)? What if i use startAt with string? Like below problem i ecnounter:

firebase.database().ref('/restaurants').orderByChild('name').startAt('Good').on('value',snap=>{
      console.log(snap.val())
    })

上面的查询将返回:

我正在考虑使用 startAt/endAt 实现文本包含",它将找到以关键字开头/结尾的字段的特定值.不确定这是 startAt/endAt 的用法.

I am thinking to achieve "text contains" with startAt/endAt, which will find specific value of field that begin/end with a keyword. Not sure is this the usage for startAt/endAt.

推荐答案

您似乎将 startAt 与接受以 特定值开头的字符串的函数混淆了.这根本不是 Firebase 过滤的工作方式.

You seem to be confusing startAt with a function that would accepts strings that start with a certain value. That's simply not how Firebase filtering work.

当您执行 ref('/restaurants').orderByChild('name').startAt('Good') 时,它会做三件事:

When you do ref('/restaurants').orderByChild('name').startAt('Good'), it does three things:

  1. 根据 name 值对 restaurants 的所有子节点进行排序.
  2. 找到以 Good 开头的第一个(或 Good 之后的任何字典)
  3. 从那里返回每个孩子
  1. Orders all child nodes of restaurants on their name value.
  2. Find the first one starting with Good (or anything lexicographically after Good)
  3. Returns every child from there on

所以你得到了名为 Good RestaurantMCD 的子节点.

So you're getting the child nodes with name Good Restaurant and MCD.

您可以使用startAtendAt 返回范围 的子节点,即所有以开头的子节点> .为此,您需要创建一个从 Good 开始到 Good\uF8FF 结束的范围:

You can use startAt and endAt to return a range of child nodes, i.e. all the ones starting with Good. To do this you create a range starting at Good and ending at Good\uF8FF:

ref('/restaurants').orderByChild('name').startAt('Good').endAt('Good\uF7FF')

由于 \uF8FF 是最后一个已知的 Unicode 字符,此查询返回以例如开头的项目.GoodGoodieGoodness,但不是 Gooey(因为 GooeGooe 之后code>Good\uF7FF).

Since \uF8FF is the last known Unicode character, this query returns items starting with e.g. Good, Goodie, Goodness, but not Gooey (since Gooe is after Good\uF7FF).

这篇关于使用 firebase 实时数据库创建搜索功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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