# Multi Level Nested Queries

**URL:** <https://discuss.elastic.co/t/multi-level-nested-queries/197611>\
**Category:** Elasticsearch\
**Created:** [September 1, 2019, 2:52am UTC](https://discuss.elastic.co/t/multi-level-nested-queries/197611 "2019-09-01T02:52:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Onkar\_Kore](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/onkar_kore/32/53413_2.png) [@Onkar\_Kore](https://discuss.elastic.co/u/Onkar_Kore)\
**Post date:** [September 1, 2019, 2:52am UTC](https://discuss.elastic.co/t/multi-level-nested-queries/197611/1 "2019-09-01T02:52:13Z")

</div>

I have a multi-level nested document. I want to query based on multiple nested queries with all the conditions must match.

Example

Document 1

```auto
{
	"publishing_rule": {
		"publishing_orders": [{
				"transporters": [{
					"fteid": "81"
				}],
				"active": false
			},
			{
				"transporters": [{
					"fteid": "82"
				}],
				"active": true
			}
		]
	}
}

```

Document 2

```auto
{
	"publishing_rule": {
		"publishing_orders": [{
				"transporters": [{
					"fteid": "81"
				}],
				"active": true
			},
			{
				"transporters": [{
					"fteid": "82"
				}],
				"active": false
			}
		]
	}
}

```

I want to fetch all the documents which match below condition

```auto
publishing_rule.publishing_orders.active = true 

AND 

publishing_rule.publishing_orders.transporters.fteid = '81'

```

Both `active` and `transporters.fteid` should be part of same object.

I have tried creating below mapping

```auto
{
  "mappings": {
    "_doc": {
      "properties": {
        "publishing_rule.publishing_orders": {
          "type": "nested",
          "properties": {
            "transporters": {
              "type": "nested"
            }
          }
        }
      }
    }
  }
}

```

And used below query

```auto
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "publishing_rule.publishing_orders",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "publishing_rule.publishing_orders.active": true
                    }
                  }
                ]
              }
            }
          }
        },
        {
          "nested": {
            "path": "publishing_rule.publishing_orders.transporters",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "publishing_rule.publishing_orders.transporters.fteid": "81"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

```

Also tried below query.

```auto
{
  "query": {
    "nested": {
      "path": "publishing_rule.publishing_orders",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "publishing_rule.publishing_orders.active": true
              }
            },
            {
              "nested": {
                "path": "publishing_rule.publishing_orders.transporters",
                "query": {
                  "bool": {
                    "must": [
                      {
                        "match": {
                          "publishing_rule.publishing_orders.transporters.fteid": "81"
                        }
                      }
                    ]
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}

```

But I am not getting expected result. The query returning both the documents.

I am expecting only Document 2 in the result.

---

<div class="post-metadata">

**Author:** ![Andrei\_Stefan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrei_stefan/32/47533_2.png) [@Andrei\_Stefan](https://discuss.elastic.co/u/Andrei_Stefan)\
**Post date:** [September 2, 2019, 10:09am UTC](https://discuss.elastic.co/t/multi-level-nested-queries/197611/2 "2019-09-02T10:09:50Z")

</div>

@Onkar_Kore please, post the result of this request `GET /your_index_name/_mapping`.

---

<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:** [September 30, 2019, 10:09am UTC](https://discuss.elastic.co/t/multi-level-nested-queries/197611/3 "2019-09-30T10:09:50Z")

</div>

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