0

enter image description hereenter image description here

In my android app, I am storing product list in this structure. But I need to check that keyword contains my searched text. I am using this query

Query query= FirebaseDatabase.getInstance().getReference(productList)
  .orderByChild("keywords").startAt("bag").endAt("bag\uf8ff");

But it is not working,Please help me in this part.Otherwise say a correct structure to save keywords in my app FirebaseDB.

dazza5000
  • 7,075
  • 9
  • 44
  • 89
MariP
  • 35
  • 1
  • 8
  • Firebase cannot efficiently query for the existence of a child in a collection/array. See my answer here: http://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value. If you have problems making that work, share the [minimal code that reproduces where you are stuck](http://stackoverflow.com/help/mcve). This will include a snippet of your actual JSON, which you can get by clicking the "Export JSON" link in your [Firebase Database console](https://console.firebase.google.com/project/_/database/data). – Frank van Puffelen Jun 02 '17 at 07:21
  • Hi Frank,I added my JSON snipp too..please help me – MariP Jun 02 '17 at 13:28
  • Thank you Frank,With your guidance and poisonmind help,its working.I achieved what i am expected...Thanks a lot. – MariP Jun 04 '17 at 05:38
  • Thanks @poisondminds for helping in my absence! – Frank van Puffelen Jun 04 '17 at 19:40

1 Answers1

1

Firebase Realtime Database doesn't work so well with "contains" like matching like you want. Rather, a better suggested way to structure this would be to give each "product" a list of keywords instead of storing it in a comma separated string.

"productList":
{
    "id1":
    {
        "keywords":
        {
            "bag": true,
            "bang": true
        },
        ...
    }
}

This sort of structure is explained further in the Firebase Realtime Database Structure Guidelines. It then makes it much better for querying. You could then do the following to find products with that keyword...

Query query = FirebaseDatabase.getInstance().getReference(productList).orderByChild("keywords/bag").startAt("");
poisondminds
  • 427
  • 4
  • 8
  • i tried with this DB structure,stored keywords in HashMap(String,Boolean).But this query returned null only. – MariP Jun 02 '17 at 06:54
  • @MariP did you rename your `keyword` node to `keywords`? Can you update the question with an export of your firebase database so I can see the exact structure ? – poisondminds Jun 02 '17 at 06:56
  • @poisondmind..yes i rename my node keywords and saved the list of string in Hashmap.but still that query return null only. – MariP Jun 02 '17 at 07:02
  • @MariP I just looked at your json. If you look at the link in my answer, it shows that you shouldn't use `false` for values within a list like structure. Instead, just don't include it. The reason the query is not coming up with anything is because you don't have any items with `bag: true` in the keyword list – poisondminds Jun 03 '17 at 02:35