#community-help

Nested Objects and Arrays in Typesense

TLDR Robert seeks advice on managing nested objects in Typesense. Kishore Nallan informs of upcoming support for nested objects and provides a current workaround. Robert indicates interest in tracking this feature. Kishore Nallan provides a link to follow the issue.

Powered by Struct AI
raised_hands1
tada1
Jun 23, 2022 (15 months ago)
Robert
Photo of md5-a1b143d897d37e5954a12ce7c4e81071
Robert
10:36 AM
Hello everyone 🙂
I’m planning to use Typesense, but I have nested objects and object array in my structure. I read that flatting may help me, but in this case the indexing would be tricky, so I had another idea, to split my data into different collections. But in this case I would have trouble with the pagination of the results, or I wasn’t able to found any example for this. I made this example object to reflect on my real object:
{
  "id": "random_id_1",
  "name": "John Doe",
  "read_history": [
    {
      "id": "random_book_id_1",
      "name": "Harry Potter and the Philosopher's Stone",
      "author": "J. K. Rowling"
    },
    {
      "id": "random_book_id_2",
      "name": "Ready Player One",
      "author": "Ernest Cline"
    }
  ],
  "favorite_books": [
    "random_book_id_2"
  ]
}

So here is an example, what I want to do. As result I want to display my readers and their favourite book names in a table, but I want to search in author names and book names as well.
In the different index idea, I planned create two indexes, first for general reader data (id, name, favorite_books) and the second would store the read history data with an extra owner_id field to connect the book document to the reader. The indexing was just fine in this case, if I ran a multi collection search I got the results in two different collections, but I want to paginate in the general reader data (and in optimal case, the book data should follow it, based on the shared id, like a SQL join) and not separately. Is there any chance I can do it with Typesense, or I have to merge (fetch and then aggregate) my objects manually?
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
11:50 AM
The good news is that we've already begun working on support for nested object and nested array of objects, so that feature will be available in a few weeks time.

In the mean time, you can convert the array of objects to simple plain arrays like this:

read_history.names: ["Harry Potter and the Philosopher's Stone", "Ready Player One"]

read_history.authors: ["J. K. Rowling", "Ernest Cline"]

tada1
Robert
Photo of md5-a1b143d897d37e5954a12ce7c4e81071
Robert
11:52 AM
Sounds awesome! Looking forward to try it out. Thank you very much for your answer! 🙂
raised_hands1
Jun 24, 2022 (15 months ago)
Robert
Photo of md5-a1b143d897d37e5954a12ce7c4e81071
Robert
08:38 AM
One last thing. Is there a ticket somewhere what I can follow to be up-to-date? I’m pretty interested to try the beta, if this is possible 😄
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
08:43 AM
Yes, you can follow this issue for updates: https://github.com/typesense/typesense/issues/227

I will post on that issue when I have an early build available for testing.
Robert
Photo of md5-a1b143d897d37e5954a12ce7c4e81071
Robert
08:43 AM
Thank you very much! 🙂