Firebase:如果一个值包含数据库中的特定文本 [英] Firebase: If a value includes a specific text in database

查看:34
本文介绍了Firebase:如果一个值包含数据库中的特定文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过滤包含特定文本的项目的列表.

I try to filter the list for items that contain a certain text.

数据库:

{"123":{"cities":"sss"},"445":{"cities":"hello hi dd"}}

并且我想获取 cities 值中包含hi"的项目:

And I want to get the items that contains "hi" within the cities value:

{"445":{"cities":"hello hi dd"}}

我试过这个:

https://xxxxx.firebaseio.com/allitems.json?orderBy="cities"&startAt="hi"

但它不起作用...它只显示以hi"开头的项目...

But it's not working... It only shows the items that starts with "hi"...

谢谢.

推荐答案

在撰写本文时,Firebase 实时数据库中没有内置全文搜索功能.

At the time of writing, there is no built-in full-text search capability in the Firebase Realtime Database.

官方 Cloud Functions 示例包括一个示例使用 Algolia 托管搜索服务进行全文搜索.

The official Cloud Functions samples include an example of full-text search with the Algolia hosted search service.

请注意,如果您搜索以某些特定字符开头的字符串(与包含某些特定字符的字符串不同),则有一种解决方法.

Note that there is a workaround if you search for a string that starts with some specific characters (which is different than a string which contains some specific characters).

查看 REST API 的范围查询"部分 文档:

Have a look at the "Range Queries" section of the REST API documentation:

我们可以结合 startAtendAt 来限制我们查询的两端.这以下示例查找名称以字母b":

We can combine startAt and endAt to limit both ends of our query. The following example finds all dinosaurs whose name starts with the letter "b":

卷曲'https://dinosaur-facts.firebaseio.com/dinosaurs.json?orderBy="$key"&startAt="b"&endAt="b\uf8ff"&print=pretty'

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

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 a b.

因此,在您的情况下,您将执行以下操作:

So in your case you would do as follows:

https://xxxxx.firebaseio.com/allitems.json?orderBy="cities"&startAt="hi"&endAt="hi\uf8ff"

这篇关于Firebase:如果一个值包含数据库中的特定文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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