0

My partial mapping is as follows (I am still using ES2.4):

"name": {
  "type": "string"
},
"misc": {
  "type": "object",
  "dynamic": "true",
  "properties": {}
}

Lets assume there is only one document in my storage, as follows

{
  "name": "foobar",
  "misc": {
    "201804": 1,
    "201805": 10,
    "201806": 2,
    "201807": 1,
    "201808": 9,
    "201809": 1,
    "201810": 8,
    "201811": 1,
    "201812": 11
  }
}

In my search query, I have groovy script, where I am trying to find the value of the key inside misc object

dynamic_key = ....my logical calculation to find key....
//assume the calculated value of dynamic_key is "201808"
kvalue = doc['misc'][dynamic_key].value //value of kvalue should be 9 as dynamic_key="201808"

I get this error type: "no_class_def_found_error", reason: "java/lang/Throwable" , so the dynamic key value selection fails. If I change this line kvalue = doc['misc'][dynamic_key].value to kvalue = doc['misc.201805].value then it finds the value correctly.

Any idea how to achieve what I am looking for?

JVK
  • 3,782
  • 8
  • 43
  • 67
  • Has the mapping not changed after indexing the given doc? – Praneeth Apr 03 '18 at 06:07
  • It's new index with this mapping always – JVK Apr 03 '18 at 14:12
  • You can remove the mapping for `misc` because while inserting the doc, ES tries to create one for you – Praneeth Apr 03 '18 at 16:34
  • Thank you Chandra. I did it and now the previous error is gone and I get this error now `type: "no_class_def_found_error", reason: "java/lang/Throwable"` , so the dynamic key value selection fails. If I change this line `kvalue = doc['misc'][dynamic_key].value` to `kvalue = doc['misc.201805].value` then it finds the value correctly. – JVK Apr 03 '18 at 16:47
  • Even though it's a json object, it is indexed as flat list of key-value pairs. Refer: https://www.elastic.co/guide/en/elasticsearch/reference/current/object.html#object – Praneeth Apr 03 '18 at 16:54
  • Ah..I see thanks for educating me :). So now the question boiled down to how to have this dynamic key in my script. – JVK Apr 03 '18 at 16:56
  • In groovy, if I can build string `misc.201805` , then I can use it. – JVK Apr 03 '18 at 16:58
  • @ChandraPraneethN I quickly tried `kk = 'misc.${dynamic_key}' lmin = doc[kk].value` and it seems strings are not concating and I get `No field found for [misc.${dynamic_key}] in mapping with types [mydocuments]"` – JVK Apr 03 '18 at 17:04
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/168139/discussion-between-jvk-and-chandra-praneeth-n). – JVK Apr 03 '18 at 17:06
  • Voila, I got it. I had to just change single quote to double `kk = "misc.${dynamic_key}"` . Thanks @ChandraPraneethN – JVK Apr 03 '18 at 17:07
  • Glad it worked! – Praneeth Apr 03 '18 at 17:09

0 Answers0