# Transform script when indexing data

**URL:** <https://discuss.elastic.co/t/transform-script-when-indexing-data/21356>\
**Category:** Elasticsearch\
**Created:** [December 22, 2014, 5:35pm UTC](https://discuss.elastic.co/t/transform-script-when-indexing-data/21356 "2014-12-22T17:35:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nwood888](https://avatars.discourse-cdn.com/v4/letter/n/71c47a/32.png) [@nwood888](https://discuss.elastic.co/u/nwood888)\
**Post date:** [December 22, 2014, 5:35pm UTC](https://discuss.elastic.co/t/transform-script-when-indexing-data/21356/1 "2014-12-22T17:35:08Z")

</div>

Hello,

I'm trying to implement a transform script when my data is being indexed,  
but it doesn't seem to have any affect. I basically want to multiply a  
date\_created field by 1000.

I've tried adding this to my mapping

* * *

```
    "transform" : {
        "script" : "ctx._source['date_created'] = 

```

## ctx.\_source['date\_created'] \* 1000;", "lang": "groovy" }, "properties" : { ...

When I index documents, I don't get any errors, but I also can't tell that  
the script actually ran or changed the data. How would I troubleshoot  
whether this script is even firing, and if so, why it's not updating the  
data?

Best,

Nick

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/5cf1ca40-3ae4-4555-ae75-733a1d8d4571%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/5cf1ca40-3ae4-4555-ae75-733a1d8d4571%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:** [December 22, 2014, 5:46pm UTC](https://discuss.elastic.co/t/transform-script-when-indexing-data/21356/2 "2014-12-22T17:46:13Z")

</div>

I'd add a new field and check for it. Or do a search that won't find  
anything unless it took effect. The document is stored untransformed so  
just fetching the document won't show you anything.  
On Dec 22, 2014 12:35 PM, "Nick Wood" [nwood888@gmail.com](mailto:nwood888@gmail.com) wrote:

> Hello,
> 
> I'm trying to implement a transform script when my data is being indexed,  
> but it doesn't seem to have any affect. I basically want to multiply a  
> date\_created field by 1000.
> 
> I've tried adding this to my mapping
> 
> * * *
> 
> ```
> "transform" : {
> "script" : "ctx._source['date_created'] =
> 
> ```
> 
> ## ctx.\_source['date\_created'] \* 1000;", "lang": "groovy" }, "properties" : { ...
> 
> When I index documents, I don't get any errors, but I also can't tell that  
> the script actually ran or changed the data. How would I troubleshoot  
> whether this script is even firing, and if so, why it's not updating the  
> data?
> 
> Best,
> 
> Nick
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/5cf1ca40-3ae4-4555-ae75-733a1d8d4571%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/5cf1ca40-3ae4-4555-ae75-733a1d8d4571%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/5cf1ca40-3ae4-4555-ae75-733a1d8d4571%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/5cf1ca40-3ae4-4555-ae75-733a1d8d4571%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAPmjWd2WPT\_3zSGLFzq12h2a8Zdzv-C8EgKnHT2g1hs3X6YneA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAPmjWd2WPT_3zSGLFzq12h2a8Zdzv-C8EgKnHT2g1hs3X6YneA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![vineeth\_mohan\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vineeth_mohan_2/32/747_2.png) [@vineeth\_mohan\_2](https://discuss.elastic.co/u/vineeth_mohan_2)\
**Post date:** [December 22, 2014, 6:39pm UTC](https://discuss.elastic.co/t/transform-script-when-indexing-data/21356/3 "2014-12-22T18:39:11Z")

</div>

Hi Nick ,

This wont change the \_source , so no point in looking there.  
Try script field to read the data of this field from store ( if enabled )  
or reverse index.

Thanks  
Vineeth

On Mon, Dec 22, 2014 at 11:05 PM, Nick Wood [nwood888@gmail.com](mailto:nwood888@gmail.com) wrote:

> Hello,
> 
> I'm trying to implement a transform script when my data is being indexed,  
> but it doesn't seem to have any affect. I basically want to multiply a  
> date\_created field by 1000.
> 
> I've tried adding this to my mapping
> 
> * * *
> 
> ```
> "transform" : {
> "script" : "ctx._source['date_created'] =
> 
> ```
> 
> ## ctx.\_source['date\_created'] \* 1000;", "lang": "groovy" }, "properties" : { ...
> 
> When I index documents, I don't get any errors, but I also can't tell that  
> the script actually ran or changed the data. How would I troubleshoot  
> whether this script is even firing, and if so, why it's not updating the  
> data?
> 
> Best,
> 
> Nick
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/5cf1ca40-3ae4-4555-ae75-733a1d8d4571%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/5cf1ca40-3ae4-4555-ae75-733a1d8d4571%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/5cf1ca40-3ae4-4555-ae75-733a1d8d4571%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/5cf1ca40-3ae4-4555-ae75-733a1d8d4571%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAGdPd5nOVjnbQgLBCqs4SfY93x41b3x1S3omMH0fwxowsH836w%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAGdPd5nOVjnbQgLBCqs4SfY93x41b3x1S3omMH0fwxowsH836w%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![nwood888](https://avatars.discourse-cdn.com/v4/letter/n/71c47a/32.png) [@nwood888](https://discuss.elastic.co/u/nwood888)\
**Post date:** [December 28, 2014, 12:09am UTC](https://discuss.elastic.co/t/transform-script-when-indexing-data/21356/4 "2014-12-28T00:09:26Z")

</div>

Thanks for the tips.

In case anyone else is looking for a similar solution, my problem had to do  
with types. The mapping that ended up working is:

{  
"test" : {  
"transform" : {  
"script" : "ctx.\_source['date\_created'] =  
ctx.\_source['date\_created'].toLong() \* 1000",  
"lang": "groovy"  
},  
"properties" : {  
"date\_created" : {"type" : "date"},  
}  
}  
}

Note the "toLong()".

Also, once a script is modified, it appears as though the type has to be  
deleted and the mapping re-applied for the changes to take effect.

Nick

On Monday, December 22, 2014 10:35:08 AM UTC-7, Nick Wood wrote:

> Hello,
> 
> I'm trying to implement a transform script when my data is being indexed,  
> but it doesn't seem to have any affect. I basically want to multiply a  
> date\_created field by 1000.
> 
> I've tried adding this to my mapping
> 
> * * *
> 
> ```
> "transform" : {
> "script" : "ctx._source['date_created'] = 
> 
> ```
> 
> ## ctx.\_source['date\_created'] \* 1000;", "lang": "groovy" }, "properties" : { ...
> 
> When I index documents, I don't get any errors, but I also can't tell that  
> the script actually ran or changed the data. How would I troubleshoot  
> whether this script is even firing, and if so, why it's not updating the  
> data?
> 
> Best,
> 
> Nick

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/7b4d0870-aba4-4405-8100-5ec127de72f1%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/7b4d0870-aba4-4405-8100-5ec127de72f1%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 12:41am UTC](https://discuss.elastic.co/t/transform-script-when-indexing-data/21356/5 "2017-07-06T00:41:46Z")

</div>


