简体   繁体   English

如何聚合 Elasticsearch 中的位字段?

[英]How do I aggregate a bit field in Elasticsearch?

I have a 60-bit (minutes in an hour) field that I would like to aggregate with a bit-wise or operator so that the end result is a bit set if any of the contributing values had that bit set.我有一个 60 位(一小时中的分钟数)字段,我想使用按位or运算符进行聚合,以便如果任何贡献值都设置了该位,则最终结果是一个位设置。

The representation of the bit field isn't determined yet, but it would be nice to have it be somewhat compact as there are a lot of records entering the aggregation.位字段的表示尚未确定,但最好让它有点紧凑,因为有很多记录进入聚合。

Say we have three documents with a bitfield taking the binary values: 0001 , 1001 , 1100 .假设我们有三个文档,其位域采用二进制值: 000110011100 A bit-wise or aggregation would combine them to the value 1101 - a bit is true if that bit is set in any value.按位or聚合会将它们组合为值1101 - 如果该位设置为任何值,则该位为真。 Much like a sum aggregation but bit-wise operation instead.很像求和聚合,而是按位运算。

I've considered a bit-position array (position present if bit is set) but it gets a little verbose.我考虑过一个位位置数组(如果设置了位,则位置存在),但它有点冗长。

Scripted metric aggregation could be possible but I'm a little at a loss of how to implement it.脚本化的度量聚合是可能的,但我有点不知道如何实现它。

Scripted metric agg would look something like this broken painless code:脚本化的度量 agg 看起来像这个破碎的无痛代码:

"aggs": {
    "profit": {
      "scripted_metric": {
        "init_script": "minagg = 0L", 
        "map_script": "minagg = minagg | minutes",
     }
   }
 }

Thanks for looking.感谢您的关注。

The scripted metric aggregation handles this.脚本化的度量聚合处理这个。 With an index with the following mapping...使用具有以下映射的索引...

"mappings": {
  "default": {
    "dynamic": "strict",
    "properties": {
      "bitfield": {
        "type": "long"
      }
    }
  }
}

... this aggregation delivers the or'd values: ...此聚合提供 or'd 值:

POST /bitfield/_search
{
  "query": {
    "match_all": {}
  },
  "size": 0,
  "aggs": {
    "orfield": {
      "scripted_metric": {
        "init_script": "state.agg = 0L",
        "map_script": "state.agg |= doc.bitfield.value",
        "combine_script": "return state.agg",
        "reduce_script": "long total = 0L; for (a in states) {total |= a} return total"
      }
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM