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

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?

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
  3. Make the hour/minute part optional when you define the time in the mapping

--Alex

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