# Using Painless script to save integer range dynamically when reindexing

**URL:** <https://discuss.elastic.co/t/using-painless-script-to-save-integer-range-dynamically-when-reindexing/199856>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [September 17, 2019, 4:06pm UTC](https://discuss.elastic.co/t/using-painless-script-to-save-integer-range-dynamically-when-reindexing/199856 "2019-09-17T16:06:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anurup\_Borah](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anurup_borah/32/54344_2.png) [@Anurup\_Borah](https://discuss.elastic.co/u/Anurup_Borah)\
**Post date:** [September 17, 2019, 4:06pm UTC](https://discuss.elastic.co/t/using-painless-script-to-save-integer-range-dynamically-when-reindexing/199856/1 "2019-09-17T16:06:59Z")

</div>

Hi,  
I am having trouble to dynamically add integer range ([https://www.elastic.co/guide/en/elasticsearch/reference/current/range.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/range.html)) to a field dynamically when I am reindexing to a new Elasticsearch cluster.

I have a new field mapping as follows

```
"pageno_range": {
            "type": "integer_range",
            "boost": 4
   },
   "pageno": {
            "type": "keyword",
            "boost": 4
    } 

```

so When I am trying to reindex for an index , I want to add integer range dynamically to the pageno\_range field.The script part for the reindexing are as follows

```
 "script": {
    "lang": "painless",
    "source": "ctx._source.type = params.index_type;ctx._routing = params.index_route + ctx._source.year;if (ctx._source.pageno ==~ /\\d+/) { ctx._source.pageno_range = params.pageno_range_obj }",
    "params": {
       "index_route": "es_mag-",
       "index_type" : "mag",
       "greater_than" : "gte",
       "less_than" : "lte",
       "pageno_range_obj": {
            "gte" : "ctx._source.pageno",
            "lte" : "ctx._source.pageno"
        }
     }
  }

```

But I am not able to dynamically insert into the **pageno\_range** field.  
I am getting the below issue :

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/7/1/715a25ff090a885b93da2cdc2c4d5b8be1910c20.png)

Please suggest me how I can dynamically insert the pageno\_range value for "gte" and "lte" with the pageno field.

Is it possible to dynamically add value to the params tag for the script?

Please guys need some help and suggestion.

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [September 18, 2019, 12:48am UTC](https://discuss.elastic.co/t/using-painless-script-to-save-integer-range-dynamically-when-reindexing/199856/2 "2019-09-18T00:48:31Z")

</div>

Params are give to the painless script as the raw json. Variable substitutions are not possible within them. I would instead put the field name you want to copy in the params, and then use that as a lookup from the source. For example:

```auto
ctx._source.pageno_range = ['gte': ctx._source[params.pageno_range_gte_field], 'lte': ctx._source[params.pageno_range_lte_field[\]];

```

Where in params you have:

```auto
"pageno_range_gte_field": "pageno",
"pageno_range_lte_field": "pageno",

```

---

<div class="post-metadata">

**Author:** ![Anurup\_Borah](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anurup_borah/32/54344_2.png) [@Anurup\_Borah](https://discuss.elastic.co/u/Anurup_Borah)\
**Post date:** [September 18, 2019, 7:20am UTC](https://discuss.elastic.co/t/using-painless-script-to-save-integer-range-dynamically-when-reindexing/199856/3 "2019-09-18T07:20:27Z")

</div>

> [@rjernst](#):
>
> ['gte': ctx.\_source[params.pageno\_range\_gte\_field], 'lte': ctx.\_source[params.pageno\_range\_lte\_field];

Thanks a lot rjernst , its working now . Thanks for the explaination too.  
I have used the script as below :

```
"script": {
    "inline": "ctx._source.type = params.index_type;ctx._routing = params.index_route + ctx._source.year;if ((ctx._source.pageno ==~ /\\d+/) && (ctx._source.pageno != null) ) { ctx._source.pageno_range = [\"gte\": ctx._source.pageno, \"lte\": ctx._source.pageno] }",
    "lang": "painless",
    "params": {
       "index_route": "es_mag-",
       "index_type" : "mag"
     }
  }

```

---

<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:** [October 16, 2019, 7:20am UTC](https://discuss.elastic.co/t/using-painless-script-to-save-integer-range-dynamically-when-reindexing/199856/4 "2019-10-16T07:20:27Z")

</div>

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