# How to search using only month and year (or only date) in date field?

**URL:** https://discuss.elastic.co/t/how-to-search-using-only-month-and-year-or-only-date-in-date-field/218332
**Category:** Elasticsearch
**Created:** [February 7, 2020, 11:26am UTC](https://discuss.elastic.co/t/how-to-search-using-only-month-and-year-or-only-date-in-date-field/218332 "2020-02-07T11:26:23Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![franzisk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/franzisk/32/61527_2.png) [@franzisk](https://discuss.elastic.co/u/franzisk)
#### Post date: [February 7, 2020, 11:26am UTC](https://discuss.elastic.co/t/how-to-search-using-only-month-and-year-or-only-date-in-date-field/218332/1 "2020-02-07T11:26:23Z")

</div>

Hi,

I am using Spring Boot Elasticsearch and I have something like this:  
`Optional<List<Evento>> findByClienteIdAndDataEqualsAndLogLike(Integer id, String eventDate, String log, Pageable pageable);`

This **eventDate** has this format ' **dd/MM/yyyy HH:mm**', so if I call it like this:  
`this.eventoRepository.findByClienteIdAndDataEqualsAndLogLike(11, "18/03/2017 20:55", "logout", PageRequest.of(0, 1000, Sort.by(Sort.Direction.DESC, "data")));`  
**It works!!**

But if call like this:  
`this.eventoRepository.findByClienteIdAndDataEqualsAndLogLike(11, "18/03/2017", "logout", PageRequest.of(0, 1000, Sort.by(Sort.Direction.DESC, "data")));`

**It doesn't work because it complains about the format:**  
`ElasticsearchParseException[failed to parse date field [18] with format [dd/MM/yyyy HH:mm]: [failed to parse date field [18] with format [dd/MM/yyyy HH:mm]]]; nested: IllegalArgumentException[failed to parse date field [18] with format [dd/MM/yyyy HH:mm]]; nested: NotSerializableExceptionWrapper[date_time_parse_exception: Text '18' could not be parsed at index 2];`

I want is to search using only the month and the year, as "bring me everything from this client on March of 2019".

Or, yet, using the complete date, such as "18/03/2017".

Any word on how I would do that?

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [February 7, 2020, 2:51pm UTC](https://discuss.elastic.co/t/how-to-search-using-only-month-and-year-or-only-date-in-date-field/218332/2 "2020-02-07T14:51:41Z")

</div>

a few options here (from an Elasticsearch perspective, not sure how this can be accomodated for in your app).

1. If you do a range query, alway supply `00:00` as time
2. Change the format of the date of the range query to `dd/MM/yyyy`, see [https://www.elastic.co/guide/en/elasticsearch/reference/7.5/query-dsl-range-query.html#range-query-field-params](https://www.elastic.co/guide/en/elasticsearch/reference/7.5/query-dsl-range-query.html#range-query-field-params)
3. Make the hour/minute part optional when you define the time in the mapping

--Alex

---

<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: [March 6, 2020, 2:51pm UTC](https://discuss.elastic.co/t/how-to-search-using-only-month-and-year-or-only-date-in-date-field/218332/3 "2020-03-06T14:51:45Z")

</div>

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