7

I am developing an Android app using Firebase as backend. I am new to Firebase and stuck at a problem, well the problem is when I try to search for a record using startAt query, it returns results which does not starts with the keyword I enters.

Here's the data set

itemname
    -KK8vI8A5BZZp3Xo3FpA
       name: "abc"
    -KK8w3uoJdJ0hBSrq0CS
       name: "test"
    -KKAC1o9Vazyg9JLtDoQ
       name: "dude"

And here's the code snippit

Query query = firebase.child(Constants.KEY_ITEM_NAME).orderByChild("name").startAt("abc");
        query.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                Iterator<DataSnapshot> i = dataSnapshot.getChildren().iterator();
                while (i.hasNext()) {
                    DataSnapshot d = i.next();
                    LOG(d.getKey(), d.getValue().toString());
                }
            }

So when I search for abc the response also includes test. Maybe I am doing something wrong or I am going the wrong way. Could anyone please point me in right direction.

P.S I am trying to use an AutocompleteTextView to search items.

Thanks

5 Answers 5

5

When you call orderByChild("name").startAt("abc") the database orders all items by their name property, skips the ones before abc and then returns them all.

If you're only looking to return children that match abc, you'd use equalTo():

query = firebase.child(Constants.KEY_ITEM_NAME).orderByChild("name").equalTo("abc");
6
  • Thank you for your response, yes I've used equalTo() but I am trying to use this query in AutoCompleteTextView and if I've more items lets say names are abc , abd , abe and I want to search for all records that starts with ab. How can I achieve this?
    – Max
    Jun 13, 2016 at 20:29
  • 1
    I even tried using this way firebase.child("itemname").orderByKey().startAt("a").endAt("a\uf8ff"); But null response.
    – Max
    Jun 14, 2016 at 5:31
  • 10
    Worked when used .orderByChild("name").startAt("ab").endAt("ab\uf8ff");
    – Max
    Jun 14, 2016 at 5:39
  • Great to hear how you solved it. I've made a note that we need to add that example back into the firebase.google.com version of the docs. Jun 14, 2016 at 13:22
  • 3
    Yes this works, but it does not find "ab" itself then. So how can you get every value that starts with "ab" and which also matches/equals "ab"? Jun 26, 2016 at 19:51
0

Unfortunately you can't do what you want. I presume the method you're searching for is startsWith() and not startAt() (to match all words begining with a substring). This method hasn't been implemented yet in Firebase, too bad.

You have to use an external service like ElasticSearch but that would mean you must have a server which is probably one of reason you actually use firebase... Anyway this is a link for implementing ElasticSearch:

https://firebase.googleblog.com/2014/01/queries-part-2-advanced-searches-with.html

Otherwise you'll have to figure out how to get rid of your search bar...

Good luck!

0

Worked for me:

        Query query = MyApplication.sReferenceOrders.orderByChild(Constants.STATUS).startAt("1").endAt("1\\uf8ff");
0

I think you are trying to implement search feature to your app .. this is my query

 private void searchForMatch(String keyword) {
    Log.d(TAG, "SearchForMatch: searching for match");
    mUserList.clear();
    if (keyword == null) {
        mUserList.clear();
    } else {
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
        Query query = reference.child("where you want to search in database ")
                .orderByChild("what do you want search from your database").startAt(keyword).endAt(keyword + "\uf8ff");
        query.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot singlesnapshot : dataSnapshot.getChildren()) {
                    Log.d(TAG, "onDataChange: found user" + singlesnapshot.getValue(User.class).toString());
                    mUserList.add(singlesnapshot.getValue(User.class));
                   this is my list adapter for displaying search results // updateUsersList();


                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }


}

If you don't know how it works then check out this tutorial on youtube.

-1

I too was looking for this solution, thanks. I tried it, and even works with this:

startAt('ab').endAt('ab\w|\W');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.