gatsby-plugin-typesense icon indicating copy to clipboard operation
gatsby-plugin-typesense copied to clipboard

Support for nested objects

Open amamenko opened this issue 2 years ago • 0 comments

Typesense supports indexing nested objects and arrays of objects since v0.24: https://typesense.org/docs/guide/tips-for-searching-common-types-of-data.html#nested-objects.

I'm not sure if gatsby-plugin-typesense has support for nested objects. For example, if I have a collection schema set up as:

options: {
  rootDir: `${__dirname}/public/something`,
  collectionSchema: {
    name: `something`,
    enable_nested_fields: true,
    fields: [
      {
        name: "id",
        type: "string",
        optional: true,
      },
      {
        name: "pages",
        type: "object[]",
      },
      {
        name: "pages.index",
        type: "int32[]",
      },
      {
        name: "pages.body",
        type: "string[]",
      },
    ],
  }

Then I should be able to index a nested object using:

<div data-typesense-field="pages">
  {pages.map((page, index) => {
    return (
      <React.Fragment key={index}>
        <div data-typesense-field="pages.index">
          {index}
        </div>
        <div data-typesense-field="pages.body">
          {page.body}
        </div>
      </React.Fragment>
    );
  })}
</div>

So that a returned object looks something like:

{
  "id": "anything",
  "pages": [
    {
      "index": 0,
      "body": "body0",
    },
    {
      "index": 1,
      "body": "body1",
    },
  ]
}

I believe the closest this plugin can get to a nested object is using object flattening, i.e. indexing pages.index and pages.body as separate fields on the document.

amamenko avatar Sep 22 '23 14:09 amamenko