# Querying a nested document value AND root document value

**URL:** <https://discuss.elastic.co/t/querying-a-nested-document-value-and-root-document-value/63217>\
**Category:** Elasticsearch\
**Created:** [October 17, 2016, 6:55pm UTC](https://discuss.elastic.co/t/querying-a-nested-document-value-and-root-document-value/63217 "2016-10-17T18:55:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kehphin](https://avatars.discourse-cdn.com/v4/letter/k/c68b51/32.png) [@kehphin](https://discuss.elastic.co/u/kehphin)\
**Post date:** [October 17, 2016, 6:55pm UTC](https://discuss.elastic.co/t/querying-a-nested-document-value-and-root-document-value/63217/1 "2016-10-17T18:55:58Z")

</div>

Suppose I have a simplified Organization document like so (ES 2.4):

```
{ 
  "organization" : { 
      "dateUpdated" : 1395211600000,

      "publications" : [ 
         { 
            "dateCreated" : 1393801200000
         },
         { 
           "dateCreated" : 1401055200000
         }
       ]
    }
}

```

I want to find all Organizations that have a publication dateCreated \< the organization's dateUpdated:

```
{
  "query": {
    "nested": {
      "path": "publications",
      "query": {
        "bool": {
          "filter": [
            {
              "script": {
                "script": "doc['publications.dateCreated'].value < doc['dateUpdated'].value"
              }
            }
          ]
        }
      }
    }
  }
}

```

My problem is that when I perform a nested query, the nested query does not have access to the root document values, so doc['dateUpdated'].value is invalid and I get 0 hits.

Any help would be 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 5, 2017, 10:11pm UTC](https://discuss.elastic.co/t/querying-a-nested-document-value-and-root-document-value/63217/2 "2017-07-05T22:11:41Z")

</div>


