# Querying nested objects AND comparing nested objects in the same time

**URL:** <https://discuss.elastic.co/t/querying-nested-objects-and-comparing-nested-objects-in-the-same-time/47513>\
**Category:** Elasticsearch\
**Created:** [April 15, 2016, 2:44pm UTC](https://discuss.elastic.co/t/querying-nested-objects-and-comparing-nested-objects-in-the-same-time/47513 "2016-04-15T14:44:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gnujeremie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gnujeremie/32/9198_2.png) [@gnujeremie](https://discuss.elastic.co/u/gnujeremie)\
**Post date:** [April 15, 2016, 2:44pm UTC](https://discuss.elastic.co/t/querying-nested-objects-and-comparing-nested-objects-in-the-same-time/47513/1 "2016-04-15T14:44:54Z")

</div>

Hi,  
I have the following mapping:

```
"page": {
"properties": {
    "rawUrl": {
        "type": "string",
         "index": "not_analyzed"
    },
    "url": {
        "type": "string",
        "analyzer": "url_email"
     },
     "crawls": {
         "type": "nested",
         "properties": {
             "cwlDate": {
                 "type": "date",
                 "format": "yyyy-MM-dd",
                 "index": "not_analyzed"
             },
             "cwlHour": {
                 "type": "date",
                 "format": "HH:mm:ss",
                 "index": "not_analyzed"
             },
             "cwlValues": {
                 "type": "nested",
                  "properties": {
                      "cwlType": {
                          "type": "string",
                           "index": "not_analyzed"
                 },
                 "cwlCode": {
                      "type": "string",
                      "index": "not_analyzed"
                 },
                 "cwlSelector": {
                      "type": "string",
                      "index": "not_analyzed"
                  },
                  "cwlValue": {
                      "type": "string",
                      "index": "not_analyzed"
                  }
              }
          }
      }
}

```

I am looking for a way to request elastic to get, for exemple:  
all documents, where crawls.cwlValues.cwlCode = 'price' and crawls.cwlValues.cwlValue \> 20, but only if crawls.cwlValues.cwlValue.cwlDate is the most recent date:

say I have a document where crawls is like:

```
 [{cwlDate: '2016-04-15', cwlValues: [{cwlCode:'price', cwlValue: '19'}]},
  {cwlDate: '2016-04-14', cwlValues: [{cwlCode:'price', cwlValue: '21'}]}]

```

I don't want the document to match, because even if cwlValue \> 20, it is when cwlDate is not the last one.

I think it is possible with a scripted query, but I was wondering if there was another way.

Thanks.

---

<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:58pm UTC](https://discuss.elastic.co/t/querying-nested-objects-and-comparing-nested-objects-in-the-same-time/47513/2 "2017-07-05T22:58:58Z")

</div>


