# Understanding bool/must behavior (intersection? )

**URL:** https://discuss.elastic.co/t/understanding-bool-must-behavior-intersection/39286
**Category:** Elasticsearch
**Created:** [January 14, 2016, 11:03pm UTC](https://discuss.elastic.co/t/understanding-bool-must-behavior-intersection/39286 "2016-01-14T23:03:54Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![frankd](https://avatars.discourse-cdn.com/v4/letter/f/b2d939/32.png) [@frankd](https://discuss.elastic.co/u/frankd)
#### Post date: [January 14, 2016, 11:03pm UTC](https://discuss.elastic.co/t/understanding-bool-must-behavior-intersection/39286/1 "2016-01-14T23:03:55Z")

</div>

I am having trouble getting a bool : must filter to return what I think it should. (I have a gist at [http://pastebin.com/e4Mbgq48](http://pastebin.com/e4Mbgq48) )

Given that this returns all 3 records:  
Bool:{  
must  
[  
conditionA = true  
]  
}

and this also Returns all 3 records:  
Bool:{  
must  
[  
conditionB = true  
]  
}

why does this Return NO records:  
Bool:{  
must  
[  
conditionA = true  
,  
conditionB = true  
]  
}

What am I misunderstanding about bool/must? (elasticsearch 1.6)  
thanks  
Frank

My Conditions are actually nested key value[] properties that I want to use for aggregation.

---

<div class="post-metadata">

### Author: ![msimos](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@msimos](https://discuss.elastic.co/u/msimos)
#### Post date: [January 15, 2016, 12:59am UTC](https://discuss.elastic.co/t/understanding-bool-must-behavior-intersection/39286/2 "2016-01-15T00:59:39Z")

</div>

A bool filter with a must clause, will only return documents that have **both** conditionA = true and conditionB = true. See this example:

[https://www.elastic.co/guide/en/elasticsearch/guide/master/combining-filters.html#\_nesting\_boolean\_filters](https://www.elastic.co/guide/en/elasticsearch/guide/master/combining-filters.html#_nesting_boolean_filters)

So in your above example, if no documents are returned then there is no document that has **both** conditionA = true and conditionB = true.

---

<div class="post-metadata">

### Author: ![frankd](https://avatars.discourse-cdn.com/v4/letter/f/b2d939/32.png) [@frankd](https://discuss.elastic.co/u/frankd)
#### Post date: [January 15, 2016, 5:00pm UTC](https://discuss.elastic.co/t/understanding-bool-must-behavior-intersection/39286/4 "2016-01-15T17:00:27Z")

</div>

Mike,  
thanks for answering.  
But I am confused because I believe my document satisfies both conditions.

I made a new gist ( [http://pastebin.com/G8P0LbLP](http://pastebin.com/G8P0LbLP) )  
that

- creates an index
- adds one doc  
-contains 3 queries:
- the first filters on must[condition A] (and the one doc is returned)  
-the second filters on must[condition B] (the same doc is returned)  
-the third filters on condition must[A,B] (no hits)

I have no trouble making this work with non-nested properties, but with the nested  
properties in my example I get this unexpected result.  
Frank

---

<div class="post-metadata">

### Author: ![msimos](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@msimos](https://discuss.elastic.co/u/msimos)
#### Post date: [January 15, 2016, 9:15pm UTC](https://discuss.elastic.co/t/understanding-bool-must-behavior-intersection/39286/5 "2016-01-15T21:15:55Z")

</div>

Try using something like this:

```auto
GET test_attribs/_search
{
  "query": {
    "nested": {
      "path": "attributes",
      "query": {
        "filtered": {
          "filter": {
            "bool": {
              "must": [
                {
                  "term": {
                    "attributes.key": "Color"
                  }
                },
                {
                  "term": {
                    "attributes.value": "Blue"
                  }
                }
              ]
            }
          }
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![frankd](https://avatars.discourse-cdn.com/v4/letter/f/b2d939/32.png) [@frankd](https://discuss.elastic.co/u/frankd)
#### Post date: [January 15, 2016, 10:20pm UTC](https://discuss.elastic.co/t/understanding-bool-must-behavior-intersection/39286/6 "2016-01-15T22:20:52Z")

</div>

That works for color, but I would like to add a second set of must's to your example with  
key='Type' and value='Autoclavable'. (Since there could be blue things with a type of 'somethingElse' and I need to select only blue, autoclavable types )  
since the document contains:  
nested attributes:[  
{u'key': u'Color', u'value': [u'Black', u'Blue', u'Teal']},  
{u'key': u'Type', u'value': [u'Autoclavable']}  
]  
-would that not also eval to true? It doesn't, and I am not sure why not.

Could it be that only one "key/value" element is examined in a pass - so that if I add to your example a term clause  
of "term": { "attributes.key": "Type" } it becomes false because key is already "Color" and can't also be "Type"?

Maybe I need a different index structure to do something like  
"select a doc from elasticsearch having an attribute where  
( key = color and value= 'Blue')  
and also having an attribute where  
(key = Type and value= 'Autoclavable'

I am ultimately trying to have dynamic filterable attributes.

---

<div class="post-metadata">

### Author: ![msimos](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@msimos](https://discuss.elastic.co/u/msimos)
#### Post date: [January 15, 2016, 11:08pm UTC](https://discuss.elastic.co/t/understanding-bool-must-behavior-intersection/39286/7 "2016-01-15T23:08:49Z")

</div>

Hi,

I believe the issue is when using nested documents there is no nested document which has key = color, value= 'Blue, key = Type and value= 'Autoclavable'. What you have is two nested documents one with ( key = color and value= 'Blue') and a second with (key = Type and value= 'Autoclavable'). So this is why the search fails. See this example:

[https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-objects.html](https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-objects.html)

---

<div class="post-metadata">

### Author: ![frankd](https://avatars.discourse-cdn.com/v4/letter/f/b2d939/32.png) [@frankd](https://discuss.elastic.co/u/frankd)
#### Post date: [January 15, 2016, 11:15pm UTC](https://discuss.elastic.co/t/understanding-bool-must-behavior-intersection/39286/8 "2016-01-15T23:15:34Z")

</div>

That's my problem, thank you.  
I guess I'll have to come up with a different index/mapping or look into dynamic mappings.

---

<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: [July 5, 2017, 11:23pm UTC](https://discuss.elastic.co/t/understanding-bool-must-behavior-intersection/39286/9 "2017-07-05T23:23:57Z")

</div>


