Bit arrays usage and filtering in Elasticsearch

2019-07-14 18:34发布

问题:

I have a bit array. And I want to filter based on if certain bits are ON or OFF. Looking at the Elasticsearch 2.3 docs, I don't see anything about bitarrays.

But it seems I can use an Array of Booleans or a Binary field.

Example: Let's say I have 2 documents each with a bit array field. Doc1 has 011100 and Doc2 has 00001 in that field. And I want to filter by 011000 which in this case only gives Doc1.

Any ideas how to do this in Elasticsearch?

Thanks you.

Edit: Another idea:

If I turn the bit array into many Bool fields, then it works. The doc might look ugly but it works. Basically if the bit array is 32 bit, then I will have 32 bool fields. Is that the best way to implement this?

回答1:

if you could change it to an array containing the bit-index which are set. That is 011100 would be[ 1 , 2 ,3 ] and then use a terms query to do an or or a must query for and

Example :

a)  document with "111" 

put test/test/1
{
   "bit_position" : [
        1,
        2,
        3
    ]
}

b) document with 010
put test/test/2
{
   "bit_position": [
      2
   ]
}

c) or-ing with 101

post test/_search
{
    "query": {
        "terms": {
           "bit_position": [
              1,
              3
           ]
        }
    }
}