sudo-suhas/elastic-builder

whether support range date query in elasticsearch6.8.0 ?

sunweiconfidence opened this issue · 2 comments

hi,
i want to query with range date, like below example:

{
  "from": 0,
  "size": 1000,
  "query": {
    "bool": {
      "must": {
        "exists": { "field": "test" }
      },
      "filter": {
        "range": {
          "CreateDate": {
            "gte": "06/06/2019",
            "lt": "30/09/2019",
            "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis||dd/MM/yyyy||yyyy"
          }
        }
      }
    }
  }
}

i use below elastic-builder js clause,

const requestBody = esb.requestBodySearch()
    .from(0).size(1000)
    .query(
        esb.boolQuery()
            .must(esb.existsQuery('test'))
            .filter(esb.rangeQuery('CreateDate').gt('06/06/2019'))
    );

but meet with below exception:

race: [parse_exception] failed to parse date field [06/06/2019] with format [strict_date_optional_time||epoch_millis]

i want to know how do let me support this range date query, or not support it? thanks

edit: formatting

The error is returned by Elasticsearch because it is not able to parse the data string present in your query. Since range query supports specifying the format, the request body that you shared can be generated like so:

const requestBody = esb.requestBodySearch()
    .from(0)
    .size(1000)
    .query(
        esb.boolQuery()
            .must(esb.existsQuery('test'))
            .filter(
                esb.rangeQuery('CreateDate')
                    .gte('06/06/2019')
                    .lt('30/09/2019')
                    .format(
                        'yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis||dd/MM/yyyy||yyyy'
                    )
            )
    );

In the future, please check out the docs since all the methods are documented - https://elastic-builder.js.org/docs/#rangequeryformat

ok, thanks, i have collected the document