0

I'm trying to create a keyword search functionality in Swift and Firebase.

my main records are stored in a structure like so...

ads
    adUID
        name: "beast of a truck"
        category: "vehicles"
        keyword: "ford"

    adUID
        name: "lil red go car"
        category: "vehicles"
        keyword: "ford"

I've created a lookup/reference "table" with a structure as such...

adKeywords
    adUID
        0:  "black"
        1:  "ford"
        2:  "F150"
        3:  "4x4"
    adUID
        0:  "red"
        1:  "ford"
        2:  "Escape"
        3:  "AWD"

I've checked out Deep path query using wildcard as a path

and Search for sub child in firebase database

but they both seem to apply to search 1 field that will always have the same name.

In my case I want to search any of the fields inside of adKeywords.

I've tried doing the following omitting queryOrdered

let ref = Database.database().reference()
let keywordQuery = ref.child("adKeywords").queryEqual(toValue: searchTerm)
keywordQuery.observeSingleEvent(of: .value, with: { snapshot in 
    print(snapshot)
})

if I just put a field in the original ad record called keyword and have one word in it I'm able to search for it, but I want to be able to associate an ad with multiple keywords

let ref = Database.database().reference()
let keywordQuery = ref.child("ads").queryOrdered(byChild: "keyword").queryEqual(toValue: searchTerm)
keywordQuery.observeSingleEvent(of: .value, with: { snapshot in 
    print(snapshot)
})

with my lookup table I cannot figure out how to search the fields because they are named with numbers, and I cannot have the named the same thing.

Is there a better way of structuring this where I am able to associate multiple keywords with a record?

Ron Myschuk
  • 6,011
  • 2
  • 20
  • 32
  • There are a few problems with your structure. Instead of reiterating, I'll refer to this previous question from a developer with the same use-case: https://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value. Even though that question is in JavaScript, the solution will be basically the same for Swift. – Frank van Puffelen Jun 11 '18 at 21:14
  • thanks @FrankvanPuffelen ill check that out. – Ron Myschuk Jun 11 '18 at 21:17

0 Answers0