0

I've the following structure in my firebase:

enter image description here

How can I make a search filtering by title? I know that firebase does not have LIKE operator, but a search by the starting chars would be possible?

I've already searched over StackOverflow and the most common anwers seems to be something like this:

firebase.reference.child("music")
            .orderByChild("title")
            .startAt("fog")
            .endAt("fog" + "\uf8ff")
            .addListenerForSingleValueEvent(object : ValueEventListener {
                override fun onCancelled(p0: DatabaseError) {}

                override fun onDataChange(p0: DataSnapshot) {
                    Log.i("DATABASE_RETURN", p0.toString())
                }
            })

That code returns always null, here's what it's printing:

I/DATABASE_RETURN: DataSnapshot { key = music, value = null }

Tgo1014
  • 536
  • 1
  • 7
  • 17
  • Are you sure you have the correct reference? Have you tried it with hard-coded (basic) values? – André Kool Jul 03 '18 at 14:59
  • Removing "startAt()" and "endAt()" works, but it returns all values from firebase, so I guess the filter is not working but idk why – Tgo1014 Jul 03 '18 at 15:01
  • 1
    I think we are going to need more info here. Like what is your searchTerm? Does it work with, for example, a hardcoded value like "Fog"? – André Kool Jul 03 '18 at 15:21
  • It's the title the people is searching for, like "fog". It do not work neither with my variable nor with a hardcoded value. Always returns null when i'm using "startAt()" and "endAt()". – Tgo1014 Jul 03 '18 at 16:30
  • Please take a moment to reproduce the problem with only hardcoded values. Right now we don't know what `firebase`, `MUSIC_COLLECTION`, `MUSIC_FIELD_TITLE` and `searchTerm` are (plus the `"$searchTerm\uf8ff"` looks like an intended hardcoded string), and your `onDataChange` does nothing. Have a look at [how to create a minimal complete verifiable example](http://stackoverflow.com/help/mcve) for the best approach to get help getting your code to work. – Frank van Puffelen Jul 03 '18 at 16:52
  • @FrankvanPuffelen I edited the question with hardcoded values – Tgo1014 Jul 03 '18 at 16:59
  • 1
    Firebase is case sensitive. You have `.startAt("fog")`, but none of your nodes start with `fog`. Use `.startAt("Fog").endAt("Fog\uf8ff")`. See https://stackoverflow.com/questions/38590937/firebase-query-methods-startat-taking-case-sensitive-parameters, https://stackoverflow.com/questions/37643459/case-insensitive-sorting-with-firebase-orderbychild – Frank van Puffelen Jul 03 '18 at 19:24

0 Answers0