Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use firebase realtime database create search function

I would like to create a search feature for my apps by using firebase real time database, which similar to facebook search.

I did some research and acknowledge that firebase real time is not able to search with "text contain". But i believe there must be some method to achieve it, like combine startAt or endAt.

I need some advise the best practice to do it.

The DB structure will look like:

Restaurant
-->RandomID
---->name: 'Good Restaurant'
---->address:
---->desc:
-->RandomID
---->name: 'Dixon Lee Restaurant'
---->address:
---->desc:
-->RandomID
---->name: 'MCD'
---->address:
---->desc:

Edited I not sure how the startAt and endAt work. Is it supposes to return 3 and above if i use startAt(3)? What if i use startAt with string? Like below problem i ecnounter:

firebase.database().ref('/restaurants').orderByChild('name').startAt('Good').on('value',snap=>{
      console.log(snap.val())
    })

The above query will return: enter image description here

I am thinking to achieve "text contains" with startAt/endAt, which will find specific value of field that begin/end with a keyword. Not sure is this the usage for startAt/endAt.

like image 720
Jerry Avatar asked Aug 21 '26 07:08

Jerry


1 Answers

You seem to be confusing startAt with a function that would accepts strings that start with a certain value. That's simply not how Firebase filtering work.

When you do ref('/restaurants').orderByChild('name').startAt('Good'), it does three things:

  1. Orders all child nodes of restaurants on their name value.
  2. Find the first one starting with Good (or anything lexicographically after Good)
  3. Returns every child from there on

So you're getting the child nodes with name Good Restaurant and MCD.

You can use startAt and endAt to return a range of child nodes, i.e. all the ones starting with Good. To do this you create a range starting at Good and ending at Good\uF8FF:

ref('/restaurants').orderByChild('name').startAt('Good').endAt('Good\uF7FF')

Since \uF8FF is the last known Unicode character, this query returns items starting with e.g. Good, Goodie, Goodness, but not Gooey (since Gooe is after Good\uF7FF).

like image 57
Frank van Puffelen Avatar answered Aug 24 '26 19:08

Frank van Puffelen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!