Algolia/Firebase Nested Records Search Updated!

I’m using Algolia to run searches on my Firebase backend. Inside FBDatabase the path ‘root/meal’ is what I want to index to Algolia but I also want to index the dessertRefs which are really childByAutoIDs.

I create a node named meal and I upload the dict keys/values to it ["food":"pizza", "drink":"ginger ale"]. Inside the callback I then add another node named dessert. I add the dict keys/values cake and iceCream to that ["cake":"coconut", "iceCream":"butter pecan"].

FB Database code:

var mealDict = [String:AnyObject]()
mealDict.updateValue("pizza", forKey: "food")
mealDict.updateValue("gingerAle", forKey: "drink")

let mealRef = self.dbRef.child("meal").childByAutoID
mealRef.updateChildValues(mealDict, withCompletionBlock: {
            (error, ref) in

         var dessertDict = [String:AnyObject]()
         dessertDict.updateValue("coconut", forKey: "cake")
         dessertDict.updateValue("butter pecan", forKey: "iceCream")

          let dessertRef = mealRef.childByAutoID
          dessertRef.updateChildValues(dessertDict)
})

Firebase layout:

root
 |
 @---meal
      |
      @---12345 //childByAutoID for each meal
            |
            |---drink: "ginger ale"
            |---food : "pizza"
            |
            @--78900 //childByAutoID for each dessert
                 |----cake : "coconut"
                 |----iceCream : "butter pecan"

Inside the Algolia console >Indices > Rankings it shows on the mealRef I can directly index the attributes ‘drink’ and ‘food’ and I can also index the dessertRef's attributes which are listed as ‘78900.cake’ and ‘78900.iceCream’ but they have the childByAutoID in front of them which I don’t want to index.

Algolia console > Indices > Rankings shows my attributes as:

Searchable Attributes:
                      78900.cake
                      78900.iceCream
                      drink
                      food
                      objectID

The problem is I want to add the Searchable Attributes as:

  • cake
  • iceCream
  • drink
  • food

How can I add “cake” and “iceCream” as attributes without the “78900” or ay other childByAutoID preceding them?

Searchable Attributes:
                      cake
                      iceCream
                      drink
                      food
                      objectID

Further info:

I set my index in Node.js:

var index = client.initIndex('meal');
var rootRef = firebase.database().ref('meal');

Currently inside my Algolia console > Indices > Browse it shows my “table” as:

Matching 1 hit in 1 ms:

         objectID: 12345
            drink: "ginger ale"
             food: "pizza"

            78900: cake: "coconut"
                   ice cream: "butter pecan"

FYI If I added another dessertNode it would look like:

 root
 |
 @---meal
      |
      @---12345
            |
            |---drink: "ginger ale"
            |---food : "pizza"
            |
            @--78900 
            |    |----cake : "coconut"
            |    |----iceCream : "butter pecan"
            |
            |
            @--55221 //another childByAutoID
                 |----cake : "red velvet"
                 |----iceCream : "vanilla"

When all is done I would want my search results to display:

  • coconut

  • butter pecan

  • ginger ale

  • pizza

  • red velvet

  • vanilla

1 Like

@lsamaria Are you able to edit the post to add spaces for readability? I think some may have gotten stripped out when you copied and pasted.

2 Likes

@dzello I’m sorry for getting back to you so late. i never got an email letting me know there was a response to my question. I didn’t think anyone answered it so I decided to continue building my app without it. I just logged in to ask another question and saw your response. Thank you very much for responding!

I’m going to add the post to StackOverflow. Your correct when you said pieces got stripped out. i’m not sure why that happened??? I’ll update the question and add it there then send you a message when done.

1 Like

@dzello Hello. You was right about the readability, I have no idea what I was thinking, I was barely able to understand it myself and I wrote it, lol. Anyhow I hope this is cleaner and clearer. I used objects anyone can relate to.

I’m not sure why I didn’t get an email when you sent the message but I’ll check everyday for now on.
Thanks!!!

@lsamaria No worries! Thanks for making it clearer and expanding on the examples.

Sorry about the lack of email, unless you’ve changed your preferences you should have gotten one :confused: Any chance it went to spam?

I will review and see if I have an answer for you.

1 Like

@dzello Thanks for helping out! Forget what I just said, I thoroughly checked the spam folder again and it was there. I have to change my filter options.

1 Like

I think you want your Algolia records to look like this:

{
    meal: "12345,
    drink: "ginger ale",
    food: "pizza",
    desserts: [{
       id: "78900",
       cake: "coconut",
       iceCream: "butter pecan"
    }, {
      ...
    }]
}

Then your searchableAttributes will be food, drink, desserts.cake and desserts.iceCream.

Most likely, you will need to write a custom program to iterate over the firebase node hierarchy and transform the objects into this format.

1 Like

@dzello. Yep that’s exactly what I want. I thought maybe there was a way to do it but I guess I have to make it custom. If ain’t 1 thing its another… :slight_smile: Thank you very much for your help. I’m actually about to post another question about my index not automatically updating. I have to run pm start in terminal and everything is fine but without that I’m stuck. Anyhow I’ll post in a couple of hrs.Thank again !!!

1 Like