简体   繁体   中英

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.

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 . A bit-wise or aggregation would combine them to the value 1101 - a bit is true if that bit is set in any value. 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:

"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:

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"
      }
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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