# How to update a field with null using NEST and the Update API

**URL:** <https://discuss.elastic.co/t/how-to-update-a-field-with-null-using-nest-and-the-update-api/299390>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [March 11, 2022, 3:11am UTC](https://discuss.elastic.co/t/how-to-update-a-field-with-null-using-nest-and-the-update-api/299390 "2022-03-11T03:11:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![xef](https://avatars.discourse-cdn.com/v4/letter/x/898d66/32.png) [@xef](https://discuss.elastic.co/u/xef)\
**Post date:** [March 11, 2022, 3:11am UTC](https://discuss.elastic.co/t/how-to-update-a-field-with-null-using-nest-and-the-update-api/299390/1 "2022-03-11T03:11:23Z")

</div>

We are using the NEST library with Elasticsearch versions 7.14.  
Lets say we have assigned a field Greeting with a value of "hello World".  
The filed is mapped as a Keyword type and Not Indexed.

If you update that document using the Update API with the same field having a null value, Elasticsearch ignores that assignement and keeps what was originally there i.e. "Hello World". In order to get rid of the fields value you have to assign it something like an empty string which classes in .NET with string properties normally have a default value of NULL.

```auto
var result = client.Update<object>(id, u => u
                 .Index("services")
                 .Doc(new
                 {
                    Greeting=null
                     
                 })

```

So when a user updates a field by removing its value in the UI, the index does not take that change and keeps the old value.

So you actually have a completely different data in the index than what you think you have after an update.

---

<div class="post-metadata">

**Author:** ![sagarpatel](https://avatars.discourse-cdn.com/v4/letter/s/ebca7d/32.png) [@sagarpatel](https://discuss.elastic.co/u/sagarpatel)\
**Post date:** [March 11, 2022, 7:21am UTC](https://discuss.elastic.co/t/how-to-update-a-field-with-null-using-nest-and-the-update-api/299390/2 "2022-03-11T07:21:48Z")

</div>

Hi,

it will not allow to add null value as it is not indexable in lucene. You can handle [null values](https://www.elastic.co/guide/en/elasticsearch/reference/7.14/null-value.html) using specifing null\_values attributes in index mapping (it will set default values).

Please check [#5152](https://github.com/elastic/elasticsearch-net/issues/5152) and comments on [#3930](https://github.com/elastic/elasticsearch-net/issues/3930#issuecomment-509512750) issue for handling null value in NEST. They have gievn example for date field but same will work for keyword field as well.

---

<div class="post-metadata">

**Author:** ![xef](https://avatars.discourse-cdn.com/v4/letter/x/898d66/32.png) [@xef](https://discuss.elastic.co/u/xef)\
**Post date:** [March 11, 2022, 7:48pm UTC](https://discuss.elastic.co/t/how-to-update-a-field-with-null-using-nest-and-the-update-api/299390/3 "2022-03-11T19:48:08Z")

</div>

Hi  
Thanks for your response.  
But you can actually insert null using a script.  
So why can you do that with a script but not a partial document?

---

<div class="post-metadata">

**Author:** ![sagarpatel](https://avatars.discourse-cdn.com/v4/letter/s/ebca7d/32.png) [@sagarpatel](https://discuss.elastic.co/u/sagarpatel)\
**Post date:** [March 16, 2022, 11:06am UTC](https://discuss.elastic.co/t/how-to-update-a-field-with-null-using-nest-and-the-update-api/299390/4 "2022-03-16T11:06:35Z")

</div>

Sorry for late response. Can you please tell me how you are updating field with `NULL` using script.

Because when you try updating field with NULL using below script, it will throw below error:

**Script**

```auto
POST my-index-000001/_update_by_query
{
  "query": {
    "match_all": {}
  }, 
  "script": {
    "source": "ctx._source['name'] = NULL",
    "lang": "painless"
  }
}

```

**ERROR**

```auto
 "caused_by" : {
      "type" : "illegal_argument_exception",
      "reason" : "cannot resolve symbol [NULL]"
    }

```

If you provide `NULL` as string value with single quotes, it will add `NULL` as string for specific field. so you are still have string or text value for that specific field and not null value.

**Script**

```auto
POST my-index-000001/_update_by_query
{
  "query": {
    "match_all": {}
  }, 
  "script": {
    "source": "ctx._source['name'] = 'NULL'",
    "lang": "painless"
  }
}

```

**Output Document**

```auto
"hits" : [
      {
        "_index" : "my-index-000001",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "NULL"
        }
      }
    ]

```

---

<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 13, 2022, 11:06am UTC](https://discuss.elastic.co/t/how-to-update-a-field-with-null-using-nest-and-the-update-api/299390/5 "2022-04-13T11:06:55Z")

</div>

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