How to search for a value in firebase Android
Solution 1:
You cannot search for any item whose title contains #Yahoo
. See:
How to perform sql "LIKE" operation on firebase?
You can however search items whose title begins with #Yahoo
:
Firebase firebase = new Firebase(Constants.FIREBASE_URL_USER_TASKS).child(Utils.encodeEmail(unProcessedEmail));
Query queryRef = firebase.orderByChild("title").startAt("#" + mHashTag)
To make this work well, you have to add an index to your Firebase rules:
"rules": {
"userTasks": {
"$email": {
".indexOn": "title" // index tasks in their title property
}
}
}
Solution 2:
This is working with the Google Firebase.
DatabaseReference mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference();
Query query = mFirebaseDatabaseReference.child("userTasks").orderByChild("title").equalTo("#Yahoo");
query.addValueEventListener(valueEventListener);
ValueEventListener valueEventListener = new ValueEventListener()
{
@Override
public void onDataChange(DataSnapshot dataSnapshot)
{
for (DataSnapshot postSnapshot : dataSnapshot.getChildren())
{
//TODO get the data here
}
}
@Override
public void onCancelled(DatabaseError databaseError)
{
}
};
Solution 3:
DatabaseReference mDatabase =FirebaseDatabase.getInstance().getReference("userTasks");
mDatabase.orderByChild("title").equalTo("#Yahoo").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
///Here you can get all data
}
@Override
public void onCancelled(DatabaseError databaseError) {}
});
Solution 4:
Answer : simply do following query :
databaseReference.orderByChild('_searchLastName')
.startAt(queryText)
.endAt(queryText+"\uf8ff");
We can combine startAt and endAt to limit both ends of our query. The following example finds all dinosaurs whose name starts with the letter b
:
curl 'https://dinosaur-facts.firebaseio.com/dinosaurs.json?orderBy="$key"&startAt="b"&endAt="b\uf8ff"&print=pretty'
The \uf8ff
character used in the query above is 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 a b
.
Here's a related question and the official docs.