How to format data to algolia

I’m learning How to use Algolia and I have a few questions.

I have the table FreightDriver, which has a relationship one to many with the table Truck

id: ID!
name: String!
email: String!
phoneNumber: String!
cities: [String!]!
state: String!
country: String!
picture: String
trucks: [Truck] @connection(keyName: "byFreightDriver", fields: ["id"])

Then, the table Truck, which has a relationship one to one with the table FreightDriver, and another table, which is Box one to one as well

id: ID!
freightDriverId: ID!
boxId: ID!
brand: String!
model: String!
yearModel: Int!
// and more fields
freightDriver: FreightDriver @connection(fields: ["freightDriverId"])
box: Box @connection(fields: ["boxId"])

and the table Box:

id: ID!
type: String!
width: Float!
height: Float!
depth: Float!

I want to find trucks based on the state and city of the FreightDrivers, and also (if the user needs it), find based on the type of box and brand or model of a truck (just as an example).

Example find by state and city of a FreightDriver:

{
  "data": {
    "freightDriversByState": {
      "items": [
        {
          "name": "Andrés Montoya",
          "cities": [
            "GUADALAJARA"
          ],
          "state": "JALISCO",
          "country": "MX",
          "trucks": {
            "items": [
              {
                "brand": "chevrolet",
                "model": "12",
                "yearModel": 2020,
                "box": {
                  "type": "Ganadera",
                  "width": 1,
                  "height": 2,
                  "depth": 3
                }
              },
              {
                "brand": "chevrolet",
                "model": "12",
                "yearModel": 2020,
                "box": {
                  "type": "Seca (abierta)",
                  "width": 12,
                  "height": 12,
                  "depth": 12
                }
              },
              {
                "brand": "chevrolet",
                "model": "12",
                "yearModel": 2020,
                "box": {
                  "type": "Seca (abierta)",
                  "width": 12,
                  "height": 12,
                  "depth": 12
                }
              },
              {
                "brand": "chevrolet",
                "model": "Semi",
                "yearModel": 2020,
                "box": {
                  "type": "Seca (abierta)",
                  "width": 1,
                  "height": 2,
                  "depth": 3
                }
              },
              {
                "brand": "chevrolet",
                "model": "12",
                "yearModel": 2020,
                "box": {
                  "type": "Seca (abierta)",
                  "width": 1,
                  "height": 2,
                  "depth": 3
                }
              },
              {
                "brand": "chevrolet",
                "model": "12",
                "yearModel": 2020,
                "box": {
                  "type": "Volcadora",
                  "width": 1,
                  "height": 2,
                  "depth": 3
                }
              },
              {
                "brand": "hola",
                "model": "12",
                "yearModel": 12,
                "box": {
                  "type": "Plataforma",
                  "width": 1,
                  "height": 2,
                  "depth": 3
                }
              },
              {
                "brand": "Ford",
                "model": "12",
                "yearModel": 2020,
                "box": {
                  "type": "Seca (abierta)",
                  "width": 1,
                  "height": 2,
                  "depth": 1
                }
              },
              {
                "brand": "dasdas",
                "model": "12",
                "yearModel": 12231,
                "box": {
                  "type": "Grúa",
                  "width": 1,
                  "height": 2,
                  "depth": 3
                }
              },
              {
                "brand": "Tesla",
                "model": "Semi",
                "yearModel": 2020,
                "box": {
                  "type": "Seca (abierta)",
                  "width": 4,
                  "height": 4,
                  "depth": 2
                }
              }
            ]
          }
        },
        {
          "name": "Roberto mendez",
          "cities": [
            "GUADALAJARA"
          ],
          "state": "JALISCO",
          "country": "MX",
          "trucks": {
            "items": []
          }
        },
        {
          "name": "Fletes Jalisco Sa de Cv ",
          "cities": [
            "GUADALAJARA"
          ],
          "state": "JALISCO",
          "country": "MX",
          "trucks": {
            "items": [
              {
                "brand": "Ford",
                "model": "F-450",
                "yearModel": 2018,
                "box": {
                  "type": "Seca (cerrada)",
                  "width": 2.7,
                  "height": 2.5,
                  "depth": 4.5
                }
              },
              {
                "brand": "Hiundai",
                "model": "H100",
                "yearModel": 2009,
                "box": {
                  "type": "Seca (abierta)",
                  "width": 2.3,
                  "height": 2,
                  "depth": 4
                }
              },
              {
                "brand": "Hiundai",
                "model": "H100",
                "yearModel": 2020,
                "box": {
                  "type": "Seca (abierta)",
                  "width": 2.35,
                  "height": 2,
                  "depth": 3
                }
              },
              {
                "brand": "Ford",
                "model": "F-450",
                "yearModel": 2018,
                "box": {
                  "type": "Seca (cerrada)",
                  "width": 2.7,
                  "height": 2.2,
                  "depth": 3
                }
              },
              {
                "brand": "Ford",
                "model": "F-450",
                "yearModel": 2004,
                "box": {
                  "type": "Seca (abierta)",
                  "width": 2.5,
                  "height": 2,
                  "depth": 3
                }
              }
            ]
          }
        },
        {
          "name": "Cotransport",
          "cities": [
            "GUADALAJARA"
          ],
          "state": "JALISCO",
          "country": "MX",
          "trucks": {
            "items": [
              {
                "brand": "Chevrolet",
                "model": "CX-5",
                "yearModel": 2019,
                "box": {
                  "type": "Seca (cerrada)",
                  "width": 4,
                  "height": 3,
                  "depth": 4
                }
              }
            ]
          }
        },
        {
          "name": "Andrés",
          "cities": [
            "EL LIMÓN",
            "MAGDALENA",
            "SANTA MARÍA DEL ORO",
            "GUADALAJARA",
            "ETZATLÁN"
          ],
          "state": "JALISCO",
          "country": "MX",
          "trucks": {
            "items": [
              {
                "brand": "chevrolet",
                "model": "12",
                "yearModel": 2020,
                "box": {
                  "type": "Seca (abierta)",
                  "width": 1,
                  "height": 2,
                  "depth": 3
                }
              }
            ]
          }
        }
      ]
    }
  }
}

But, if I want to find using the state and city of a freight driver, the type of box and the model or brand of a truck, that’s where the problem comes, because I’m using Dynamodb and this one does not have relationships, so, it’s a little bit tricky to get the exact data. That’s why I’d like to use Algolia, but how do I format the data for Algolia in this case? What do I need to do? thanks!

Hi @andresmontoyafcb

It seems like you’re searching for “trucks”, not drivers.
I suggest you have a index named “trucks”.
Algolia’s indexing structure isn’t 1:1 mapped with relational databases. So you’d need to adjust things.

Each truck record will have its “box” attribute.
Then trucks can be searched based on the attributes of truck, and also on the attributes of box, right?

And if you need to search for trucks based on drivers’ state and city, then I recommend you to put state and city attributes into each truck. So the index trucks will be like the following:

[
  {
    id,
    brand,
    model,
    yearModel,
    box: {
      type,
      width,
      height,
      depth,
    },
    driverLocation: {
      state,
      city,
    }
  },
  ...
]

Whenever state or city of a driver changes, you will have to update all the corresponding trucks, which can be a downside for you, but I believe this structure will bring you the fastest search result.

And if you use InstantSearch with this data structure, you can just put refinementList widgets on the properties like,

refinementList({
  attribute: 'brand',
})

// or

refinementList({
  attribute: 'box.width',
})

// or

refinementList({
  attribute: 'driverLocation.state'
})

Does this make sense to you?
Let me know how it goes and if you have any question!

1 Like

wow! thank you so much! I’ll try to check it and see how it works

1 Like