16

I am saving data as follows in the Firebase:

structure

I want to find all records that have #Yahoo in their title. What will be the exact query for that?

I am confused with the random key created. I am not sure how to handle this so i am posting it here.

Firebase firebase = new Firebase(Constants.FIREBASE_URL_USER_TASKS).child(Utils.encodeEmail(unProcessedEmail));
        Query queryRef = firebase.orderByKey().startAt("#" + mHashTag).endAt("#" + mHashTag + "\uf8ff");

        queryRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                mTasksList.clear();
                mAdapter.notifyDataSetChanged();
                for (DataSnapshot task : dataSnapshot.getChildren()) {
                    mTasksList.add(task.getValue(TaskModel.class));
                }
                mAdapter.notifyItemRangeInserted(0, mTasksList.size());
                mSwipeToRefresh.post(new Runnable() {
                    @Override
                    public void run() {
                        mSwipeToRefresh.setRefreshing(false);
                    }
                });
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {
                mSwipeToRefresh.post(new Runnable() {
                    @Override
                    public void run() {
                        mSwipeToRefresh.setRefreshing(false);
                    }
                });
            }
        });
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Shajeel Afzal
  • 5,913
  • 6
  • 43
  • 70

7 Answers7

19

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
    }
  }
}
Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Can i achieve that after changing the structure of the database? – Shajeel Afzal Dec 30 '15 at 21:49
  • I am also maintaining the task IDs in the HashTag tree, as the list. But is it possible to get only the tasks with their IDs? – Shajeel Afzal Dec 30 '15 at 21:50
  • 1
    The code above will filter the tasks by their title. – Frank van Puffelen Dec 30 '15 at 21:55
  • 1
    Thanks for that but it will give items whose title begins with #Yahoo. That is not required, #Tag can be anywhere. – Shajeel Afzal Dec 30 '15 at 21:58
  • 1
    As said in my answer: Firebase cannot do a query for strings that *contain* a specific value. If that is what you are looking for, I'll find a duplicate. – Frank van Puffelen Dec 30 '15 at 22:00
  • 1
    As i said i am also maintaining the task IDs in the HashTag tree as the list. For example in this image http://imgur.com/y6q2QRR i am showing the structure of the Hash Tag. You can see the taskKeys node, I will store all the Task keys in it. Can i get all the task details in one query? Or should i loop through to get them? – Shajeel Afzal Dec 30 '15 at 22:01
  • Could you explain further (the docs at firebase.google.com about Indexing weren't too clear) what this does and if this is mandatory? – Ali Bdeir Dec 01 '16 at 19:30
  • @FrankvanPuffelen are you able to make it non-case sensitive? – edwoollard Aug 24 '17 at 07:11
  • https://stackoverflow.com/questions/38590937/firebase-query-methods-startat-taking-case-sensitive-parameters – Frank van Puffelen Aug 24 '17 at 07:44
6

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)
    {

    }
};
Son Nguyen Thanh
  • 1,199
  • 15
  • 19
4
    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) {}
});
Nabeel K
  • 5,938
  • 11
  • 38
  • 68
Anupam
  • 41
  • 5
1

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.

Jules Dupont
  • 7,259
  • 7
  • 39
  • 39
0

Try the following code

DatabaseReference mDatabaseRef =FirebaseDatabase.getInstance().getReference("userTasks");

  Query query=mDatabaseRef.orderByChild("title").equalTo("#Yahoo");

    query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            for (DataSnapshot data:dataSnapshot.getChildren()){


                Records models=data.getValue(Records.class);
                String latitude=models.getLatitude();
                String longitude=models.getLongitude();

            }

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

I have declared only two strings here, latitude and longitude. If you want to load more values you need to add that also.

Then create a new class Records as follows

public class Records {

public String latitude,longitude;

public Records()
{

}

public String getLatitude() {
    return latitude;
}

public void setLatitude(String latitude) {
    this.latitude = latitude;
}

public String getLongitude() {
    return longitude;
}

public void setLongitude(String longitude) {
    this.longitude = longitude;
} }
Mohammed Javad
  • 163
  • 1
  • 11
0

I know this is late but this solution worked for me.

 getContentResolver().delete(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                        MediaStore.Video.VideoColumns.DATA + "=?" , new String[]{ paths.get(i) });
makkhokher
  • 228
  • 2
  • 15
0
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference animalsRef = rootRef.child("userTasks");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        Boolean found;
        String search = "Yahoo";
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String movieName = ds.child("movieName").getValue(String.class);
            found = movieName.contains(search);
            Log.d("TAG", movieName + " / " + found);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
animalsRef.addListenerForSingleValueEvent(eventListener);
Tarik Billa
  • 137
  • 1
  • 4