Hi, I'm having some trouble with a nested scripted query. I'll just quickly explain what I'm trying to achieve:
I have objects like:
{
"metaData": {
"meta": [
{
"key": "width",
"value": 10
},
{
"key": "height",
"value": 104
},
{
"key": "etc",
"value": 1056
}
]
}
}
This is the mapping:
"properties": {
"metaData": {
"properties": {
"meta": {
"type": "nested",
"properties": {
"key": {
"type": "keyword"
},
"value": {
"type": "long"
}
}
}
}
}
}
Now I need a query that will only return documents where f.e. the value field of the nested object with key "width" is greater than the value field of the nested object with key "height".
I am attempting this with a nested query and a script query like this:
{
"query": {
"nested": {
"path": "metaData.meta",
"query": {
"script": {
"script": "if (doc['metaData.meta'].length < 1) return false; int width; int height; for(int i=0; i< doc['metaData.meta'].length; i++) { if (doc['metaData.meta'][i].key == 'width') {width=doc['metaData.meta'][i].valueDec;} else if (doc['metaData.meta'][i].key == 'height') {height=doc['metaData.meta'][i].valueDec;} } return width > height"
}
}
}
}
}
Now with this approach i would need access to each nested object in the for loop, but this error is thrown: No field found for [metaData.meta] in mapping with types [type].
Anyone who knows how I could better approach this without changing the mapping/ structure of the document?