0

This is my code to search in the firebase database but it only searches from the start not in between.

private void processSearch(String query) {
       Query query1 = databaseReference.child("posts").orderByChild("bookName").startAt(query).endAt(query+"\uf8ff");
       query1.addValueEventListener(new ValueEventListener() {
           @Override
           public void onDataChange(@NonNull DataSnapshot snapshot) {
               for (DataSnapshot dataSnapshot : snapshot.getChildren()){
                   AllPostItemHelper itemHelper = dataSnapshot.getValue(AllPostItemHelper.class);
                   itemHelper.setPostId(dataSnapshot.getKey());
                   allPostItemSearch.add(itemHelper);
               }
               AllPostItemAdapter allPostItemAdapter = new AllPostItemAdapter(allPostItemSearch,getContext());
               postThings.setAdapter(allPostItemAdapter);
           }

           @Override
           public void onCancelled(@NonNull DatabaseError error) {
               
           }
       });
   }

} 
Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
  • Aside from my answer, since you're using Java, I think that this [resource](https://medium.com/firebase-tips-tricks/how-to-filter-firestore-data-cheaper-705f5efec444) will help. Here is the corresponding [repo](https://github.com/alexmamo/FilterFirestoreResults). It's for Firestore, but you can simply adapt it to the Realtime Database. – Alex Mamo Feb 02 '23 at 11:08

1 Answers1

0

only search from the starting not in between

That's expected behavior since you are performing a call to Query#startAt() which:

Creates a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default.

However, if you need to search for a substring, please note that this is not possible. You can only perform queries that match a string that starts with a specific string or starts with a range of strings.

If you think to download an entire node to search for fields client-side, please note that this solution isn't practical at all. To enable a full-text search for your Firebase Realtime Database data, I recommend you use a third-party search service like Algolia or Elasticsearch.

If you consider at some point in time to try using Cloud Firestore, please see the following example:

To see how it works with Cloud Firestore but in the same way, you can use it with Firebase Realtime Database.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193