#community-help

Discussing Implementation of Nested Fields Feature

TLDR Stefan asks about modeling ingredients data in a database, but realizes it might reference too much for the search engine. Kishore Nallan confirms his query is IN search-related and mentions plans to support nested fields in later updates.

Powered by Struct AI

1

Feb 12, 2022 (21 months ago)
Stefan
Photo of md5-d6c265b4792dbf0a1d6ae378f39d8736
Stefan
12:25 PM
Hi!
Am I correct that there is currently no way to cover a use case like this:
Assuming I have a database of meals where each meal has X ingredients.
Each ingredient is present in a certain amount

In a perfect world I would model it like this:
ingredients: [
{
    name: "chicken",
    concentration: num
}
]


And then filter for it like this:
ingredients.name = 'chicken' AND ingredients.concentration >= 5

Where ingredients should refer to the same nested Object

While writing this I realized that this is probably way too much for typesense or any search engine to cover. This is something for a database. Asking it anyway just in case I am missing something.

Only idea I have to cover this use case is to go crazy and flatten the ingredients array like this:
chicken.present: bool
chicken.concentration: num

But given that there can be thousands of ingredients this would probably not scale well.
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
12:32 PM
I've seen that post before. This is a form of IN search where the filter comparison is applied within an object fields than across globally. When we add support for nested fields, we will have to support this as well. Mongo has operators for distinguishing between in-record and cross-record searches that we might take inspiration from.

The other way to solve this is with a join, which we are also exploring. In that case you will have a separate ingredients collection that has a reference id to the main recipe collection such that it will be trivial to filter on the ingredients and then join on the other fields from the recipe collection.
12:33
Kishore Nallan
12:33 PM
Supporting joins will probably cover a lot of cases where nested fields are useful.
Stefan
Photo of md5-d6c265b4792dbf0a1d6ae378f39d8736
Stefan
12:33 PM
Wow that would be awesome. Didn't expect this to be in the scope of the nested feature
12:35
Stefan
12:35 PM
really looking forward to this. If there is an alpha version I can test it in production 😅
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
12:38 PM
Joins or nested fields? 🙂 Both are only at conceptual spike stage at the moment.
Stefan
Photo of md5-d6c265b4792dbf0a1d6ae378f39d8736
Stefan
12:41 PM
I think for my particular use case the nested fields are a better fit. Using joins I would need to implement some kind of many to many relationship. Nested fields seems easier

1