Android - Firebase query startAt does not work as expected

I am developing an Android application using Firebase as a backend. I am new to Firebase and am stuck in the problem, but the problem is that when I try to search for a record using the startAt query startAt it returns results that do not start with the keyword that I entered.

Here is the dataset

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

And here is the snippit code

 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 look for abc , the answer also includes test . Maybe I'm doing something wrong, or I'm wrong. Can someone point me in the right direction.

PS I'm trying to use AutocompleteTextView to search for items.

thanks

+6
source share
4 answers

When you call orderByChild("name").startAt("abc") , the database orders all the elements by name property, skips them to abc and then returns them all.

If you want to return only those children that match abc , you should use equalTo() :

 query = firebase.child(Constants.KEY_ITEM_NAME).orderByChild("name").equalTo("abc"); 
+5
source

Unfortunately, you cannot do what you want. I assume that the method you are looking for is startsWith() , not startAt() (to match all words starting with a substring). This method has not yet been implemented in Firebase, too bad.

You should use an external service like ElasticSearch, but that means you should have a server, which is probably one of the reasons why you are actually using firebase ... Anyway, this is a link to implement ElasticSearch:

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

Otherwise, you will have to figure out how to get rid of the search bar ...

Good luck

0
source

Worked for me:

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

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

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

All Articles