ElasticSearch/Painless: How do I access/sum all va

2019-06-08 00:51发布

问题:

I've been looking at aggregations, and at scripting using painless, and I am not able to figure out how to iterate/sum over all values in an object.

Example:

My mapping looks like

"field1": {
  "properties": {
    "subfield1": {
      "type": "float"
    },
    "subfield2": {
      "type": "float"
    },
    "subfield3": {
      "type": "float"
    }
  }
}

Let's assume my data looks like this:

{
  "field1" : {
    "subfield1": 50.0,
    "subfield2": 20.5,
    "subfield3": 30.5
  }
}

I want to perform a range query on 50.0 + 20.5 + 30.5, or, basically, access all the values within the field1 object in some way.

Aggregations do not allow me to use wild-cards in fields. I was looking at the code for LeafDocLookup (used internally for painless), and I see that the relevant methods are disabled.

I've managed to write the script like this:

"query": {
  "script": {
    "script": {
      "inline": "return (doc['field1.subfield1'].value + doc['field1.subfield2'].value + doc['field1.subfield3'].value > 50);",
      "lang": "painless"
    }
  }
}

but this is obviously sub-optimal, and doesn't solve the main issue of dynamic keys.

回答1:

I finally figured it out! It isn't available in the doc object within elasticsearch, but it is available within _ctx.source.

So, I can access the object as a Java HashMap object if I use params['_source']

"query": {
  "script": {
    "script": {
      "inline": "float sum = 0.0f; for (float v: params['_source'].values()) { sum += v; } return (sum > 50);",
      "lang": "painless"
    }
  }
}