# How to write a simple nested query for this case?

**URL:** <https://discuss.elastic.co/t/how-to-write-a-simple-nested-query-for-this-case/150169>\
**Category:** Elasticsearch\
**Created:** [September 27, 2018, 10:12am UTC](https://discuss.elastic.co/t/how-to-write-a-simple-nested-query-for-this-case/150169 "2018-09-27T10:12:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ali\_houshyaripour](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ali_houshyaripour/32/35931_2.png) [@ali\_houshyaripour](https://discuss.elastic.co/u/ali_houshyaripour)\
**Post date:** [September 27, 2018, 10:12am UTC](https://discuss.elastic.co/t/how-to-write-a-simple-nested-query-for-this-case/150169/1 "2018-09-27T10:12:27Z")

</div>

Hi,  
I have a place model that it has hours for week day as you see in below, Now I want to write a query to get places that open now (pass hour and minute as separate parameters to query), but don't know how to write a advanced nested query with json and not plugin framework.  
Anyone can help me?

{  
"\_index" : "places",  
"\_type" : "place",  
"\_id" : "15a12fg690t81p",  
"\_score" : 1.0,  
"\_source" : {  
"id" : "15a12fg690t81p",  
"name" : "fast-food",  
"hours" : [  
{  
"day" : 0,  
"is\_overnight" : false,  
"start" : "08:00",  
"end" : "22:00"  
},  
{  
"day" : 1,  
"is\_overnight" : false,  
"start" : "08:00",  
"end" : "22:00"  
},  
{  
"day" : 2,  
"is\_overnight" : false,  
"start" : "08:00",  
"end" : "22:00"  
},  
{  
"day" : 3,  
"is\_overnight" : false,  
"start" : "08:00",  
"end" : "22:00"  
},  
{  
"day" : 4,  
"is\_overnight" : false,  
"start" : "08:00",  
"end" : "22:00"  
},  
{  
"day" : 5,  
"is\_overnight" : false,  
"start" : "08:00",  
"end" : "18:00"  
}  
]  
}  
}

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [September 27, 2018, 11:00am UTC](https://discuss.elastic.co/t/how-to-write-a-simple-nested-query-for-this-case/150169/2 "2018-09-27T11:00:13Z")

</div>

We now have a range field type that is optimized for this use case.  
Example:

```
// Setup index with new range field type
DELETE test
PUT test
{
  "settings": {
	"number_of_replicas": 0,
	"number_of_shards": 1
  },
  "mappings": {
	"_doc": {
	  "properties": {
		"hours": {
		  "type": "nested",
		  "properties": {
			"day": {
			  "type": "integer"
			},
			"hours": {
			  "type": "double_range"
			}
		  }
		}
	  }
	}
  }
}

```

Add a doc (note use of decimals to express hours - maybe you'd prefer integers to express minuteOfDay)

```
// Add doc
POST test/_doc/1
{
  "id": "15a12fg690t81p",
  "name": "fast-food",
  "hours": [
	{
	  "day": 0,
	  "is_overnight": false,
	  "hours": {
		"gte": "8",
		"lte": "22.5"
	  }
	},
	{
	  "day": 5,
	  "is_overnight": false,
	  "hours": {
		"gte": "8",
		"lte": "18"
	  }
	}
  ]
}

```

Now search - need to use nested and regular term query that denotes a position somewhere in the time range,

```
POST test/_doc/_search
{
  "query": {
	"nested": {
	  "path": "hours",
	  "query": {
		"bool": {
		  "must": [
			{
			  "match": {
				"hours.day": 5
			  }
			},
			{
			  "term":{
				"hours.hours":19
			  }
			}
		  ]
		}
	  }
	}
  }
}
```

---

<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 25, 2018, 11:00am UTC](https://discuss.elastic.co/t/how-to-write-a-simple-nested-query-for-this-case/150169/3 "2018-10-25T11:00:16Z")

</div>

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