2017-10-20 249 views
0

有人能够解释如何查询特殊字符的Firebase吗?Firebase Swift查询符号

我已经得到了一些数据,像这样 -

posts 
    post_1 
    description: "This is a post! #thisdoesntwork" 
    post_2 
    description: "Heres another post! #neitherdoesthis" 

如果我在SWIFT运行一个查询 - 获取返回

let db = Database.database().reference() 

db.child("posts").queryOrdered(byChild: "description").queryStarting(atValue: "[a-zA-Z0-9]*").queryEnding(atValue: "#thisdoesntwork").observeSingleEvent(of: .value) { (snapshot) in 
    // No results! 
} 

没有。但是它的工作原理,当我忽略像这样的主题标签 -

db.child("posts").queryOrdered(byChild: "description").queryStarting(atValue: "[a-zA-Z0-9]*").queryEnding(atValue: "thisdoesntwork").observeSingleEvent(of: .value) { (snapshot) in 
    // One post gets returned here 
} 

这是因为散列是一个特殊的角色,我需要以某种方式逃避?还是我以错误的方式查询它?

在此先感谢。

+0

可能重复的[添加Firebase数据,点和正斜杠](https://stackoverflow.com/questions/19132867/adding-firebase-data-dots-and-forward-slashes) – DoesData

+0

@DoesData这个答案是肯定的不是重复的,并且具有不相关的信息。 OP询问子字符串类型搜索。 – Jay

回答

1

你认为发生的事情不是。让我来解释一下,并提供一个例子:

看来你正试图执行一个字符串搜索。也许甚至是一个子字符串搜索。

Firebase不提供子字符串搜索功能,甚至字符串搜索也不像字符串搜索那样,因为它会像swift这样的语言。

所以对于初学者来说,这不是有效的

queryStarting(atValue: "[a-zA-Z0-9]*") 

,将字面上搜索以字符串或字符等于[A-ZA-Z0-9] *启动一个节点。因此,如果您的节点碰巧看起来如下所示:

posts 
    post_x 
    description: "[a-zA-Z0-9]* This node would be returned" 

这将是一个匹配项。

.startWith: a query that starts with the given string 
.endWith: a query ending with a string that starts with the given string 
     (not the ending part of a string or a substring) 

让我提供基于你的结构

posts 
    post_1 
    description: "This is a post! #thisdoesntwork" 
    post_2 
    description: "Heres another post! #neitherdoesthis" 
    post_2 
    description: "a" 
    post_3 
    description: "n" 

和示例查询

let postsRef = ref.child("posts") 
    let queryRef = postsRef.queryOrdered(byChild: "description") 
          .queryStarting(atValue: "This") 
          .queryEnding(atValue: "z") 
    queryRef.observeSingleEvent(of: .value) { snapshot in 
     print(snapshot) 
    } 

这个查询将返回岗位1,3的例子结构,以及4.为什么呢?

post_1开始与字母大写T,它是ASCII 84.

的查询将返回具有开始84(ASCII T)的ASCII值和与122(ASCII Z)结束的所有节点。因此,帖子3是一个a,它是ascii 97和post 4,n是ascii 110.所有这些都被返回。 *对于那些接下来的人来说,查询实际上以单词'This'开始并以'z'结尾,但是这个例子简化了。

虽然一方面可能看起来有点限制,但实际上它非常强大。

一种用法是当您要查询以特定字符串开头的一系列值时。因此,假设您拥有一家农产品分销公司,并且拥有Apple,Banana,Peanut和Walnut等产品。你可以组织你的数据库这样

items 
    item_0 
    description: fruit_apple 
    item_1 
    description: fruit_banana 
    item_2 
    description: nut_peanut 
    item_3 
    description: nut_walnut 

如果你想所有的水果的列表,你可以查询像这样

let queryRef = postsRef.queryOrdered(byChild: "description") 
          .queryStarting(atValue: "fruit_") 
          .queryEnding(atValue: "fruit_") 

这被称为复合值。

就你而言,底线的答案是你不能直接搜索字符串内的特殊字符,但是,你可以搜索一系列以ASCII字符开头的字符。

查询从“!”开始在结束“/”将返回所有的字符串开头的字符:

33 ! 
34 \" 
35 # 
36 $ 
37 % 
38 & 
39 ' 
40 (
41 ) 
42 * 
43 + 
44 , 
45 - 
46 . 
47/

这超长的答案是不是一个真正的解决方案,但将可能与重组的火力地堡的帮助,所以你可以得到你要查询的数据上。

+0

谢谢Jay。这当然让我意识到我正在考虑这个错误的方式。 为了记录我最终编码了标签并将它们分成了一个不同的孩子,我可以更容易地进行查询。对于全文搜索,我认为使用像Algolia这样的东西可能是我的最佳选择,但由于我现在只使用免费的Firebase计划进行开发,因此不支持。 –