# How can I mark root object as null

**URL:** <https://discuss.elastic.co/t/how-can-i-mark-root-object-as-null/327719>\
**Category:** Elasticsearch\
**Created:** [March 15, 2023, 5:36am UTC](https://discuss.elastic.co/t/how-can-i-mark-root-object-as-null/327719 "2023-03-15T05:36:59Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vivek\_Burman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vivek_burman/32/118310_2.png) [@Vivek\_Burman](https://discuss.elastic.co/u/Vivek_Burman)\
**Post date:** [March 15, 2023, 5:36am UTC](https://discuss.elastic.co/t/how-can-i-mark-root-object-as-null/327719/1 "2023-03-15T05:36:59Z")

</div>

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](https://www.elastic.co/guide/en/elasticsearch/reference/current/null-value.html#:~:text=A%20null%20value%20cannot%20be,that%20field%20has%20no%20values.&text=Replace%20explicit%20null%20values%20with%20the%20term%20NULL%20.,be%20replaced%20with%20the%20null_value%20.))

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

```

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [March 15, 2023, 6:07am UTC](https://discuss.elastic.co/t/how-can-i-mark-root-object-as-null/327719/2 "2023-03-15T06:07:51Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Vivek\_Burman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vivek_burman/32/118310_2.png) [@Vivek\_Burman](https://discuss.elastic.co/u/Vivek_Burman)\
**Post date:** [March 15, 2023, 6:28am UTC](https://discuss.elastic.co/t/how-can-i-mark-root-object-as-null/327719/3 "2023-03-15T06:28:01Z")

</div>

So, the output could be like this

```auto
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

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [March 15, 2023, 6:53am UTC](https://discuss.elastic.co/t/how-can-i-mark-root-object-as-null/327719/4 "2023-03-15T06:53:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Vivek\_Burman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vivek_burman/32/118310_2.png) [@Vivek\_Burman](https://discuss.elastic.co/u/Vivek_Burman)\
**Post date:** [March 15, 2023, 7:24am UTC](https://discuss.elastic.co/t/how-can-i-mark-root-object-as-null/327719/5 "2023-03-15T07:24:06Z")

</div>

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?

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

```

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [March 15, 2023, 7:30am UTC](https://discuss.elastic.co/t/how-can-i-mark-root-object-as-null/327719/6 "2023-03-15T07:30:16Z")

</div>

Set an empty array:

`custom_field: []`

---

<div class="post-metadata">

**Author:** ![Vivek\_Burman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vivek_burman/32/118310_2.png) [@Vivek\_Burman](https://discuss.elastic.co/u/Vivek_Burman)\
**Post date:** [March 15, 2023, 7:57am UTC](https://discuss.elastic.co/t/how-can-i-mark-root-object-as-null/327719/7 "2023-03-15T07:57:12Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/c/9/c9fb9e3734bf607ed3f273db84cb1f56ed2c3954.png)

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [March 15, 2023, 8:09am UTC](https://discuss.elastic.co/t/how-can-i-mark-root-object-as-null/327719/8 "2023-03-15T08:09:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Vivek\_Burman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vivek_burman/32/118310_2.png) [@Vivek\_Burman](https://discuss.elastic.co/u/Vivek_Burman)\
**Post date:** [March 15, 2023, 8:38am UTC](https://discuss.elastic.co/t/how-can-i-mark-root-object-as-null/327719/9 "2023-03-15T08:38:33Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [March 15, 2023, 8:47am UTC](https://discuss.elastic.co/t/how-can-i-mark-root-object-as-null/327719/10 "2023-03-15T08:47:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Vivek\_Burman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vivek_burman/32/118310_2.png) [@Vivek\_Burman](https://discuss.elastic.co/u/Vivek_Burman)\
**Post date:** [March 15, 2023, 8:57am UTC](https://discuss.elastic.co/t/how-can-i-mark-root-object-as-null/327719/11 "2023-03-15T08:57:05Z")

</div>

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"??

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [March 15, 2023, 9:02am UTC](https://discuss.elastic.co/t/how-can-i-mark-root-object-as-null/327719/12 "2023-03-15T09:02:34Z")

</div>

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...

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 12, 2023, 9:02am UTC](https://discuss.elastic.co/t/how-can-i-mark-root-object-as-null/327719/13 "2023-04-12T09:02:59Z")

</div>

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