Elastic search range query not working

Hi Currently I am having an elastic search index and i am trying to perform a date range query on the documents within that index using java high level rest client library . However it doesn't work as expected. so the following is my index .I have to perform a range query on the field @timestamp.

{
  "took" : 6,
  "timed_out" : false,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 229081,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "mep-reports-2019.09.11",
        "_type" : "doc",
        "_id" : "68e8e03f-baf8-4bfc-a920-58e26edf835c-353899837500",
        "_score" : 1.0,
        "_source" : {
          "account_id" : "270d13e6-2f4f-4d51-99d5-92ffba5f0cb6",
          "inventory" : "SMS",
          "flight_name" : "test flight 001",
          "status" : "ENROUTE",
          "msg_text" : "Test !!!!!!!!!!!!!!1 elastic search:is.gd/YfzotY",
          "flight_id" : "68e8e03f-baf8-4bfc-a920-58e26edf835c",
          "submission_ts" : "1568197286",
          "recipient" : "353899837500",
          "o_error" : null,
          "nof_segments" : "-1",
          "campaign_id" : "0fae8662-bee9-46ac-9b3e-062f4ba55966",
          "campaign_name" : "Index search petri11",
          "@version" : "1",
          "sender" : "800111",
          "delivery_ts" : "0",
          "@timestamp" : "2019-09-11T10:21:26.000Z"
        }
      },
      {
        "_index" : "mep-reports-2019.09.11",
        "_type" : "doc",
        "_id" : "68e8e03f-baf8-4bfc-a920-58e26edf835c-353899837503",
        "_score" : 1.0,
        "_source" : {
          "account_id" : "270d13e6-2f4f-4d51-99d5-92ffba5f0cb6",
          "inventory" : "SMS",
          "flight_name" : "test flight 001",
          "status" : "ENROUTE",
          "msg_text" : "Test !!!!!!!!!!!!!!1 elastic searchh",
          "flight_id" : "68e8e03f-baf8-4bfc-a920-58e26edf835c",
          "submission_ts" : "1568197286",
          "recipient" : "353899837503",
          "o_error" : null,
          "nof_segments" : "-1",
          "campaign_id" : "0fae8662-bee9-46ac-9b3e-062f4ba55966",
          "campaign_name" : "Index search petri11",
          "@version" : "1",
          "sender" : "800111",
          "delivery_ts" : "0",
          "@timestamp" : "2019-09-11T10:21:26.000Z"
        }
      }
}

the followign is my java code that tries to do a range query on the @timestamp and it doesnt work . I am only pasting a part of the range query snippet below.

SearchRequestBuilder range(ZonedDateTime startDate, ZonedDateTime endDate) {

    System.out.println(startDate); // 2019-08-31T23:00Z
    System.out.println(endDate);   // 2019-09-17T13:25:18.154Z



    RangeQueryBuilder startRangeQueryBuilder = QueryBuilders.rangeQuery("@timestamp").gte(startDate);
    startRangeQueryBuilder.format("yyyy-MM-dd'T'HH:mm:ss.SSSZ");

    boolQueryBuilder.should(startRangeQueryBuilder);

    RangeQueryBuilder endRangeQueryBuilder = QueryBuilders.rangeQuery("@timestamp").lte(endDate);
    boolQueryBuilder.should(endRangeQueryBuilder);
    endRangeQueryBuilder.format("yyyy-MM-dd'T'HH:mm:ss.SSSZ");

    return this;
}

any idea how can i fix my range query to in order to work it correctly

thank you so much

hey,

please take your time to reread how should clauses work within a bool query. In this particular case a should results in an OR, which means you are searching for documents older than X or younger Y.

First you should combine this to one range query

QueryBuilders.rangeQuery("@timestamp").gte(startDate).lte(endDate);

Second you should put that single rangeQuery into the filter part of a boolean query.

--Alex

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