# How to query a nested object with combined AND and OR logic?

**URL:** https://discuss.elastic.co/t/how-to-query-a-nested-object-with-combined-and-and-or-logic/102805
**Category:** Elasticsearch
**Created:** [October 5, 2017, 7:45am UTC](https://discuss.elastic.co/t/how-to-query-a-nested-object-with-combined-and-and-or-logic/102805 "2017-10-05T07:45:33Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![mpskovvang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mpskovvang/32/22795_2.png) [@mpskovvang](https://discuss.elastic.co/u/mpskovvang)
#### Post date: [October 5, 2017, 7:45am UTC](https://discuss.elastic.co/t/how-to-query-a-nested-object-with-combined-and-and-or-logic/102805/1 "2017-10-05T07:45:33Z")

</div>

Hi,

How should this expression be written as a query:

_([attributes.id](http://attributes.id) = 14 OR [attributes.id](http://attributes.id) = 15) AND ([attributes.id](http://attributes.id) = 4843 OR [attributes.id](http://attributes.id) = 4859)_

The nested object looks like this:

```
{  
  "attributes":[  
    {  
      "id":14,
      "type":"color",
      "name":"Sort",
      "version":1
    },
    {  
      "id":15,
      "type":"color",
      "name":"Sølv",
      "version":1
    },
    {  
      "id":2031,
      "type":"brand",
      "name":"Jimmy Choo",
      "version":1
    },
    {  
      "id":4843,
      "type":"size",
      "name":"36x28",
      "version":1
    },
    {  
      "id":4859,
      "type":"size",
      "name":"38x36",
      "version":1
    },
    {  
      "id":4927,
      "type":"size",
      "name":"60J",
      "version":1
    },
    {  
      "id":4958,
      "type":"size",
      "name":"75F",
      "version":1
    }
  ]
}

```

I've tried using this query - among many - without any luck:

```
{  
  "query":{  
    "nested":{  
      "path":"attributes",
      "query":{  
        "bool":{  
          "should":[  
            {  
              "terms":{  
                "attributes.id":[  
                  14,
                  15
                ]
              }
            },
            {  
              "terms":{  
                "attributes.id":[  
                  4843,
                  4859
                ]
              }
            }
          ],
          "minimum_should_match":2
        }
      }
    }
  }
}

```

Any help would appreciated.

---

<div class="post-metadata">

### Author: ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)
#### Post date: [October 13, 2017, 7:01am UTC](https://discuss.elastic.co/t/how-to-query-a-nested-object-with-combined-and-and-or-logic/102805/2 "2017-10-13T07:01:09Z")

</div>

Not sure why this does't match, but I would replace the `should` key with `must` key and then remove the `minimum_should_match` key then it maps more directly to your logical query. Can you run this query in the explain and validate query APIs?

---

<div class="post-metadata">

### Author: ![mpskovvang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mpskovvang/32/22795_2.png) [@mpskovvang](https://discuss.elastic.co/u/mpskovvang)
#### Post date: [October 13, 2017, 9:27am UTC](https://discuss.elastic.co/t/how-to-query-a-nested-object-with-combined-and-and-or-logic/102805/3 "2017-10-13T09:27:05Z")

</div>

Thanks for your suggestion! The validation API says the following query is valid but I'm still not getting any hits:

```
GET migration2/product/_search
{
  "query": {
    "nested": {
      "path": "variants.attributes",
      "query": {
        "bool": {
          "must": [
            {
              "terms": {
                "variants.attributes.id": [204]
              }
            },
            {
              "terms": {
                "variants.attributes.id": [2627]
              }
            }
          ]
        }
      }
    }
  }
}

```

Please note I used some other values to match the resultset. The attributes are also a nested object to variants, but that shouldn't make any difference, right?

This is the response from the explain API:

```
{
  "_index": "migration2",
  "_type": "product",
  "_id": "5d25c131748f7d98c589fcfbb68d451e.204",
  "matched": false,
  "explanation": {
    "value": 0,
    "description": "Failure to meet condition(s) of required/prohibited clause(s)",
    "details": [
      {
        "value": 0,
        "description": "no match on required clause (ToParentBlockJoinQuery (+variants.attributes.id:[204 TO 204] +variants.attributes.id:[2627 TO 2627]))",
        "details": [
          {
            "value": 0,
            "description": "Not a match",
            "details": []
          }
        ]
      },
      {
        "value": 0,
        "description": "match on required clause, product of:",
        "details": [
          {
            "value": 0,
            "description": "# clause",
            "details": []
          },
          {
            "value": 1,
            "description": "_type:product, product of:",
            "details": [
              {
                "value": 1,
                "description": "boost",
                "details": []
              },
              {
                "value": 1,
                "description": "queryNorm",
                "details": []
              }
            ]
          }
        ]
      }
    ]
  }
}

```

UPDATED:

This is the document trying to match:

```
{
  "_index" : "migration2",
  "_type" : "product",
  "_id" : "5d25c131748f7d98c589fcfbb68d451e.204",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "status" : "draft",
    "variants" : [
      {
        "version" : 1,
        "sku" : "f4e1cd4583811f4e8e37a8c56eb4616c",
        "gtin" : null,
        "attributes" : [
          {
            "id" : 204,
            "type" : "color",
            "name" : "Lilla",
            "version" : 1
          },
          {
            "id" : 2627,
            "type" : "brand",
            "name" : "Nike",
            "version" : 1
          }
        ],
        "attributes_optimized" : {
          "brands" : [
            2627
          ],
          "colors" : [
            204
          ],
          "sizes" : []
        },
        "attributes_array" : [
          2627,
          204
        ]
      }
    ]
  }
}

```

Note: there are three different attributes fields because I'm comparing their performances.

---

<div class="post-metadata">

### Author: ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)
#### Post date: [October 13, 2017, 9:52am UTC](https://discuss.elastic.co/t/how-to-query-a-nested-object-with-combined-and-and-or-logic/102805/4 "2017-10-13T09:52:33Z")

</div>

What do your mappings look like?

---

<div class="post-metadata">

### Author: ![Daniel\_Penning](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/daniel_penning/32/48792_2.png) [@Daniel\_Penning](https://discuss.elastic.co/u/Daniel_Penning)
#### Post date: [October 13, 2017, 10:08am UTC](https://discuss.elastic.co/t/how-to-query-a-nested-object-with-combined-and-and-or-logic/102805/5 "2017-10-13T10:08:06Z")

</div>

There is no single nested object in your example document that can match both must clauses of your bool query. You need a bool query with two separate nested queries.

```auto
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "variants.attributes",
            "query": {
              "terms": {
                "variants.attributes.id": [
                  204
                ]
              }
            }
          }
        },
        {
          "nested": {
            "path": "variants.attributes",
            "query": {
              "terms": {
                "variants.attributes.id": [
                  2627
                ]
              }
            }
          }
        }
      ]
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![mpskovvang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mpskovvang/32/22795_2.png) [@mpskovvang](https://discuss.elastic.co/u/mpskovvang)
#### Post date: [October 13, 2017, 10:24am UTC](https://discuss.elastic.co/t/how-to-query-a-nested-object-with-combined-and-and-or-logic/102805/6 "2017-10-13T10:24:45Z")

</div>

Thanks, this solved my problem!

However, I had to wrap the query within another nested query to ensure the single variant to have all attributes (not mentioned in my original post):

```
{
  "query": {
    "nested": {
      "path": "variants",
      "query": {
        "bool": {
          "must": [
            {
              "nested": {
                "path": "variants.attributes",
                "query": {
                  "terms": {
                    "variants.attributes.id": [
                      204
                    ]
                  }
                }
              }
            },
            {
              "nested": {
                "path": "variants.attributes",
                "query": {
                  "terms": {
                    "variants.attributes.id": [
                      2627
                    ]
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}
```

---

<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: [November 10, 2017, 10:24am UTC](https://discuss.elastic.co/t/how-to-query-a-nested-object-with-combined-and-and-or-logic/102805/7 "2017-11-10T10:24:51Z")

</div>

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