#community-help

Handling Null Fields in Schema

TLDR Ivan reported a bug regarding null fields in the schema. Kishore Nallan confirmed it as a bug, asked to file a Github report, and mentioned it was fixed in version 0.25.0.rc25.

Powered by Struct AI

1

1

10
5mo
Solved
Join the chat
Apr 28, 2023 (5 months ago)
Ivan
Photo of md5-a10e9010023d49c7c0b051c07c3b384f
Ivan
07:37 AM
Hi Folks, I have a schema that looks something like this:
{
  name: '...',
  enable_nested_fields: true,
  fields: [
    { name: 'test', type: 'object' },
    { name: 'test.entry1', type: 'string', facet: true, optional: true },
    { name: 'test.entry2', type: 'string', facet: true, optional: true },
    ...
  ]
}

Now If I query this collection a return object could look like this:
{
  test: { entry1: 'something', entry2: null },
  test.entry2: null
}

If the value of a field is null then it is added to the root of the return object as an additional property. E.g. test.entry2 in the example above.

Has anyone else noticed this?
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
07:42 AM
Hi Ivan, can give me a sample document that you indexed which is returned this way?
Ivan
Photo of md5-a10e9010023d49c7c0b051c07c3b384f
Ivan
09:37 AM
Sure thing
09:38
Ivan
09:38 AM
const schema = {
  name: 'test',
  enable_nested_fields: true,
  fields: [
    { name: 'text', type: 'string' },
    { name: 'person', type: 'object' },
    { name: 'person.name', type: 'string', facet: true, optional: true },
    { name: 'person.age', type: 'string', facet: true, optional: true },
    { name: 'person.address', type: 'string', facet: true, optional: true },
  ]
}

const client = new Typesense.Client(...)

try {
  await client.collections().create(schema)
} catch (err) {
  ...
}

const data = [
  { text: 'a', person: { name: null, age: null, address: null } },
  { text: 'b', person: { name: 'test', age: null, address: null } },
  { text: 'c', person: { name: null, age: 'test', address: null } },
  { text: 'd', person: { name: null, age: null, address: 'test' } }
]

try {
  await client.collections(schema.name).documents().import(data)
} catch(err) {
  ...
}
09:39
Ivan
09:39 AM
"hits": [
    {
      "document": {
        "id": "3",
        "person": {
          "address": "test",
          "age": null,
          "name": null
        },
        "person.age": null,
        "person.name": null,
        "text": "d"
      },
      "highlight": {},
      "highlights": []
    },
    {
      "document": {
        "id": "2",
        "person": {
          "address": null,
          "age": "test",
          "name": null
        },
        "person.address": null,
        "person.name": null,
        "text": "c"
      },
      "highlight": {},
      "highlights": []
    },
    {
      "document": {
        "id": "1",
        "person": {
          "address": null,
          "age": null,
          "name": "test"
        },
        "person.address": null,
        "person.age": null,
        "text": "b"
      },
      "highlight": {},
      "highlights": []
    },
    {
      "document": {
        "id": "0",
        "person": {
          "address": null,
          "age": null,
          "name": null
        },
        "person.address": null,
        "person.age": null,
        "person.name": null,
        "text": "a"
      },
      "highlight": {},
      "highlights": []
    }
  ]
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
09:44 AM
Thanks, this is a bug. I will look into it
Ivan
Photo of md5-a10e9010023d49c7c0b051c07c3b384f
Ivan
09:58 AM
Awesome. Thank you. Do you want me to file a bug report on github?
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
09:59 AM
Yes please that will be great!

1

10:32
Kishore Nallan
10:32 AM
Ivan This has already been fixed in recent 0.25 RC builds, can you please check against 0.25.0.rc25
Ivan
Photo of md5-a10e9010023d49c7c0b051c07c3b384f
Ivan
12:32 PM
Can confirm, this is now fixed. Thank you

1