1

I want to let my users search for some contents in my Firebase real time Database. The problem is that Firebase Query only lets me search/filter results with endAt() and startAt(). For Example Let's say my User searches for "rro" , then my searchview should generate suggestions as "Arrow" (after looking in my database if it finds entry with Arrow).

I am able to put suggestions in searchview myself but the problem is .startAt() and .endAt() cannot achieve / or find anything if "rro" is provided. I want filter like .contains();

How can I achieve this in Firebase for Android?

Frank van Puffelen
  • 418,229
  • 62
  • 649
  • 645
coder3101
  • 3,175
  • 1
  • 19
  • 24
  • The Firebase Database is not a full-text search engine. If you need such capabilities, you'll have to add another component for that. Have a look at Kato's answer here for one option: https://stackoverflow.com/questions/22506531/how-to-perform-sql-like-operation-on-firebase – Frank van Puffelen Jun 14 '17 at 13:34
  • I don't want to use Firebase cloud functions because I currently have no good knowledge of node.js . Is there any other way out. A effective and fast – coder3101 Jun 14 '17 at 14:02
  • It'll require an external text search engine. Maybe somebody else knows an easier option, but it's unlikely (this question is asked regularly and the one I linked is the de-facto answer). – Frank van Puffelen Jun 14 '17 at 14:19

2 Answers2

3

I found a better answer for this situation that doens't requiere creating a special structure:

FirebaseDatabase.getInstance().getReference("users").orderByChild("LastName").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 LastName property from the database.

The original answer is by Rizwan Khan. Link: https://www.quora.com/How-would-you-search-a-Firebase-Realtime-Database-with-a-substring

-1

Make structure like this

- objects
  - UID1:Arrow
  - UID2:Rowboat
  - UID3:Bow
- searchable
  - arrow
    - UID1
  - rrow
    - UID1
  - row
    - UID1
    - UID2
  - ow
    - UID1
    - UID2
    - UID3
  - w
    - UID1
    - UID2
    - UID3
  - rowboat
    - UID2
  - owboat
    - UID2
  - wboat
    - UID2
  - boat
    - UID2
  - oat
    - UID2
  - oa
    - UID2
  - a
    - UID1
    - UID2
  - bow
    -UID3

Then its easy to search, just use a Query like

FirebaseDatabase.getInstance().getReference().child("searchable").orderByKey().startAt(input).endAt(input+"~");

When you create object like arrow you must use a .updateChildren() where you add the entries in searchable aswell. Also when arrow changes name you must update in searchable aswell, and I recommend convert all to lowercase, both your search text and the object names in searchable.

  • This is not what I want, I have a structure similar to this and have a node called searchable with keys as titles and values as url to body. This would make the searchable node filled with many keys, I don't want that.. I need optimization and as effective as it can be... The Title will be generated by end user so he/she may give a title like "The King and the Prince" then there will be so many keys for this note.. (about 17Combination3) that would be ineffective and will occupy heavy on my database.. – coder3101 Jun 14 '17 at 13:41
  • This would be a workaround true, but only if you have a few records, and as you can see in the example, you only have 3 UIDs, and yet more that 10 times the records you started with. What if you have 1000, or 100.000 UIDs? This solution is not viable. – James Ele Dec 20 '20 at 10:22