#community-help

Handling Nested Objects in Typesense

TLDR Darren queried about storing arrays of objects in Typesense. Kishore Nallan advised that Typesense doesn't support nesting but shared alternative reindexing methods. They added improved nesting support is planned for the future.

Powered by Struct AI
14
20mo
Solved
Join the chat
Dec 27, 2021 (21 months ago)
Darren
Photo of md5-fa83de4db3e1444aecaf7a587e35eb16
Darren
10:09 AM
On of my data models has an array of objects. notifications[{ message: <string>, level: <string> } ... ] - seems like from the docs, I can't store this at typsense as there is no support for a nested object like this that I can turn into an array? I would have liked from the search results to show the level of any notifications in search results, but I guess I can't? Perhaps if I restructure it on indexing to be { '0': { message, level }, '1': { message, level } ... } ... thoughts?
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
10:19 AM
Typesense does not support indexing of array of objects. You can have those fields flattened out as array of strings. E.g.

messages: ["m1", "m2"]
levels: [1, 2, 3]

Then with some client side code you can show the level.
10:20
Kishore Nallan
10:20 AM
The other option is to index those messages as top level objects with message, level and notification_id fields, and then doing a group_by on notifications.
Darren
Photo of md5-fa83de4db3e1444aecaf7a587e35eb16
Darren
10:32 AM
Ok, I'll have to get creative with how I index these.
PS - just to confirm - typesense does support nested paths yes? eg <http://status.is:1|status.is:1> as a filter? Just asking because I can't results returned with such a filter and I couldn't find any reference or example in the docs that nested paths are supported
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
10:33 AM
We don't support any form of nesting. You can have a field with dots in the name, but they are not considered any different from any other character.
Darren
Photo of md5-fa83de4db3e1444aecaf7a587e35eb16
Darren
10:34 AM
Ah. Ok, I'll have to flatten the data at indexing also. Thanks
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
10:34 AM
So you can have a field name called <http://status.is|status.is> but it is just a regular field name. People use dots to refer to fields that they "flatten" out this way prior to indexing.
Darren
Photo of md5-fa83de4db3e1444aecaf7a587e35eb16
Darren
10:36 AM
Yeah, got it, thanks. Past experience with Algolia, I recall having nested structures so I assumed I could here. No worries. Thanks.
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
10:36 AM
Yeah, it's on our roadmap to support next year 🙂
Darren
Photo of md5-fa83de4db3e1444aecaf7a587e35eb16
Darren
10:46 AM
You guys are on 🔥! Great product. You deserve every success!
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
10:46 AM
Thank you!
12:54
Kishore Nallan
12:54 PM
Darren just to be sure. If you don't intend to search on nested documents they can certainly be part of the document. They will be stored and returned in the response verbatim. What we don't support is the ability to index and search on nested fields.
Darren
Photo of md5-fa83de4db3e1444aecaf7a587e35eb16
Darren
02:14 PM
Yeah, I understand. I did need to facet them. But its ok, I haven't locked in my serializer/unserializer to/from typesense db, so I will flatten them. Better for your product of course if you didn't need to ... next year ...
Jan 04, 2022 (20 months ago)
Martin
Photo of md5-4c44d17869045701f2ca4035972ad4c5
Martin
05:02 AM
We discussed this before and I look forward to Typesense supporting searching in objects/nested structures. I have a standard use case with a blog with posts and authors, and for now solve it my flattening the authors field (into authors and author_ids).