How can I mark root object as null

So my mapping looks like this. My query is I would like to mark the custom_field property as null as a root
[NULL_VALUES IN ELASTIC SEARCH] (null_value | Elasticsearch Guide [8.6] | Elastic)

 "custom_field":{
                "properties":{
                    "pid":{
                        "type":"integer",
                        "index":false
                    },
                    "sharing_type":{
                        "type":"keyword"
                    }
                }
            },

I am not sure I understand what the problem is and what you are trying to achieve. Can you please provide an example and elaborate a bit more?

So, the output could be like this

custom_field: {
   "pid": 2,
   "sharing_type": "global"
}

or
custom_field: null

So my query is since null is not indexed. I need to use null_values mapping. So how can I achieve this? for the given example

I do not think you can set a node to null, but you should be able to set it to an empty object:

custom_field: {}

The standard way to handle fields that are not populated is however to completely omit them from the document.

Sorry my bad, the structure is an array of objects. Not just an object. So now that its an array of objects or a null value. How can I accomplish this?

custom_field: [{
"pid": 2,
"sharing_type": "global"
}, {
"pid": 3,
"sharing_type": "local"
}]

Set an empty array:

custom_field: []

Okay but empty arrays are also treated as null right? So now how do I mention it as null_value?

You don't.

If there are no objects in the list, either omit the field completely or show it as an empty array. Null values only apply to fields that are indexed, which the root object is not. In the example above the fields in the array are indexed as custom_field.pid and custom_field.sharing_type. custom_field is part of the path but not indexed separately, so you can not set a null value for it.

That's interesting. So If I wish to index custom_field do I need to write it as

mapping: {
  "properties": {
     "custom_field": {
       "type": <What should go here?????>
     }
   }
}

Each field must have a mapping and can not be an object and allow a value at the same time. If you make custom_field any type except object it will be able to take a value but can no longer hold an array of objects as in your example.

That is why I am recommending either omitting it or setting it to an empty array as you can not assign a null value.

So if I set it as an empty array. Can I run a query like "Give me all the docs which has empty custom_field"??

No, I do not think you can (could be wrong though). You may however be able to use a scripted or runtime field to check if the field is empty (may be slow) or you could instead check for the existence of any of the fields in the objects held in the array (if they all do not exist the array must be empty).

Let's see if anyone else have a better answer...

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.