ElasticSearch / Painless:如何访问/汇总对象中的所有值 [英] ElasticSearch/Painless: How do I access/sum all values in an object

查看:1156
本文介绍了ElasticSearch / Painless:如何访问/汇总对象中的所有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究聚合,使用无痛编写脚本,我无法弄清楚如何对对象中的所有值进行迭代/求和。

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.

示例:

我的映射看起来像

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

假设我的数据如下:

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

我想在 50.0 + 20.5 + 30.5 上执行范围查询,或者,基本上,访问 field1 某种方式的对象。

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.

聚合不允许我在字段中使用通配符。
我是查看 LeafDocLookup 的代码(内部用于无痛),我看到相关方法被禁用。

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.

推荐答案

我终于明白了!它在elasticsearch中的 doc 对象中不可用,但 _ctx.source

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

因此,如果我使用<$ c,我可以将对象作为Java HashMap 对象访问$ c> params ['_ 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"
    }
  }
}

这篇关于ElasticSearch / Painless:如何访问/汇总对象中的所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆