0

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.

JVK
  • 3,782
  • 8
  • 43
  • 67
  • What do the individual components, ie `doc['los.start']` return? – tim_yates Mar 27 '18 at 20:47
  • If I just `return doc['los.start']`, then I get error `class_cast_exception` – JVK Mar 27 '18 at 20:59
  • it is not possible to access nested values using doc values. Nested documents are stored as separate documents and a script can only be executed in the context of the parent document, hence you can only access nested data via `_source` but not `doc` – Val Mar 28 '18 at 04:15
  • @Val so you are saying.. this will this work instead `los_map = [_source['los.start'], _source['los.max'], _source['los.min']].transpose()` ? – JVK Mar 28 '18 at 05:24
  • Using `params._source` – Val Mar 28 '18 at 05:31

0 Answers0