# Using scripts to prevent data rewriting

**URL:** <https://discuss.elastic.co/t/using-scripts-to-prevent-data-rewriting/195371>\
**Category:** Elasticsearch\
**Created:** [August 15, 2019, 4:05pm UTC](https://discuss.elastic.co/t/using-scripts-to-prevent-data-rewriting/195371 "2019-08-15T16:05:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![korjavin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/korjavin/32/52373_2.png) [@korjavin](https://discuss.elastic.co/u/korjavin)\
**Post date:** [August 15, 2019, 4:05pm UTC](https://discuss.elastic.co/t/using-scripts-to-prevent-data-rewriting/195371/1 "2019-08-15T16:05:31Z")

</div>

My index has a field `DateSeen` with the timestamp. .

I want to prevent rewriting of index documents if DateSeen is before one that stored.

I try to achieve this with a script like this:

```auto
POST _scripts/check_date
{"script":{"lang":"painless","source":"if(ctx._source.dateSeen == null || (Instant.parse(ctx._source.dateSeen)).isBefore(Instant.parse(params.dateSeen))) ctx._source.putAll(params)"}}

```

But it looks like not working solution.

Help me please find a way to achieve this behavior.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 16, 2019, 12:59pm UTC](https://discuss.elastic.co/t/using-scripts-to-prevent-data-rewriting/195371/2 "2019-08-16T12:59:05Z")

</div>

can you provide a fully fledged reproducible example including indexation of sample documents so one can reproduce?

Also the Elasticsearch version you are testing against would be good to know.

---

<div class="post-metadata">

**Author:** ![korjavin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/korjavin/32/52373_2.png) [@korjavin](https://discuss.elastic.co/u/korjavin)\
**Post date:** [August 17, 2019, 8:37am UTC](https://discuss.elastic.co/t/using-scripts-to-prevent-data-rewriting/195371/3 "2019-08-17T08:37:47Z")

</div>

Yes, this is my experiment:

```auto
PUT /test_index
{
    "aliases": {},             
    "mappings": {              
        "dynamic": "strict",   
        "properties": {        
            "dateSeen": {
                "type": "date"
            }
        }
    }
}

POST _scripts/test_last_script
{"script":{"lang":"painless","source":"if(ctx._source.dateSeen == null || (Instant.parse(ctx._source.dateSeen)).isBefore(Instant.parse(params.dateSeen))) ctx._source.putAll(params)"}}

put /test_index/_doc/1 
{
  
  "dateSeen": "2019-06-26T22:23:42Z"
}

put /test_index/_doc/1 
{
  
  "dateSeen": "2019-06-20T22:23:42Z"
}

GET test_index/_search
{
  "query": {
    "match_all": {}
  }
}

```

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 19, 2019, 6:56am UTC](https://discuss.elastic.co/t/using-scripts-to-prevent-data-rewriting/195371/4 "2019-08-19T06:56:37Z")

</div>

this is not a complete example (or maybe the issue). You are just storing a script, but the script is not executed during indexed. You may want to use a [script processor](https://www.elastic.co/guide/en/elasticsearch/reference/7.3/script-processor.html) before indexing that is running the above script.

---

<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:** [September 16, 2019, 6:56am UTC](https://discuss.elastic.co/t/using-scripts-to-prevent-data-rewriting/195371/5 "2019-09-16T06:56:39Z")

</div>

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