# Help with querying with nested fields and range

**URL:** https://discuss.elastic.co/t/help-with-querying-with-nested-fields-and-range/149910
**Category:** Elasticsearch
**Created:** [September 25, 2018, 10:48pm UTC](https://discuss.elastic.co/t/help-with-querying-with-nested-fields-and-range/149910 "2018-09-25T22:48:44Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![chandu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chandu/32/35878_2.png) [@chandu](https://discuss.elastic.co/u/chandu)
#### Post date: [September 25, 2018, 10:48pm UTC](https://discuss.elastic.co/t/help-with-querying-with-nested-fields-and-range/149910/1 "2018-09-25T22:48:44Z")

</div>

Hi

Could someone help with the query?

Here is couple of sample records (simplified) that are stored in elasticsearch

record#1  
{  
"some\_field": true,  
"products": {  
"a": { "dt": "2018-08-28"},  
"b": { "dt": "2018-08-29"},  
"c": { "dt": "2018-08-27"},  
"d": { "dt": "2018-08-20"},  
}  
}

record#2  
{  
"some\_field": false,  
"products": {  
"a": { "dt": "2018-08-27"},  
"d": { "dt": "2018-08-21"},  
}  
}

I am trying to write a query that would fetch the following result:  
**All records that have products "b" with dt in the range of 2018-08-20 and 2018-08-30 sorted by products.b.dt**

I am running ES 2.4

Any help is appreciated.

I tried running the below query but it is throwing error. Basically, I wanted to run "range" only if the field exist

`{ "query": { "bool": { "must": [{"exists": { "field": "products.b"}}, {"range":{"products.b.dt": {"lt":" 2018-08-30", "gt": " 2018-08-20"} }}] } } }`

---

<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: [October 23, 2018, 10:48pm UTC](https://discuss.elastic.co/t/help-with-querying-with-nested-fields-and-range/149910/2 "2018-10-23T22:48:56Z")

</div>

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