Defining Nested Fields in Typesense Schema

TLDR Ivy wanted to define a list of strings in a list of objects, and Jason suggested omitting the explicit field type for "products.ingredients". The schema generated the desired field type.

Photo of Ivy
Ivy
Tue, 09 May 2023 21:20:55 UTC

Hi all, I have a question about nested objects! I'd like to have a schema like this: ```{ storeName: "Best Slice Shop" products: [{ id: 123, name: pizza, ingredients: ["Tomatoes", "Garlic", ... ] } ]``` But it looks like I can only define types on one layer deep (ref ), e.g. ```{ "enable_nested_fields": true, "name": "store", "fields": [ { "name": "products", "type": "object[]" }, { "name": "products.id", "type": "int[]" }, { "name": "products.name", "type": "string[]" }, { "name": "products.ingredients", "type": "string[][]" <-- doesn't work } ] }``` Is there a way to define a field to be list of strings on an list of objects field? Thank you!!

Photo of Jason
Jason
Tue, 09 May 2023 21:22:37 UTC

Interesting! Could you try this and see what happens: ```{ "name": "products.ingredients", "type": "string[]" }```

Photo of Jason
Jason
Tue, 09 May 2023 21:23:08 UTC

Hang on

Photo of Jason
Jason
Tue, 09 May 2023 21:23:12 UTC

Let’s try something else

Photo of Jason
Jason
Tue, 09 May 2023 21:24:02 UTC

Could you use this schema: ```{ "enable_nested_fields": true, "name": "store", "fields": [ { "name": "products", "type": "object[]" }, ] }``` then index one of your documents and then look at the updated collection schema which Typesense generates and check the field type for `products.ingredients`?

Photo of Ivy
Ivy
Tue, 09 May 2023 21:41:08 UTC

Ok it looks like it's string[]! Interesting. Thank you! ``` { "facet": false, "index": true, "infix": false, "locale": "", "name": "products.ingredients", "optional": true, "sort": false, "type": "string[]" },``` ```[ { "id": "123", "ingredients": [ "Tomatoes", "Garlic" ], "name": "pizza" } ]```

Photo of Jason
Jason
Tue, 09 May 2023 21:42:22 UTC

Btw, on a side note, the most generic field definition takes precedence in the collection schema. So if you have both `products` and `products.ingredients` explicitly listed, then products will take precedence and index all nested fields

Photo of Ivy
Ivy
Tue, 09 May 2023 21:46:51 UTC

Ah ok that makes sense and clarifies things, thank you!