# Preventing document update based on field values

**URL:** <https://discuss.elastic.co/t/preventing-document-update-based-on-field-values/356858>\
**Category:** Elasticsearch\
**Created:** [April 5, 2024, 11:02am UTC](https://discuss.elastic.co/t/preventing-document-update-based-on-field-values/356858 "2024-04-05T11:02:14Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashish25](https://avatars.discourse-cdn.com/v4/letter/a/67e7ee/32.png) [@ashish25](https://discuss.elastic.co/u/ashish25)\
**Post date:** [April 5, 2024, 11:02am UTC](https://discuss.elastic.co/t/preventing-document-update-based-on-field-values/356858/1 "2024-04-05T11:02:14Z")

</div>

Is there anyway to prevent updating document based on some field?

Eg-

Below documents already present in elastic:  
{  
"business\_address" : "660 Sacramento St",  
"business\_city" : "San Francisco",  
"business\_id" : 22  
}

And if someone is trying to update above document then it should prevent it bcz "business\_id" : 22

Is there anyway to prevent it?

---

<div class="post-metadata">

**Author:** ![Carlos\_D](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carlos_d/32/126245_2.png) [@Carlos\_D](https://discuss.elastic.co/u/Carlos_D)\
**Post date:** [April 5, 2024, 2:33pm UTC](https://discuss.elastic.co/t/preventing-document-update-based-on-field-values/356858/2 "2024-04-05T14:33:11Z")

</div>

> [@ashish25](#):
>
> Is there anyway to prevent updating document based on some field?

Hey @ashish25 !

You might use script updating for that, something similar to:

```auto
PUT /index_name/_update/doc_id
{
  "script": {
    "lang": "painless", // Scripting language (other options available)
    "source": "if (ctx._source.field_name.value != 'undesired_value') { ... update logic ... }"
  }
}

```

Or, you could implement the logic in your application, and retrieve the doc first before updating to check the value.

Hope that helps!

---

<div class="post-metadata">

**Author:** ![ashish25](https://avatars.discourse-cdn.com/v4/letter/a/67e7ee/32.png) [@ashish25](https://discuss.elastic.co/u/ashish25)\
**Post date:** [April 8, 2024, 6:56am UTC](https://discuss.elastic.co/t/preventing-document-update-based-on-field-values/356858/3 "2024-04-08T06:56:50Z")

</div>

Thanks @Carlos_D for your response, just one more question regarding your script above -  
if (doc.field\_name.value != 'undesired\_value') --\> will it check the value of document which we are trying to ingest or will it check the value of existing document in elastic?

---

<div class="post-metadata">

**Author:** ![Carlos\_D](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carlos_d/32/126245_2.png) [@Carlos\_D](https://discuss.elastic.co/u/Carlos_D)\
**Post date:** [April 8, 2024, 7:15am UTC](https://discuss.elastic.co/t/preventing-document-update-based-on-field-values/356858/4 "2024-04-08T07:15:54Z")

</div>

Hi @ashish25 ! It refers to the current document value. There were some errors in the original script I provided - I updated the script in my previous response to properly use `ctx._source` to refer to the existing document.

In case you want to check the provided value, I would suggest using script parameters for that. Please check [these examples](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html#update-api-example).

---

<div class="post-metadata">

**Author:** ![ashish25](https://avatars.discourse-cdn.com/v4/letter/a/67e7ee/32.png) [@ashish25](https://discuss.elastic.co/u/ashish25)\
**Post date:** [April 8, 2024, 12:26pm UTC](https://discuss.elastic.co/t/preventing-document-update-based-on-field-values/356858/5 "2024-04-08T12:26:12Z")

</div>

Thanks @Carlos_D, I am able to prevent the document via your suggestion.

Is it possible to put this script at index pattern level ? So that the script will be executed for all the documents at the time of ingestion.

---

<div class="post-metadata">

**Author:** ![Carlos\_D](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carlos_d/32/126245_2.png) [@Carlos\_D](https://discuss.elastic.co/u/Carlos_D)\
**Post date:** [April 8, 2024, 2:52pm UTC](https://discuss.elastic.co/t/preventing-document-update-based-on-field-values/356858/6 "2024-04-08T14:52:24Z")

</div>

> Is it possible to put this script at index pattern level ? So that the script will be executed for all the documents at the time of ingestion.

You could use an [ingest pipeline](https://www.elastic.co/guide/en/elasticsearch/reference/current/ingest.html) to check that, using the [fail processor](https://www.elastic.co/guide/en/elasticsearch/reference/current/fail-processor.html).

Using the ingest pipeline as the final pipeline for the index will perform that check for all ingested documents.

---

<div class="post-metadata">

**Author:** ![ashish25](https://avatars.discourse-cdn.com/v4/letter/a/67e7ee/32.png) [@ashish25](https://discuss.elastic.co/u/ashish25)\
**Post date:** [April 8, 2024, 7:09pm UTC](https://discuss.elastic.co/t/preventing-document-update-based-on-field-values/356858/7 "2024-04-08T19:09:11Z")

</div>

Thank you very much for your response.

I created processor like below:

**Processor:**

PUT \_ingest/pipeline/my-pipeline\_test  
{  
"description": "My optional pipeline description",  
"processors": [  
{  
"drop": {  
"description": "Drop documents",  
"if": " **ctx.\_source?.status == 'Delivered**'"  
}  
}  
]  
}

And then trying to POST below document:

POST my-test-stream/ **\_doc** /123?pipeline=my-pipeline\_test  
{  
"my-keyword-field": "foo",  
"status": "Delivered"  
}

And after that when I am trying to update above document like:

POST my-test-stream/ **\_update** /123?pipeline=my-pipeline\_test  
{  
"my-keyword-field": "foo1",  
"status": "Initiate"  
}

It is giving me below error instead of dropping the document:  
{  
"error" : {  
"root\_cause" : [  
{  
"type" : "x\_content\_parse\_exception",  
"reason" : "[2:3] [UpdateRequest] unknown field [my-keyword-field]"  
}  
],  
"type" : "x\_content\_parse\_exception",  
"reason" : "[2:3] [UpdateRequest] unknown field [my-keyword-field]"  
},  
"status" : 400  
}

And also when I am trying to execute below commands, it got updated successfully instead of dropping the document -  
POST my-test-stream/ **\_doc** /123?pipeline=my-pipeline\_test  
{  
"my-keyword-field": "foo1",  
"status": "Initiate"  
}

Please let me know whether I am missing something.

---

<div class="post-metadata">

**Author:** ![ashish25](https://avatars.discourse-cdn.com/v4/letter/a/67e7ee/32.png) [@ashish25](https://discuss.elastic.co/u/ashish25)\
**Post date:** [April 10, 2024, 8:31am UTC](https://discuss.elastic.co/t/preventing-document-update-based-on-field-values/356858/8 "2024-04-10T08:31:25Z")

</div>

@Carlos_D It seems we can't query the existing document inside processor.  
So, is there any other workaround?

---

<div class="post-metadata">

**Author:** ![Carlos\_D](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carlos_d/32/126245_2.png) [@Carlos\_D](https://discuss.elastic.co/u/Carlos_D)\
**Post date:** [April 10, 2024, 1:50pm UTC](https://discuss.elastic.co/t/preventing-document-update-based-on-field-values/356858/9 "2024-04-10T13:50:16Z")

</div>

I'm able to check the ingested document, but not the existing one on the `if` section ☹ .

Another think I'd like to check is whether it's possible to do that in a `script` processor, as the context they receive is different that the `if` section. But I haven't been able to confirm that yet.

Besides that, I can't think of anything else that is Elasticsearch based - it would have to be dealt with at the client level.

---

<div class="post-metadata">

**Author:** ![ashish25](https://avatars.discourse-cdn.com/v4/letter/a/67e7ee/32.png) [@ashish25](https://discuss.elastic.co/u/ashish25)\
**Post date:** [April 12, 2024, 5:22am UTC](https://discuss.elastic.co/t/preventing-document-update-based-on-field-values/356858/10 "2024-04-12T05:22:17Z")

</div>

**script processor** is also not able to query existing document ☹  
We are having this check at client level but observing some edge scenario where our client is not able to handle it, so trying to explore this option.

Thanks @Carlos_D for all your response.
