Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a simple search in string in Firebase database?

People also ask

How do you filter data in Firebase realtime database?

We can filter data in one of three ways: by child key, by key, or by value. A query starts with one of these parameters, and then must be combined with one or more of the following parameters: startAt , endAt , limitToFirst , limitToLast , or equalTo .

Can you search Firebase?

Ways to Filter and Search data in Firebase Realtime databaseThrough the use of id , which is typically used to look for a specific value that has its id . Through the use of a child path from which any changes to data in the database are listed. Through the use of a variable that can be given as a parameter.

What is uf8ff Firebase?

The character \uf8ff used in the query is a very high code point in the Unicode range (it is a Private Usage Area [PUA] code). Because it is after most regular characters in Unicode, the query matches all values that start with queryText .


In my case I was able to partly achieve a SQL LIKE in the following way:

databaseReference.orderByChild('_searchLastName')
                 .startAt(queryText)
                 .endAt(queryText+"\uf8ff")

The character \uf8ff used in the query is a very high code point in the Unicode range (it is a Private Usage Area [PUA] code). Because it is after most regular characters in Unicode, the query matches all values that start with queryText.

In this way, searching by "Fre" I could get the records having "Fred, Freddy, Frey" as value in _searchLastName property from the database.


Create two String variables

    searchInputToLower = inputText.getText().toString().toLowerCase();

    searchInputTOUpper = inputText.getText().toString().toUpperCase();

Then in the Query set it to:

    DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Products");//Your firebase node you want to search inside..

    FirebaseRecyclerOptions<Products> options =
            new FirebaseRecyclerOptions.Builder<Products>()//the Products is a class that get and set Strings from Firebase Database.
            .setQuery(reference.orderByChild("name").startAt(searchInputUpper).endAt(searchInputLower + "\uf8ff"),Products.class)
            .build();

the "name" it's the node inside the Products Main Node.

the .startAt(searchInputUpper) & .endAt(searchInputLower + "\uf8ff") to make the search as contains all characters that typed in the inputText.getText() that you get.


Firebase is noSQL therefore it does not have searches built in like you'll find in SQL. You can either sort by values/key or you can equalto

https://firebase.google.com/docs/database/android/retrieve-data

You can find examples at the link above. That is the latest documentation for firebase.

If you are looking for SQL like searches. Then take a look at elastic search. But that will increase the complexity since you need a platform to put it on. For that i could recommend Heroku or maybe GoogleCloudServers

Here is a blog post about advanced searches with elastic search https://firebase.googleblog.com/2014/01/queries-part-2-advanced-searches-with.html


This question might be old but there is a documented way of how to achieve this way, It is simple to implement. Quoted:

To enable full text search of your Cloud Firestore data, use a third-party search service like Algolia. Consider a note-taking app where each note is a document:

Algolia will be part of your firebase functions and will do all the searches you want.

// Update the search index every time a blog post is written.
exports.onNoteCreated = functions.firestore.document('notes/{noteId}').onCreate(event => {
      // Get the note document
      const note = event.data.data();

      // Add an 'objectID' field which Algolia requires
      note.objectID = event.params.noteId;

      // Write to the algolia index
      const index = client.initIndex(ALGOLIA_INDEX_NAME);
      return index.saveObject(note);
});

To implement the search, the best way is to use instant search - android


Sample Search Image: GIF


I my case used:

var query = 'text'
databaseReference.orderByChild('search_name')
             .startAt(`%${query}%`)
             .endAt(query+"\uf8ff")
             .once("value")

In this way, searching by "test" I could get the records having "Test 1, Contest, One test" as value in 'search' property from the database.


The feature you're looking for is called full-text search and this is something most databases (including Firebase) don't provide out-of-the-box because it requires storing the data in a format that's optimized for text search (vs optimized for filtering) - these are two different problem sets with a different set of trade-offs.

So you would have to use a separate full-text search engine in conjunction with Firebase to be able to do this, especially if you need features like faceting, typo tolerance, merchandizing, etc.

You have a few options for a full-text search engine:

  • There's Algolia which is easy to get up and running but can get expensive quickly
  • There's ElasticSearch which has a steep learning curve but uber flexible
  • There's Typesense which aims to be an open source alternative to Algolia.