Elasticsearch Date Range Query

Sep 15, 2019

Introduction

In addition to numeric ranges, the range query also supports query results using a date range. You can specify date values for one or more conditions such as less than, less than or equal to, greater than and greater than or equal to a given value. The following is an example of a date-range query which uses less than a given date.

{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "birth_date": {
              "lt": "1955-01-31",
              "format": "yyyy-MM-dd"
            }
          }
        }
      ]
    }
  },
  "from": 0,
  "size": 25
}

In addition to values for fields like lt, lte, gt and gte, you can also specify the format in which the date is specified.

Sample results for the above query is shown below.

  "hits": {
    "total": 1805,
    "max_score": 0.0,
    "hits": [
      {
        "_index": "employees.csv",
        "_type": "doc",
        "_id": "5",
        "_score": 0.0,
        "_source": {
          "emp_no": 10005,
          "birth_date": "1955-01-21",
          "first_name": "Kyoichi",
          "last_name": "Maliniak",
          "gender": null,
          "hire_date": "1989-09-12"
        }
      },
      {
        "_index": "employees.csv",
        "_type": "doc",
        "_id": "150",
        "_score": 0.0,
        "_source": {
          "emp_no": 10150,
          "birth_date": "1955-01-29",
          "first_name": "Zhenbing",
          "last_name": "Perng",
          "gender": false,
          "hire_date": "1986-11-16"
        }
      },
...

The Format Codes

The format codes used to specify the format is shown below.

(click for larger image)

Specifying a range

To specify a date range, specify values bracketing a date region by including values for lt or lte and gt or gte. For example, the query below specifies a range of about a month for the date-range window.

{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "birth_date": {
              "lt": "1955-01-31",
              "gt": "1955-01-01",
              "format": "yyyy-MM-dd"
            }
          }
        }
      ]
    }
  },
  "from": 0,
  "size": 25
}

Which results in hits within the specified range.

    "hits": [
      {
        "_index": "employees.csv",
        "_type": "doc",
        "_id": "5",
        "_score": 0.0,
        "_source": {
          "emp_no": 10005,
          "birth_date": "1955-01-21",
          "first_name": "Kyoichi",
          "last_name": "Maliniak",
          "gender": null,
          "hire_date": "1989-09-12"
        }
      },
      {
        "_index": "employees.csv",
        "_type": "doc",
        "_id": "150",
        "_score": 0.0,
        "_source": {
          "emp_no": 10150,
          "birth_date": "1955-01-29",
          "first_name": "Zhenbing",
          "last_name": "Perng",
          "gender": false,
          "hire_date": "1986-11-16"
        }
      },
...

Equals Date Query

Since the range query conditions lack an equals clause, you can simulate an equals query using the same date for lte and gte. Example below.

{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "birth_date": {
              "gte": "1955-01-21",
              "lte": "1955-01-21",
              "format": "yyyy-MM-dd"
            }
          }
        }
      ]
    }
  },
  "from": 0,
  "size": 25
}

The resultant hits:

"hits": [
  {
    "_index": "employees.csv",
    "_type": "doc",
    "_id": "5",
    "_score": 1.0,
    "_source": {
      "emp_no": 10005,
      "birth_date": "1955-01-21",
      "first_name": "Kyoichi",
      "last_name": "Maliniak",
      "gender": null,
      "hire_date": "1989-09-12"
    }
  },
  {
    "_index": "employees.csv",
    "_type": "doc",
    "_id": "4290",
    "_score": 1.0,
    "_source": {
      "emp_no": 14290,
      "birth_date": "1955-01-21",
      "first_name": "Miyeon",
      "last_name": "Olivero",
      "gender": null,
      "hire_date": "1987-11-12"
    }
  },
...

Date Range query in Argon

Argon provides a dialog for specifying date ranges for a query.

1. Right-click for context menu on a date field.

(click for larger image)

2. Choose condition from among: <, <=, > or >=.

(click for larger image)

3. Choose second condition (and the date) if required.

(click for larger image)

4. Showing results using a single date.

(click for larger image)

See Also

Numeric Range Query