0

I'm learning Elasticsearch API while practicing I'm facing the issue is unable to fetch documents between two dates those documents match two fields but without date range it's working fine

    BoolQueryBuilder filter = new BoolQueryBuilder();
    BoolQueryBuilder query = QueryBuilders.boolQuery();

    for (String q : list) {
            // both the fields must exists
            query = QueryBuilders.boolQuery().must(QueryBuilders.matchQuery("field1", q))
                    .must(QueryBuilders.matchQuery("field2", val));
            filter.should(query);
    }

    filter.must(QueryBuilders.rangeQuery("datetime").gte(from).lte(to);

    searchSourceBuilder.query(filter);

Where,

list contains the list of words for the field1 field. Both field1 & field2 must match such document I want to retrieve

datetime is a custom datetime field & the value looks like 2022-06-09 12:32:36

Can anyone help me to resolve this issue

nay
  • 95
  • 12
  • JHLRC is deprecated if you are using Elasticsearch > 7.15 you should use their latest client https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/index.html – Amit Jun 09 '22 at 17:02
  • @Amit yes I'm using 8.2 latest version – nay Jun 09 '22 at 17:05

1 Answers1

0

I think you need, Date format to convert date values in the query. To format your dates, either use the built-in formats provided by ES - https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html

or, you can try customised format - https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

After formatting, gte and lte should work as expected.

nc_164
  • 11
  • 3