# Find count of nested objects matching with query

**URL:** <https://discuss.elastic.co/t/find-count-of-nested-objects-matching-with-query/33333>\
**Category:** Elasticsearch\
**Created:** [October 30, 2015, 5:26am UTC](https://discuss.elastic.co/t/find-count-of-nested-objects-matching-with-query/33333 "2015-10-30T05:26:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jagdish\_nasit](https://avatars.discourse-cdn.com/v4/letter/j/e5b9ba/32.png) [@jagdish\_nasit](https://discuss.elastic.co/u/jagdish_nasit)\
**Post date:** [October 30, 2015, 5:26am UTC](https://discuss.elastic.co/t/find-count-of-nested-objects-matching-with-query/33333/1 "2015-10-30T05:26:23Z")

</div>

Is there a way we can find #of nested objects matching with query criteria.

{ "customername": "John",  
"order": [  
{ "productname":"books", "qty":5, "orderdate": 2015-12-01},  
{ "productname":"pens", "qty":15, "orderdate": 2015-12-01},  
{ "productname":"papers", "qty":100, "orderdate": 2015-12-01}  
]  
}

1. how can we find if we have customer with order where qty \> 100
2. how many items is there in order with qty \> 100

Thanks !!!

---

<div class="post-metadata">

**Author:** ![mainec](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mainec/32/5557_2.png) [@mainec](https://discuss.elastic.co/u/mainec)\
**Post date:** [October 30, 2015, 10:30am UTC](https://discuss.elastic.co/t/find-count-of-nested-objects-matching-with-query/33333/2 "2015-10-30T10:30:54Z")

</div>

> [@jagdish\_nasit](#):
>
> how can we find if we have customer with order where qty \> 100

With the help of a range query this is actually fairly simple (for more information see here [Range query | Elasticsearch Guide [8.11] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html)

```
DELETE index

PUT index/type/1
{
    "customername": "John", 
    "order": [ 
        { "productname":"books", "qty":5, "orderdate": "2015-12-01"},
        { "productname":"pens", "qty":15, "orderdate": "2015-12-01"},
        { "productname":"papers", "qty":100, "orderdate": "2015-12-01"},
        { "productname":"disks", "qty":100, "orderdate": "2015-12-01"}
    ]

```

}

```
GET _search
{
    "query": {
        "range" : {
            "order.qty" : 
                {"gte": 100}
        }
    }
}

```

> [@jagdish\_nasit](#):
>
> how many items is there in order with qty \> 100

IIRC for this you need to help Elasticsearch determine the correct mapping. You will probably want to specify that you'd like to use nested objects and execute a count query against those. For more information check out [https://www.elastic.co/guide/en/elasticsearch/reference/2.x/nested.html](https://www.elastic.co/guide/en/elasticsearch/reference/2.x/nested.html)

Hope this helps,  
Isabel

---

<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, 11:41pm UTC](https://discuss.elastic.co/t/find-count-of-nested-objects-matching-with-query/33333/3 "2017-07-05T23:41:44Z")

</div>


