2

In firebase, how can one go about running a query to find matches which have a specific word in the string?

for example querying all descriptions that have the word happy in them.

I am able to do this with JS but that means I have to load the entire DB which is over 300 items...

Cheers!

hugger
  • 426
  • 4
  • 19
  • Server-side queries can filter for strings that *start with* a certain substring, but can't search for strings that *contain* a certain substring. See https://stackoverflow.com/questions/28589092/firebase-query-find-item-with-child-that-contains-string – Frank van Puffelen Dec 20 '18 at 15:47
  • 1
    Darn, this is a shame... So my only option would be downloading the whole node then query with JS? Anything you may recommend? – hugger Dec 20 '18 at 16:12
  • 1
    If you want full-text search, I'd set up a full-text search solution (ElasticSearch, Algolia, etc). Firebase Realtime Database is more focused on realtime data synchronization. – Frank van Puffelen Dec 20 '18 at 16:23

1 Answers1

2

You don't have to download the entire DB for this (i assume you'r using realtime database over firestore).

You can filter your data mixing orderByChild() or orderByKey() or orderByValue() with query methods like startAt(), endAt() and equalTo().

For example, if your list of nodes url is https://mydb-xxxx.firebaseio.com/parentnode/childnodeslist

You can query in this way:

// Find all nodes whose property myString is "hello"

var ref = firebase.database().ref("parentnode/childnodeslist");
ref.orderByChild("myString").equalTo('hello').once(‘value’).then(function(element){
  console.log(element);
});

see the query doc and how to structure your data in firebase

EDIT: BASED ON COMMENTS:

If you want to make a full text search you can:

1) Make it client side

// assume "myString" is your param name

var word = 'hello';
var listOfItems = [];
firebase.database().ref("parentnode/childnodeslist").then(function(list){
  listOfItems = list.map(function(item){
    if( item.myString.indexOf(word) >= 0){
      return {id: item.id, word: item.myString}
    }
  })
})

2) Use a third party tools like ElasticSearch (as suggested by @Frank-van-Puffelen) or Algolia

3) Use Cloud Functions

Emanuele
  • 795
  • 4
  • 13
  • Hi emanuele. I'm aware of this type of querying, my problem is that 'hello' could be the third/fourth/tenth word in the string. I need to query that specific word and return all paragraphs that contain that word. – hugger Dec 20 '18 at 16:10
  • Is not a problem how deeper is your final node – Emanuele Dec 20 '18 at 16:49
  • It is only 3 deep – hugger Dec 20 '18 at 17:14
  • You can use `ref('one/two/three')` then query the param name `orderByChild('param')` with the string you want `equalTo('string')` – Emanuele Dec 20 '18 at 21:54
  • I dont think you're understanding my question clearly. I am not looking for a one worded string, I am trying to return all paragraphs with a specific string in it which could be a word in the middle of that paragraph... does that make sense? so equalTo('bob') would not make sense if i was trying to return a paragraph like, "hi there my name is **bob** and i like pizza". I need to return all pargraphs in that node with the word bob in it, not single worded strings that === bob. – hugger Dec 20 '18 at 22:38
  • I've add more examples. Hope that helps – Emanuele Dec 21 '18 at 12:38
  • Thanks emanuele! So **#1** is what I was initially doing, this requires the whole node to be downloaded if i'm not mistaken which is not a good idea in my case because rather than a search input its buttons the user presses with a single string value (could be quick and lots of downloads). option **#2** elastic search seems too heavy for my needs (if it was ecommerce type of application with the text search I would be more open to it. I'd like to hear more about your **#3** if you dont mind. I am using cloud functions for a couple things but dont see how this would be possible in cloud funcs – hugger Dec 22 '18 at 17:00
  • 2
    @hugger in cloud function you can create a REST API so you can get a string client side and search whatever you want in your database then return a list of results – Emanuele Dec 24 '18 at 15:18
  • this would be bad for a large section of nodes, ie usernames ```firebase.database().ref("parentnode/childnodeslist").``` better to filter before downloading – SuperUberDuper Aug 31 '20 at 17:10