Using scripts to prevent data rewriting

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:

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.

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.

Yes, this is my experiment:

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": {}
  }
}

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 before indexing that is running the above script.

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