# Nested queries that refer to an expression on the parent

**URL:** https://discuss.elastic.co/t/nested-queries-that-refer-to-an-expression-on-the-parent/336938
**Category:** Elasticsearch
**Created:** [June 27, 2023, 3:11am UTC](https://discuss.elastic.co/t/nested-queries-that-refer-to-an-expression-on-the-parent/336938 "2023-06-27T03:11:20Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![DaveG](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/daveg/32/46195_2.png) [@DaveG](https://discuss.elastic.co/u/DaveG)
#### Post date: [June 27, 2023, 3:11am UTC](https://discuss.elastic.co/t/nested-queries-that-refer-to-an-expression-on-the-parent/336938/1 "2023-06-27T03:11:20Z")

</div>

Hi All,

I wish to write a query on nested data where there are expressions on the parent data as wells as expressions on the nested data all mixed together.

For example, get me all the records that have id = 1 and exists lineId 11

```auto
GET myIndex/_search
{
  "query": {
    "nested": {
      "path": "lines",
      "query": {
        "bool": {
          "must": [
            {
              "range": {
                "lines.lineId": {
                  "gte": 11,
                  "lte": 11
                }
              }
            },
            {
              "range": {
                "id": {
                  "gte": 1,
                  "lte": 1
                }
              }
            }
          ]
        }
      }
    }
  }
}

```

This does not work as it does not recognize the root level id field in the nested query. I realize in this simple example I can re-write this as a bool query of the root id=1 and nested lineId=11, but how would I compose more complex cases:

example:  
give me record where exists a line with id=1 and line.lineid = 11 or someOtherParentField = 'x' and line.someLineField = 'y' and ...

all thoughts appreciated.

---

<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 25, 2023, 3:12am UTC](https://discuss.elastic.co/t/nested-queries-that-refer-to-an-expression-on-the-parent/336938/2 "2023-07-25T03:12:18Z")

</div>

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