2

I'm trying to filter the data from my database using this code:

fdb.orderByChild("title").startAt(searchquery).endAt(searchquery+"\uf8ff").addValueEventListener(valuelistener2);

My database is like this:

  "g12" : {
    "Books" : {
      "-Mi_He4vHXOuKHNL7yeU" : {
        "title" : "Technical Sciences P1"
      },
      "-Mi_He50tUPTN9XDiVow" : {
        "title" : "Life Sciences"
      },
      "-Mi_He51dhQfl3RAjysQ" : {
        "title" : "Technical Sciences P2"
      }}

While the code works, it only returns the first value that matches the query and doesn't fetch the rest of the data even though it matches.

If I put a "T" as my search query, I just get the first title "Technical Sciences P1 " and don't get the other one with P2

(Sorry for the vague and common question title, it's just I've been looking for a solution for so long)

  • Your code matches all nodes whose `title` starts with the value of `searchquery`. If you don't see the results you'd expect, edit your question to include: 1) the `valuelistener2 ` that you use to process the results 2) the value of `searchquery` that we can use to reproduce the problem 3) the JSON at `fdb` that you'd expect to get back (as text, no screenshots please). You can get this by clicking the "Export JSON" link in the overflow menu (⠇) on your [Firebase Database console](https://console.firebase.google.com/project/_/database/data). – Frank van Puffelen Sep 02 '21 at 21:00

1 Answers1

2

While the codes works, it only returns the first value that matches the query

That's the expected behavior since Firebase Realtime Database does not support native indexing or search for text fields in database properties.

When you are using the following query:

fdb.orderByChild("title").startAt(searchquery).endAt(searchquery+"\uf8ff")

It means that you are trying to get all elements that start with searchquery. For example, if you have a title called "Don Quixote" and you search for "Don", your query will return the correct results. However, searching for "Quix" will yield no results.

You might consider downloading the entire node to search for fields client-side but this solution isn't practical at all. To enable full-text search of your Firebase Realtime Database data, I recommend you to 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
  • Thank you Mr Alex, I edited my question...I hope it's now clear – Refine Tshingila Sep 03 '21 at 09:40
  • How is `fdb` defined? And what is the value of `searchquery` that you are using when returning only the first element? – Alex Mamo Sep 03 '21 at 09:52
  • sorry I'm not quite broad in java as I'm using sketchware and for test purposes the ```searchquery``` value I entered was "Tech" and "T". I used this tutorial: https://www.sketchwarehelp.com/2020/06/firebase-query-data-with-value-of-key.html?m=1 – Refine Tshingila Sep 03 '21 at 10:39
  • How is fdb defined? – Alex Mamo Sep 03 '21 at 10:50
  • Sorry I forgot about the question and yes I sort of kinda solved it. I recreated the activity from scratch and it worked this time... I'm not sure what the problem was though, prolly careless coding but thanks for checking in on me Alex❤️ – Refine Tshingila Sep 04 '21 at 14:54