# Issue with Range query using hour\_minute format

**URL:** https://discuss.elastic.co/t/issue-with-range-query-using-hour-minute-format/335567
**Category:** Elasticsearch
**Created:** [June 8, 2023, 5:56pm UTC](https://discuss.elastic.co/t/issue-with-range-query-using-hour-minute-format/335567 "2023-06-08T17:56:16Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Daniel\_Scott](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/daniel_scott/32/122038_2.png) [@Daniel\_Scott](https://discuss.elastic.co/u/Daniel_Scott)
#### Post date: [June 8, 2023, 5:56pm UTC](https://discuss.elastic.co/t/issue-with-range-query-using-hour-minute-format/335567/1 "2023-06-08T17:56:16Z")

</div>

I'm having issue using the range query with the various time formats. Here is my query in question:

```auto
{
	"_source": ["created_on"],
	"fields": [{
		"field": "created_on",
		"format": "hour_minute"
	}],
	"query": {
		"range": {
			"created_on": {
				"gt": "23:59",
				"format": "hour_minute"
			}
		}
	},
	"sort": [{
		"created_on": "desc"
	}],
	"size": 25,
	"from": 0,
	"track_total_hits": true
}

```

I'll get a hit like  
\<\>

```auto
{
        "_source": {
          "created_on": "2023-06-08T17:43:29.364Z"
        },
        "fields": {
          "created_on": [
            "17:43"
          ]
        },
        "sort": [
          1686246209364
        ]
      }

```

So the gt part of the query is ignored. No matter what I put in for gt I get all results. Inversely if I put any value in for lt I get no results.

Any thoughts on this?

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [June 9, 2023, 9:05am UTC](https://discuss.elastic.co/t/issue-with-range-query-using-hour-minute-format/335567/2 "2023-06-09T09:05:19Z")

</div>

Welcome!

What's the mapping of the field?

---

<div class="post-metadata">

### Author: ![Daniel\_Scott](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/daniel_scott/32/122038_2.png) [@Daniel\_Scott](https://discuss.elastic.co/u/Daniel_Scott)
#### Post date: [June 9, 2023, 2:28pm UTC](https://discuss.elastic.co/t/issue-with-range-query-using-hour-minute-format/335567/3 "2023-06-09T14:28:14Z")

</div>

Mapping of the field is date

```auto
 "created_on": {
          "type": "date"
        }

```

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [June 9, 2023, 2:52pm UTC](https://discuss.elastic.co/t/issue-with-range-query-using-hour-minute-format/335567/4 "2023-06-09T14:52:40Z")

</div>

If you don't care about the date itself but just the hour of the day, you need to index that value as a the hour of the day.  
I'd recommend doing some maths here and for `17:43` add a field:

```
"mn_of_the_day": 1063

```

And then use that field.

To do this, you can either do that from your application which generates the data, or you can use an [Ingest Script Processor](https://www.elastic.co/guide/en/elasticsearch/reference/8.8/script-processor.html).

If you can't do that before indexing, then you can do that with [a runtime field](https://www.elastic.co/guide/en/elasticsearch/reference/current/runtime.html) (using a script) but that will be much slower. You have an example in the documentation which [computes the day of the week](https://www.elastic.co/guide/en/elasticsearch/reference/current/runtime-mapping-fields.html). You can use that as a start.

---

<div class="post-metadata">

### Author: ![Daniel\_Scott](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/daniel_scott/32/122038_2.png) [@Daniel\_Scott](https://discuss.elastic.co/u/Daniel_Scott)
#### Post date: [June 9, 2023, 3:33pm UTC](https://discuss.elastic.co/t/issue-with-range-query-using-hour-minute-format/335567/5 "2023-06-09T15:33:52Z")

</div>

Interesting. Am I correct in assuming that `format` on a range query does not cast the value? That seems to be what the documentation indicates.

I originally was looking to do this with a painless script but was unable to find a way to pull full time from a date, I can pull hour or minute but not them together, unless I'm missing something

---

<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 7, 2023, 3:33pm UTC](https://discuss.elastic.co/t/issue-with-range-query-using-hour-minute-format/335567/6 "2023-07-07T15:33:53Z")

</div>

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