0

I am working with the Firebase Realtime Database, and my "description" subkey is indexed (the below structure has it mentioned).

{
    "check123": {
        "description": "Linked In Page",
        "tags": [
            "one"
        ],
        "url": "https://linkedin.com"
    },
    "fb": {
        "description": "My Facebook page",
        "tags": [
            "one",
            "two"
        ],
        "url": "https://facebook.com"
    },
    "twitter": {
        "description": "My Twitter page",
        "url": "https://twitter.com"
    } 
}



I wanted to search for "Facebook" in "description" key so that it returns the "fb" key's object. I am able to get it using search term "My Facebook" with below code:

get(
      query(
        ref(db, "/dbSubPathToMentionedObject"),
        orderByChild("description"),
        startAt(searchTerm),
        endAt(searchTerm+"\uf8ff")
      )
    )

where searchTerm is the variable for the current search term. But I want to search for substring from the middle. Is it possible? If so how?
Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
vinaych
  • 79
  • 1
  • 10

1 Answers1

1

But I want to search for substring from the middle. Is it possible?

No, it's not. There is no way you can perform a query in the Realtime Database that can return elements containing a particular substring. The best you can do is use the query at the end of your question.

A possible solution, that works for extremely small data sets would be to download all the content in a node and perform the search on the client.

Since the Realtime Database doesn't provide a solution for full-text search, then you should consider using third-party libraries like Elastic Search, Algolia, or Typesense.

If you consider at some point in time try using Cloud Firestore, then I recommend you reading the following resource:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193