0

I have a database which looks something like this:

  "trucks" : {
    "3705ec54-8a2e-4eb1-8bb9-ab2243645ac1" : {
      "email" : "sandwiches@123.com",
      "name" : "Sandwich Truck",
      "phone" : "123 - 456 - 1234",
      "provider" : "password",
      "selfDescription" : "We serve delicious sandwiches at a moderate price.  Cards Accepted.",
      "userType" : "truck",
      "website" : "www.sandwiches.com"
    },
    "54fea8cd-2203-46bd-aaf8-9d823e85313d" : {
      "email" : "pizza@123.com",
      "name" : "Supa Pizza",
      "phone" : "619 - 222 - 4444",
      "provider" : "password",
      "selfDescription" : "We serve incredible pizza at an incredibly unfair price.",
      "userType" : "truck",
      "website" : ""
    },
    "6c542367-507c-4d01-af2c-bf93a7efaef4" : {
      "email" : "fries@123.com",
      "name" : "Pete's Fries",
      "phone" : "11111111111111111111",
      "profilePhoto" : "",
      "provider" : "password",
      "selfDescription" : "We make some of the world's most delicious fries.",
      "userType" : "truck",
      "website" : ""
    },
    "7c6c4395-aec1-443c-908d-62db517def5e" : {
      "email" : "chili@123.com",
      "name" : "Mark's Chili",
      "phone" : "1-800-CHIL-LLL",
      "profilePhoto" : "",
      "provider" : "password",
      "selfDescription" : "We serve the most delicious chili, chili your mamma's mamma is scared to try.",
      "userType" : "truck",
      "website" : ""
    }
  }

I'd like to implement a search bar for which trucks whose names match the search terms will be returned. For example, any truck name containing the string 'fries' will have its ID committed to an array of search result IDs.

Here's what I've tried so far, but no dice. When I type in fries in my searchbar and hit the search button, it does not print "6c542367-507c-4d01-af2c-bf93a7efaef4"

let usersRef = Firebase(url: "https://•••••••••.firebaseIO.com/users")

usersRef.queryOrderedByChild("name").queryEqualToValue(searchBar.text).observeEventType(.ChildAdded, withBlock:{
    snapshot in

    print(snapshot.key)
})

I'm not even sure how close I am, any help would be massively appreciated. Thanks!

Joe Sloan
  • 775
  • 2
  • 9
  • 16

1 Answers1

1

You are querying using queryEqualToValue (Query.equalTo()), which will return result only if there is an exact match.

Explanation in Detail:

In the search bar if we are entering "fries" it will look for an exact match of "fries", which is not available in our document and hence the desired value "6c542367-507c-4d01-af2c-bf93a7efaef4" is not printed.

Instead if we give the value "Pete's Fries" in the search bar then we will get the result "6c542367-507c-4d01-af2c-bf93a7efaef4"

If we give partial value for our search, then we are trying to implement a search which is similar to LIKE Query in SQL. Please refer the below posts to get more info on "how to perform LIKE query in firebase"

How to perform sql "LIKE" operation on firebase?

Community
  • 1
  • 1
Clement Amarnath
  • 5,301
  • 1
  • 21
  • 34
  • While this is a good explanation of the issue, the link rely's on Elasticsearch as a solution and I don't think that's going to happen on an iOS device as it's a node.js service [Elasticsearch on mobile devices](http://stackoverflow.com/questions/25218159/running-elasticsearch-server-on-a-mobile-device-android-iphone-ios). If anyone has more information please post! – Jay Jun 18 '16 at 14:22