# How to Update a Field in a document to NULL usiong NEST and the Update API

**URL:** <https://discuss.elastic.co/t/how-to-update-a-field-in-a-document-to-null-usiong-nest-and-the-update-api/299003>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [March 7, 2022, 6:35pm UTC](https://discuss.elastic.co/t/how-to-update-a-field-in-a-document-to-null-usiong-nest-and-the-update-api/299003 "2022-03-07T18:35:52Z")\
**Posts on this page:** 6\
**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 7, 2022, 6:35pm UTC](https://discuss.elastic.co/t/how-to-update-a-field-in-a-document-to-null-usiong-nest-and-the-update-api/299003/1 "2022-03-07T18:35:52Z")

</div>

This is the third time we are posting this in the hope someone can shed some light on it - Please.

We are using the NEST library with Elasticsearch versions 7.14.  
Lets say we have assigned a field 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.

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:** ![casterQ](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/casterq/32/93257_2.png) [@casterQ](https://discuss.elastic.co/u/casterQ)\
**Post date:** [March 8, 2022, 2:04am UTC](https://discuss.elastic.co/t/how-to-update-a-field-in-a-document-to-null-usiong-nest-and-the-update-api/299003/2 "2022-03-08T02:04:52Z")

</div>

is this what you need？

```auto
PUT c3
{
  "mappings": {
    "properties": {
      "k1": {
        "type": "keyword"
      }
    }
  }
}
POST c3/_doc
{
  "k1":"hello world",
  "k2":1
}
POST c3/_search
{
  "query": {
    "term": {
      "k1": {
        "value": "hello world"
      }
    }
  }
}
POST c3/_update_by_query
{
  "query": {
    "term": {
      "k1": {
        "value": "hello world"
      }
    }
  },
  "script": {
    "source": "ctx._source.k1 = null",
    "lang": "painless"
  }
}
POST c3/_search

```

---

<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 9, 2022, 6:02am UTC](https://discuss.elastic.co/t/how-to-update-a-field-in-a-document-to-null-usiong-nest-and-the-update-api/299003/3 "2022-03-09T06:02:19Z")

</div>

Hi. Thanks for your response.  
Does one **have to** use a script to set a field value to null?  
I mean why can't one use the Update API by indexing a partial document such as:

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

```

---

<div class="post-metadata">

**Author:** ![casterQ](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/casterq/32/93257_2.png) [@casterQ](https://discuss.elastic.co/u/casterQ)\
**Post date:** [March 9, 2022, 6:23am UTC](https://discuss.elastic.co/t/how-to-update-a-field-in-a-document-to-null-usiong-nest-and-the-update-api/299003/4 "2022-03-09T06:23:43Z")

</div>

you can also use pipelien to update:

```auto
  "pipeline": {
    "processors": [
      {
        "remove": {
          "field": "k1"
        }
      }
    ]
  }

```

---

<div class="post-metadata">

**Author:** ![casterQ](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/casterq/32/93257_2.png) [@casterQ](https://discuss.elastic.co/u/casterQ)\
**Post date:** [March 11, 2022, 3:40am UTC](https://discuss.elastic.co/t/how-to-update-a-field-in-a-document-to-null-usiong-nest-and-the-update-api/299003/5 "2022-03-11T03:40:40Z")

</div>

does pipeline help you？

---

<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 8, 2022, 3:40am UTC](https://discuss.elastic.co/t/how-to-update-a-field-in-a-document-to-null-usiong-nest-and-the-update-api/299003/6 "2022-04-08T03:40:44Z")

</div>

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