# Elasticsearch groovy script not working as expected

**URL:** <https://discuss.elastic.co/t/elasticsearch-groovy-script-not-working-as-expected/125816>\
**Category:** Elasticsearch\
**Created:** [March 27, 2018, 8:37pm UTC](https://discuss.elastic.co/t/elasticsearch-groovy-script-not-working-as-expected/125816 "2018-03-27T20:37:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pungent](https://avatars.discourse-cdn.com/v4/letter/p/a698b9/32.png) [@pungent](https://discuss.elastic.co/u/pungent)\
**Post date:** [March 27, 2018, 8:37pm UTC](https://discuss.elastic.co/t/elasticsearch-groovy-script-not-working-as-expected/125816/1 "2018-03-27T20:37:31Z")

</div>

My partial mapping of an index `listing` elasticsearch 2.5 (I know I have to upgrade to newer version and start using painless, let's keep that aside for this question)

```
"name": { "type": "string" },
"los": {                      
  "type": "nested",                     
  "dynamic": "strict",                      
  "properties": {                     
    "start": { "type": "date", "format": "yyyy-MM" },
    "max": { "type": "integer" },
    "min": { "type": "integer" }
  }                     
}

```

I have only `one` document in my storage and that is as follows:

```
{
  "name": 'foobar',
  "los": [{
          "max": 12,
          "start": "2018-02",
          "min": 1
      },
      {
          "max": 8,
          "start": "2018-03",
          "min": 3
      },
      {
          "max": 10,
          "start": "2018-04",
          "min": 2
      },
      {
          "max": 12,
          "start": "2018-05",
          "min": 1
      }
  ]
}

```

I have a a groovy script in my elastic search query as follows:

```
los_map = [doc['los.start'], doc['los.max'], doc['los.min']].transpose()
return los_map.size()

```

This groovy query ALWAYS returns 0, which is not possible, as I have one document, as mentioned above (even if I add multiple documents, it still returns 0) and `los` field is guaranteed to be present in every doc with multiple objects in it. So it seems the transpose which I am doing is not working correctly?

I also tried changing this line `los_map = [doc['los.start'], doc['los.max'], doc['los.min']].transpose()` to `los_map = [doc['los'].start, doc['los'].max, doc['los'].min].transpose()` then I get this error `"No field found for [los] in mapping with types [listing]"`

Does anyone have any idea how to get the transpose work?

By the way, if you are curious, my complete script is as follows:

```
losMinMap = [:]
losMaxMap = [:]
los_map = [doc['los.start'], doc['los.max'], doc['los.min']].transpose()
los_map.each {st, mx, mn ->
  losMinMap[st] = mn
  losMaxMap[st] = mx
}
return los_map['2018-05']

```

Thank you in advance.

---

<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:** [April 24, 2018, 8:37pm UTC](https://discuss.elastic.co/t/elasticsearch-groovy-script-not-working-as-expected/125816/2 "2018-04-24T20:37:58Z")

</div>

Closed on April 24, 2018, 8:37pm UTC

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