在没有服务器端代码的情况下在 Firebase 中搜索 [英] Searching in Firebase without server side code

查看:23
本文介绍了在没有服务器端代码的情况下在 Firebase 中搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 Firebase 获取名称包含给定字符串的所有用户.例如,如果我有这些用户:

I am trying to get all the users having the name that contains a given string from Firebase. For example, if I have these users:

Devid, Andy, Bob

我希望所有用户的名称都包含D",因此我希望结果如下:

I would like to get all the users having the name that contains a 'D' so I expect this as result:

Devid, Andy

这是我目前的 Firebase 结构:

This is my Firebase's structure at the moment:

由于 Firebase 区分大小写,我创建了一个包含小写名称的属性 name_.

Since Firebase is case sensitive I've created an attribute name_ that contains the lowercase name.

使用 startAt 和 endAt 我可以获得名称以定义的字符串开头的所有用户

Using startAt and endAt I can get all the users with the name starting with a defined string

ref.orderByChild("name_").startAt(text).endAt(text+"uf8ff").on('value', ...);

但这只会给我以给定字符串开头的名称的用户,例如,如果文本是D",我会得到:

But this gives me only the users having the name that starts with a given string, for example if text is 'D' I'll get:

Devid

1) 目前我的查询意味着给我所有具有以给定字符串开头的 name_ 的用户";有没有办法让它的意思是给我所有名称包含给定字符串的用户"?

1) At the moment my query means, "give me all the users having name_ that starts with a given string" is there a way to make it mean "give me all the users which name contains a given string"? NO

Firebase 查询与全文搜索没有任何相似之处运营商.要完成这些,您要么必须集成一个外部全文搜索引擎,还是搞出一个很精心的自定义索引方案.Firebase 和索引/搜索

Firebase Queries don't have anything similar to full-text search operators. To accomplish those, you'll either have to integrate an external full-text search engine, or come up with a very elaborate custom indexing scheme. Firebase and indexing/search

2) 目前我不想拥有服务器端代码,有什么好的和有效的方式来实现自定义索引?

2) At the moment I don't want to have server side code, what can be a good and efficient way to implement custom indexes?

谢谢

推荐答案

好的 - 没有办法完全按照您当前的结构做您想做的事情.

Ok - there's no way to do exactly what you want with your current structure.

然而这只是我的脑海中浮现:

However this just popped into my head:

users:
  user_1234
    first_name: "Devid"
    components:
       "D": true
       "e": true
       "v": true
       "i": true
       "d": true
  user_5678
    first_name: "Andy"
    components:
       "A": true
       "n": true
       "d": true
       "y": true
  user_1010
    first_name: "Bob"
    components:
       "B": true
       "o": true
       "b": true

这里有一些 ObjC 代码来实现它(并且已经过测试!)

and here's some ObjC Code to make it happen (and it's tested!)

Firebase *ref = [myRootRef childByAppendingPath:@"users"];

FQuery *q1 = [ref queryOrderedByChild:@"components/b"];
FQuery *q2 = [q1 queryEqualToValue:@1];

[q2 observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot *snapshot) {

    NSLog(@"%@", snapshot.value);

}];

此代码返回 Bob.

要获取所有 'd' 人,请将components/b"更改为components/d"

To get all of the 'd' people, change the "components/b" to "components/d"

您可以变得非常疯狂并添加更​​多组合以扩展您的搜索能力

You can get really crazy and add more combinations to expand your search capability

users:
  user_1234
    first_name: "Devid"
    components:
       "D": true
       "e": true
       "v": true
       "i": true
       "d": true
       "De": true
       "Dev": true
       "Devi": true
       "Devid": true
       "ev": true
       "evi": true
       "evid": true
       ... etc

编写几行代码来迭代名称并写出组合非常简单.

It would pretty simple to code up a few lines of code to iterate over the name and write out the combinations.

显然,将所有名字读入快照,将它们转储到数组中并(在 ObjC 中)使用 NSPredicate 来提取您需要的内容会更有效(如果您的数据集有限).

Obviously it would be way more efficient (if you have a limited data set) to just read all of the first names into snapshot, dump them into an array and (in ObjC) use an NSPredicate to pull out what you need.

这篇关于在没有服务器端代码的情况下在 Firebase 中搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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