How to access nested property in script_fields on elastic 5.x

I need to iterate over nested objects of painless script fields for my query, but the doc ['nestedProperty.property'] notation did not give me a value, nor does it use the doc ['nestedProperty.property'] array notation] [0]

Any idea on how to use this?

EDIT

Document example:

{
    "neestedProperty": [
      {
        "property": 12,
        "innerNeestedProperty": {
          "innerProperty1": 45,
          "innerProperty2": -45
        }
      }
    ]
}

Request example:

{
  "query": {
    match_all: {}
  },
  "script_fields": {
    "scripted": {
      "script": {
        "inline": "doc['neestedProperty.property'] * params.multiplier",
        "params": {
          "multiplier": 100
        },
        "lang": "painless"
      }
    }
  }
}
+4
source share
2 answers

doc-notation obviously does not work with nested objects, but you can directly access _source-object, as Horst Seirer pointed out.

, _source , ; ctx -variable . scripted_fields params._source. (, )

_source, get -notation, . , , params._source.nestedProperty[0].property .

, . , - :

def returnval=[];
for (nested in params._source.nestedProperty) {
  returnval.add(nested['property']*params.multiplier)
}
return returnval;

, .

, , , , , .

+1

_source LinkedHashMap (. ). , get. , .

- :

ctx._source.some_field

, .

neestedProperty (/ArrayList) , . LinkedHashMap.

:

ArrayList nestedObjects = ctx._source.get('neestedProperty'); 

for(o IN nestedObjects) { 
    o.get('property') = o.get('property')*params.multiplier; 
}
0

Source: https://habr.com/ru/post/1664507/


All Articles