Wildcard query on Firebase?

11,467

Solution 1

I know it's been a while but I thought that others might be interested. You can "fake" a wildcard search for things like foo* (so basically you can search for values beginning with a specified string).

For iOS & Swift it would look like this:

dbReference.child("person").queryOrdered(byChild: "name").queryStarting(atValue: "foo").queryEnding(atValue: "foo\u{f8ff}").observe(.childAdded) { (snapshot: FIRDataSnapshot) in
    print("\(snapshot.key) - \(String(describing: snapshot.value))")
}

What this does is using a start and end values for name property where the end key is equal to the start + a very high code point in the Unicode range. Because it is after most regular characters in Unicode, the query matches all values that start with foo.

Solution 2

No. But kinda.

You cannot do a wildcard query, however, you can structure your data that will allow for this.

For example, say we want to find matches for users whose name starts with Ler

Here's our structure

users
  uid_0
    name: "Leroy"

Store the decomposed data in another node: Remember, disk space is cheap.

decomposed
  uid_0
    L: true
    Le: true
    Ler: true
    Lero: true
    Leroy: true

then perform a query on the decomposed node for the value of true for children equal to Ler

ref.queryOrderedByChild("Ler").queryEqualToValue(true).observeEventType(.ChildAdded, 
     withBlock: { snapshot in
                    print(snapshot.key)
                })

And the snapshot.key will be uid_0

Share:
11,467
MoreScratch
Author by

MoreScratch

I know just enough to be dangerous. I break more things than I fix.

Updated on June 04, 2022

Comments

  • MoreScratch
    MoreScratch about 2 years

    Is it possible to do wildcard queries on Firebase? For example:

    https://foo.firebaseio.com/person.json?orderBy="name"&equalTo="Lun*"
    
  • MoreScratch
    MoreScratch over 8 years
    That's a interesting way to achieve what I need. Only issue is that I need to do a wildcard for any part of a name (e.g. roy). Also, I am going to have ~200K people in my db. Will it scale?
  • Jay
    Jay over 8 years
    It can scale, (Firebase is scalable) however, it's probably not realistic for that size a dataset. If you were just looking for something like an autofill, where you type the first few chars of say, a last name, then it's more realistic. We crafted a ObjC class that we can throw a name at and it will decompose the name into all searchable combinations and then it stores that in a Firebase node; with that it feels pretty automated, but we haven't tried working with thousands of names. It's probably a good question for the Firebase team.
  • Oswaldo
    Oswaldo almost 8 years
    Imagine you want to do a wildcard search in all string fields of your database, it's impracticable to decompose each value of each field in N combinations
  • Jay
    Jay almost 8 years
    @Oswaldo As I posted in my prior comment, it's applicable in some situations where, for example you have one field you want to query out of 100 users. Decomposing the string 'feels' like a lot of data but if it's a short string, it's only a couple hundred bytes times 100 or even 1000 users. That's a tiny amount of storage but it is highly situational. And thanks for the down vote even through I made it clear it's not for large datasets. We use it all the time and it works VERY well if used properly.
  • Oswaldo
    Oswaldo almost 8 years
    @Jay, I downvoted because the quetion was more general use case and not specific like yout answer. However I found this question very usefull for me and the community and would like to have a more satisfactory answer, dont deserves 2 upvotes for a very specific use case answer
  • mamu
    mamu over 7 years
    how is that an answer for your question?
  • MoreScratch
    MoreScratch over 7 years
    I needed to close the loop on the question. It was clear to me that a solution does not exist within Firebase so we decided to simply change our BaaS provider. In essence there is no answer to be had that we can see based on the documentation and the responses to the question.
  • Learn2Code
    Learn2Code almost 7 years
    You offer no explanation of how it solved your problem, or did it really answer your question.
  • MoreScratch
    MoreScratch almost 7 years
    It solved my problem because I decided to go with another solution that supported the functionality that I required. I thought that was clear. In any case I un-marked it as the answer because it has proven to be confusing for some people.
  • cjbarth
    cjbarth over 3 years
    This is a great solution. If you want to cover emoji and other characters too, you can use the largest permissible Unicode character: \u{10ffff}.
  • Hesam
    Hesam almost 3 years
    For Android something like this: firestore?.collection("YOUR_COLLECTION") ?.orderBy("YOUR_KEY", Query.Direction.ASCENDING) ?.startAt(tag) ?.endAt("$tag\\uf8ff") ?.limit(10) ?.get() ?.addOnSuccessListener { ... }