2017-10-20 250 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計劃進行開發,因此不支持。 –