2017-04-20 64 views
0

enter image description here如何尋找孩子的價值觀火力的Android

DatabaseReference databaseReference=mDatabase; 
     String queryText="Hotel"; 
     databaseReference.orderByChild("Coupon") 
       .startAt(queryText) 
       .endAt(queryText+"\uf8ff"); 

這裏我連着,我用得到的「優惠券」孩子的名字,當我進入下的「酒店」查詢碼優惠券。但是我得到了空白。我應該得到Hotel1,Hotel2對象。我對firebase是新的。所以希望你的支持。提前感謝。

+0

基本上,你正試圖執行一個'SELECT * FROM優惠券,其中的優惠券LIKE'Hotel%''對嗎? –

+0

是的。當我們考慮SQL是正確的。我需要使用它作爲我的搜索查詢。 – HRCJ

+1

您能否爲您的結構爲何如此添加更多細節?我似乎無法弄清楚爲什麼當您將這些數據再次存入時,您將Hotel1/Hotel2作爲您的鑰匙。 –

回答

2

在Web版本,他們使用一種叫做ElasticSearch,你可以嘗試更多的在這裏讀到:https://firebase.googleblog.com/2014/01/queries-part-2-advanced-searches-with.html

但爲Android,我認爲這是不執行這樣一個搜索的任何功能。我會做的就是查詢所有記錄然後過濾他們自己:

DatabaseReference databaseReference = mDatabase; 
mDatabase.addValueEventListener(new ValueEventListener(new ValueEventListener() { 
@Override 
public void onDataChange(DataSnapshot dataSnapshot) { 
    for(DataSnapshot val : dataSnapshot.getChildren()){ 
     //I am not sure what record are you specifically looking for 
     //This is if you are getting the Key which is the record ID for your Coupon Object 
     if(val.getKey().contains("Hotel")){ 
      //Do what you want with the record 
     } 

     //This is if your are querying for the hotel child 
     if(val.child("hotel").getValue(String.class).contains("Hotel")){ 
      //Do what you want with the record 
     } 
    } 
} 
@Override 
public void onCancelled(FirebaseError firebaseError) { 

} 

}

+0

是的,我看到有很好的web解決方案。但我無法找到android.Any方式感謝您的幫助。我檢查然後我會標記這是正確的。 – HRCJ

+1

從技術上講,它會返回所有酒店,但是如果在記錄中包含「Hotel」,那麼您將檢查每條記錄,因此,它會返回帶有「Hotel」的記錄或您添加的任何搜索查詢。 –

+0

這對我的工作來說似乎是一個很好的解決方案。但是如果它是一個大型數據集,我們會遇到麻煩。因爲我們必須在單擊搜索時加載所有數據。 – HRCJ

0

不要裝入你的整個數據庫,篩選出需要的數據。它產生不必要的流量,必須加載,計算和刪除。相反,使用:

DatabaseReference myRef = FirebaseDatabase.getDatabaseReference(); 

Query searchQuery = myRef.child("Coupon").orderByChild("hotel").equalTo("yourSearchString"); 

爲了讓這段代碼的工作,你還必須添加indexOn在規則的相應屬性(火力地堡數據庫控制檯)。

相關問題