# Add Column at runtime through inline scipt : Date difference

**URL:** <https://discuss.elastic.co/t/add-column-at-runtime-through-inline-scipt-date-difference/44011>\
**Category:** Elasticsearch\
**Created:** [March 10, 2016, 10:41am UTC](https://discuss.elastic.co/t/add-column-at-runtime-through-inline-scipt-date-difference/44011 "2016-03-10T10:41:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![pranavtiwary](https://avatars.discourse-cdn.com/v4/letter/p/ea5d25/32.png) [@pranavtiwary](https://discuss.elastic.co/u/pranavtiwary)\
**Post date:** [March 10, 2016, 10:41am UTC](https://discuss.elastic.co/t/add-column-at-runtime-through-inline-scipt-date-difference/44011/1 "2016-03-10T10:41:48Z")

</div>

I have a situation, where i need to calculate a value based on if the column is present and add this to all document and it should be indexed.

**How can I add a column to my indexed document by using doc**. ctx and fields are not working. What are other api that a doc have

I have following script :  
**SCRIPT**

```auto
GET /scm_master/suborder_details/_search?
{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
             {"exists": { "field": "created_on" }},
             { "missing": {"field": "new_field"}
            }
          ]
        }
      }
    }
  },
   "script_fields" : {
     "new_field" : {
       "script" :"def duration = groovy.time.TimeCategory.minus(new java.util.Date(doc['updated_on'].value), new java.util.Date(doc['created_on'].value));;"
     }
  }
}

```

**output**

```auto
{
  "took": 7,
  "timed_out": false,
  "_shards": {
    "total": 10,
    "successful": 10,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "scm_master",
        "_type": "suborder_details",
        "_id": "subOrderCode5",
        "_score": 1,
        "fields": {
          "new_field": [
            "0.207 seconds"
          ]
        }
      }
    ]
  }
}

```

it runs successfully, but a new field is not created.

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [March 10, 2016, 12:02pm UTC](https://discuss.elastic.co/t/add-column-at-runtime-through-inline-scipt-date-difference/44011/2 "2016-03-10T12:02:32Z")

</div>

`script_fields` just adds the field to the search result which it looks like it did properly. I think what you are looking for is update by query which is a feature coming in 2.3 and 5.0.

---

<div class="post-metadata">

**Author:** ![pranavtiwary](https://avatars.discourse-cdn.com/v4/letter/p/ea5d25/32.png) [@pranavtiwary](https://discuss.elastic.co/u/pranavtiwary)\
**Post date:** [March 10, 2016, 1:57pm UTC](https://discuss.elastic.co/t/add-column-at-runtime-through-inline-scipt-date-difference/44011/3 "2016-03-10T13:57:00Z")

</div>

Thanks Nik.

Is there any solution available for 2.2, any hack ☹

When should we expect 2.3.

Also I have one query : even if we update a document via script is it that the old document is deleted and updated document is re indexed like fresh. And so updating a doc is more time taking?

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [March 10, 2016, 3:05pm UTC](https://discuss.elastic.co/t/add-column-at-runtime-through-inline-scipt-date-difference/44011/4 "2016-03-10T15:05:10Z")

</div>

> [@pranavtiwary](#):
>
> Is there any solution available for 2.2, any hack ☹

Use the update api on all of the documents you want to change? [Update API | Elasticsearch Guide [8.11] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html#_scripted_updates)

> [@pranavtiwary](#):
>
> Also I have one query : even if we update a document via script is it that the old document is deleted and updated document is re indexed like fresh. And so updating a doc is more time taking?

Updates are basically atomic deletes and adds. Using the script just saves you a round trip back to the client.

> [@pranavtiwary](#):
>
> When should we expect 2.3.

A couple of weeks I think.

---

<div class="post-metadata">

**Author:** ![pranavtiwary](https://avatars.discourse-cdn.com/v4/letter/p/ea5d25/32.png) [@pranavtiwary](https://discuss.elastic.co/u/pranavtiwary)\
**Post date:** [March 10, 2016, 4:04pm UTC](https://discuss.elastic.co/t/add-column-at-runtime-through-inline-scipt-date-difference/44011/5 "2016-03-10T16:04:42Z")

</div>

Thanks Again Nik.

Yes at this moment, I am using update api only for all documents. **But I am having an issue.**

When I am updating this document via below script, then at same time may be there is another update at that index via java update api.

**Issue** : sometimes i see the new fields get created but some times not, but if i re run the script, it gets created . And we run query only well the condition written in script meets. (But still we have if condition in script to be double sure). Is it due to the case that may be at same time some other thread trying to update the same document ?

I run below script using java bulk processor api.

**//my script**

> if( ctx.\_source.containsKey('order\_created\_date') && ctx.\_source.containsKey('sp\_awb\_uploaded\_status\_date') ){def duration = groovy.time.TimeCategory.minus(new java.util.Date("5237683"), new java.util.Date("12321223")); ctx.\_source.order\_to\_shipped = duration.days;}

**//My Java function**

> for (ElasticDataDTO dto : dtos) {  
> try {  
> UpdateRequest updatReq = new UpdateRequest(indexName, indexType, dto.getSubOrderCode());  
> if(null==params)  
> params= new HashMap\<String, Object\>();  
> Script scriptObj = new Script(script, ScriptType.INLINE, null,params);  
> updatReq.script(scriptObj);  
> bulkProcessor.add(updatReq);  
> }catch (Exception e) {  
> throw new RuntimeException(e);  
> }  
> }

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [March 10, 2016, 4:36pm UTC](https://discuss.elastic.co/t/add-column-at-runtime-through-inline-scipt-date-difference/44011/6 "2016-03-10T16:36:36Z")

</div>

You should wrap code blocks in ``` so they are readable, please.

You should check the responses on those update requests - if you see any version conflicts then you know that an in flight conflict caused the trouble. There is a `retry_on_conflict` parameter you can use to retry the script.

---

<div class="post-metadata">

**Author:** ![pranavtiwary](https://avatars.discourse-cdn.com/v4/letter/p/ea5d25/32.png) [@pranavtiwary](https://discuss.elastic.co/u/pranavtiwary)\
**Post date:** [March 10, 2016, 5:05pm UTC](https://discuss.elastic.co/t/add-column-at-runtime-through-inline-scipt-date-difference/44011/7 "2016-03-10T17:05:57Z")

</div>

Thanks Nik.

I will try the `retry_on_conflict`.

---

<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:** [July 5, 2017, 11:09pm UTC](https://discuss.elastic.co/t/add-column-at-runtime-through-inline-scipt-date-difference/44011/8 "2017-07-05T23:09:23Z")

</div>


