Hi :wave: I'm trying to craft a schema with neste...
# community-help
e
Hi 👋 I'm trying to craft a schema with nested objects, and make them sortable. Is that possible? The below schema errors with (v29):
Field cells.number_value cannot be a sortable field.
Copy code
const spreadsheet_rows = {
  name: "spreadsheet_rows",
  enable_nested_fields: true,
  fields: [
    { name: "id", type: "string" },
    { name: "account_id", type: "string" },
    { name: "spreadsheet_id", type: "string", sort: true },

    // { name: "address.zip", type: "string" },
    { name: "cells", type: "object[]" },
    { name: "cells.key", type: "string[]" },
    { name: "cells.number_value", type: "int64[]", optional: true, sort: true },
    { name: "cells.string_value", type: "string[]", optional: true, sort: true },
    { name: "cells.boolean_value", type: "bool[]", optional: true, sort: true },
    { name: "cells.status", type: "string[]", optional: true },
    { name: "cells.updated_at", type: "int64[]", optional: true },
  ],

  default_sorting_field: "spreadsheet_id",
}
k
int64[]
cannot be a sortable field. It must be a single valued type like
int64
or
int32
.
e
When I change to int64 it fails saying a value must be an array
k
In this case, I realize it's an array of objects, with each array object having this value. From Typesense perspective, it's not clear which array value should be used to order the record among peers.
e
I ended up with:
Copy code
{ name: "cells.*", type: "object" },
    { name: "cells.*.key", type: "string" },
    { name: "cells.*.status", type: "string", optional: true },
    { name: "cells.*.updated_at", type: "int64", optional: true },
    { name: "cells.*.number_value", type: "int64", optional: true, sort: true },
    { name: "cells.*.string_value", type: "string", optional: true, sort: true },
    { name: "cells.*.boolean_value", type: "bool", optional: true, sort: true },
which seems to work?
k
It could be a validation quirk 🤔 The underlying issue that when an array is involved, which value should be used as the representative for that document? The sort field value must be singular.
e
My understanding is that what a field is marked as sortable it can be used by the
sort_by
parameter in the search request. But perhaps I can use "conditions" without having to make the field sortable?
k
The sort value needs to be singular for each document.
_eval()
still needs a condition which will produce a singlular true/false value.
e
Copy code
{
  "sort_by": "cells.line_1.string_value:desc"
}
Would work then based on the above?
This is what I get when I query the collection (after creating the schema and adding documents)
Copy code
{
  "facet": false,
  "index": true,
  "infix": false,
  "locale": "",
  "name": "cells.line_1.string_value",
  "optional": true,
  "sort": true,
  "stem": false,
  "stem_dictionary": "",
  "store": true,
  "type": "string"
}
Did some testing, this works. Is this a glitch? Can I rely on this functionality?
k
But this is a string field, earlier we were discussing integer array. I don't follow.
e
Lets start again I was hoping to create a dynamic schema one that contains an array of objects, each is identified by a unique key (for example
address_line_1
) Here's what I ended up doing in order to achieve a dynamic schema, and make it sortable:
Copy code
const orders = {
  name: "orders",
  enable_nested_fields: true,
  fields: [
    { name: "id", type: "string" },
    { name: "account_id", type: "string" },
    { name: "order_id", type: "string", sort: true },

    { name: "cells.*", type: "object" },
    { name: "cells.*.key", type: "string" },
    { name: "cells.*.status", type: "string", optional: true },
    { name: "cells.*.updated_at", type: "int64", optional: true },
    { name: "cells.*.number_value", type: "int64", optional: true, sort: true },
    { name: "cells.*.string_value", type: "string", optional: true, sort: true },
    { name: "cells.*.boolean_value", type: "bool", optional: true, sort: true },
  ],

  default_sorting_field: "order_id",
}
Copy code
const rows = await client
  .collections('orders')
  .documents()
  .search({
    q: '*',
    sort_by: 'cells.address_line_1.string_value:desc',
  })
The above works from what I can tell. The documents added to the collections are marked as sortable, and work when used in a search request. Is this the expected behavior?
@Kishore Nallan?
k
Also give me a sample document which will get indexed with this schema?
e
Copy code
{
  "cells": {
    "company_domain": {
      "string_value": "possimus.net"
    },
    "company_size_min": {
      "number_value": 4722
    },
    "emails": {
      "string_value": "janet+zawadzki@centurytel.dev, koichi_romero@aim.com"
    },
    "full_name": {
      "string_value": "Muhammed Ikeda"
    },
    "job_title": {
      "string_value": "Principal Web Engineer"
    },
    "linkedin_profile": {
      "string_value": "https://www.linkedin.com/in/Abdullahi_Magnússon"
    },
    "location": {
      "string_value": "1035 Hilma Mills, Jaskolskifort, 23816, Slovakia (Slovak Republic)"
    },
    "phones": {
      "string_value": "+221 45 297 4875, +855 39 162 286"
    },
    "seniority": {
      "string_value": "Central"
    }
  },
  "id": "49991",
  "order_id": "019ab580-cbf2-7224-9437-5cd8c867cb52",
  "account_id": "1"
}
k
This doc only has nested object and not array of objects.
e
Yep. like I said, this is what works for me. Is that OK? Am I using it in the way in which it was designed for?
k
Yes it's fine to use