# Bitwise logical operation in filter

**URL:** <https://discuss.elastic.co/t/bitwise-logical-operation-in-filter/109982>\
**Category:** Elasticsearch\
**Created:** [December 1, 2017, 8:25pm UTC](https://discuss.elastic.co/t/bitwise-logical-operation-in-filter/109982 "2017-12-01T20:25:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sliu](https://avatars.discourse-cdn.com/v4/letter/s/a9a28c/32.png) [@sliu](https://discuss.elastic.co/u/sliu)\
**Post date:** [December 1, 2017, 8:25pm UTC](https://discuss.elastic.co/t/bitwise-logical-operation-in-filter/109982/1 "2017-12-01T20:25:31Z")

</div>

I see this get asked many times over the years. Is there a way to do bitwise operation in query filtering? For instance, I may have a data field for store opening day: M,T,W,Th,F,S,Sun. So:  
1111100: store opens on weekdays, not weekends  
1111111: opens everyday  
1010000: opens monday and wednesday only.  
That way I could use this for filtering to get stores that are open today.

Please point me to right documents if there's such way. Thanks.

---

<div class="post-metadata">

**Author:** ![Mikhail\_Khludnev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mikhail_khludnev/32/59591_2.png) [@Mikhail\_Khludnev](https://discuss.elastic.co/u/Mikhail_Khludnev)\
**Post date:** [December 1, 2017, 9:39pm UTC](https://discuss.elastic.co/t/bitwise-logical-operation-in-filter/109982/2 "2017-12-01T21:39:47Z")

</div>

{  
"opening\_days": ["M","T","W","Th","F","S","Sun"]  
}  
{  
"opening\_days": ["M","T","W","Th","F"]  
}  
{  
"opening\_days": ["M","W"]  
}

opening\_days:S

---

<div class="post-metadata">

**Author:** ![sliu](https://avatars.discourse-cdn.com/v4/letter/s/a9a28c/32.png) [@sliu](https://discuss.elastic.co/u/sliu)\
**Post date:** [December 2, 2017, 11:34pm UTC](https://discuss.elastic.co/t/bitwise-logical-operation-in-filter/109982/3 "2017-12-02T23:34:20Z")

</div>

Thanks Mikhail.  
I realize that the example is not exactly what I'm looking for. Let me try it this way: let's say each document has 0 to 5 locks:  
00000 -- no lock, anyone can access this document.  
10000 -- user with key 1 can access this document.  
10010 -- user with keys 1 and 4 to access this document.

In search query, I'd like to use the user's keys to filter search results: user with key "10000" can access 1st and 2nd documents. User with "01111" can only access 1st document.

In real world problem, I may need to define 200 locks for all documents. What I'm looking for is long bitwise operation in Elastic.

Thanks for reading it.

---

<div class="post-metadata">

**Author:** ![Mikhail\_Khludnev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mikhail_khludnev/32/59591_2.png) [@Mikhail\_Khludnev](https://discuss.elastic.co/u/Mikhail_Khludnev)\
**Post date:** [December 3, 2017, 7:32am UTC](https://discuss.elastic.co/t/bitwise-logical-operation-in-filter/109982/4 "2017-12-03T07:32:30Z")

</div>

Ok. I've got it.

> User with "01111" can only access 1st document

It's the key requirement. In boolean logic it hits the second doc 10010 ,  
but this doc requires to be hit twice to appear in results.  
This problem is known like boolean expression search or sometimes reverse  
search. eg percolator does something similar.

I've done it on other search engine based on Lucene, you know. It worked as  
follows:  
we issue boolean query like keys:(2 OR 3 OR 4 ..)  
but then we need to score every term hit as 1, sum them and cut off docs  
having score less then number of tokens in keys fields.  
It's a little bit cumbersome, but feasible. Probably Elastic already has  
something more convenient.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 31, 2017, 7:32am UTC](https://discuss.elastic.co/t/bitwise-logical-operation-in-filter/109982/5 "2017-12-31T07:32:37Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
