Using Painless script to save integer range dynamically when reindexing

Hi,
I am having trouble to dynamically add integer range (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 :

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.

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:

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:

"pageno_range_gte_field": "pageno",
"pageno_range_lte_field": "pageno",

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

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