Reputation: 81
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
Upvotes: 7
Views: 8853
Reputation: 77
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.
Upvotes: 0
Reputation: 29
I too was looking for this solution, thanks. I tried it, and even works with this:
startAt('ab').endAt('ab\w|\W');
Upvotes: -1
Reputation: 4897
Worked for me:
Query query = MyApplication.sReferenceOrders.orderByChild(Constants.STATUS).startAt("1").endAt("1\\uf8ff");
Upvotes: 0
Reputation: 2962
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!
Upvotes: 0
Reputation: 598740
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");
Upvotes: 5