# Doc values are unmodifiable while using scripted metric

**URL:** <https://discuss.elastic.co/t/doc-values-are-unmodifiable-while-using-scripted-metric/168756>\
**Category:** Elasticsearch\
**Created:** [February 17, 2019, 10:00pm UTC](https://discuss.elastic.co/t/doc-values-are-unmodifiable-while-using-scripted-metric/168756 "2019-02-17T22:00:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nmeaux](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nmeaux/32/31380_2.png) [@nmeaux](https://discuss.elastic.co/u/nmeaux)\
**Post date:** [February 17, 2019, 10:00pm UTC](https://discuss.elastic.co/t/doc-values-are-unmodifiable-while-using-scripted-metric/168756/1 "2019-02-17T22:00:59Z")

</div>

Hello,

I am using a scripted metric, which is :

```
  "scripted_metric": {
"init_script": "state.nodes = new HashMap();",
"map_script": "int jtime = 1538438400 ; for (int i = jtime; i < jtime+2; ++i) {if (doc.time_start.value < i && doc.time_end.value > i) {state.nodes[i] = doc.nodelist_expand}}",
"combine_script": "def result = new HashMap(); for (entry in state.nodes.entrySet()) { if (result.containsKey(entry.getKey())) { for (n in entry.getValue()) { result[entry.getKey()].add(n); } } else { result[entry.getKey().toString()]=entry.getValue(); }} return result;",
"reduce_script": "def result = new HashMap(); for (s in states) { for (entry in s.entrySet()) { if (result.containsKey(entry.getKey())) { for (n in entry.getValue()) { result[entry.getKey()].add(n); } } else { result[entry.getKey().toString()]=entry.getValue(); } } } return result;" }

```

And i get this error :

```
{
  "error": {
    "root_cause": [],
    "type": "search_phase_execution_exception",
    "reason": "",
    "phase": "fetch",
    "grouped": true,
    "failed_shards": [],
    "caused_by": {
      "type": "script_exception",
      "reason": "runtime error",
      "script_stack": [
        "org.elasticsearch.index.fielddata.ScriptDocValues.add(ScriptDocValues.java:97)",
        "java.util.AbstractList.add(AbstractList.java:108)",
        "result[entry.getKey()].add(n); } } else { ",
        " ^---- HERE"
      ],
      "script": "def result = new HashMap(); for (s in states) { for (entry in s.entrySet()) { if (result.containsKey(entry.getKey())) { for (n in entry.getValue()) { result[entry.getKey()].add(n); } } else { result[entry.getKey().toString()]=entry.getValue(); } } } return result;",
      "lang": "painless",
      "caused_by": {
        "type": "unsupported_operation_exception",
        "reason": "doc values are unmodifiable"
      }
    }
  },
  "status": 503
} 

```

I dont understand it, i dont try to modify a doc values, just a hashmap which is define in the reduce\_script in order to make some kinds of calculation.  
reduce\_script & combine\_script are very similar, but as combine script is working well, reduce, not.  
I am using elasticsearch : 6.6.0 , could this be considered as a bug, or something i did not understand ?  
thx

rgds

Nicolas

---

<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:** [March 17, 2019, 10:01pm UTC](https://discuss.elastic.co/t/doc-values-are-unmodifiable-while-using-scripted-metric/168756/2 "2019-03-17T22:01:00Z")

</div>

Closed on March 17, 2019, 10:01pm UTC

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