This version of the OpenSearch documentation is no longer maintained. For the latest version, see the current documentation. For information about OpenSearch version maintenance, see Release Schedule and Maintenance Policy.

Range query

You can search for a range of values in a field with the range query.

To search for documents in which the line_id value is >= 10 and <= 20, use the following request:

  1. GET shakespeare/_search
  2. {
  3. "query": {
  4. "range": {
  5. "line_id": {
  6. "gte": 10,
  7. "lte": 20
  8. }
  9. }
  10. }
  11. }

copy

Operators

The field parameter in the range query accepts the following optional operator parameters:

  • gte: Greater than or equal to
  • gt: Greater than
  • lte: Less than or equal to
  • lt: Less than

Date fields

You can use range queries on fields containing dates. For example, assume that you have a products index and you want to find all the products that were added in the year 2019:

  1. GET products/_search
  2. {
  3. "query": {
  4. "range": {
  5. "created": {
  6. "gte": "2019/01/01",
  7. "lte": "2019/12/31"
  8. }
  9. }
  10. }
  11. }

copy

For more information about supported date formats, see Formats.

Format

To use a date format other than the field’s mapped format in a query, specify it in the format field.

For example, if the products index maps the created field as strict_date_optional_time, you can specify a different format for a query date as follows:

  1. GET /products/_search
  2. {
  3. "query": {
  4. "range": {
  5. "created": {
  6. "gte": "01/01/2022",
  7. "lte": "31/12/2022",
  8. "format":"dd/MM/yyyy"
  9. }
  10. }
  11. }
  12. }

copy

Missing date components

OpenSearch populates missing date components with the following values:

  • MONTH_OF_YEAR: 01
  • DAY_OF_MONTH: 01
  • HOUR_OF_DAY: 23
  • MINUTE_OF_HOUR: 59
  • SECOND_OF_MINUTE: 59
  • NANO_OF_SECOND: 999_999_999

If the year is missing, it is not populated.

For example, consider the following request that specifies only the year in the start date:

  1. GET /products/_search
  2. {
  3. "query": {
  4. "range": {
  5. "created": {
  6. "gte": "2022",
  7. "lte": "2022-12-31"
  8. }
  9. }
  10. }
  11. }

copy

The start date is populated with the default values, so the gte parameter used is 2022-01-01T23:59:59.999999999Z.

Relative dates

You can specify relative dates by using date math.

To subtract 1 year and 1 day from the specified date, use the following query:

  1. GET products/_search
  2. {
  3. "query": {
  4. "range": {
  5. "created": {
  6. "gte": "2019/01/01||-1y-1d"
  7. }
  8. }
  9. }
  10. }

copy

In the preceding example, 2019/01/01 is the anchor date (the starting point) for the date math. After the two pipe characters (||), you are specifying a mathematical expression relative to the anchor date. In this example, you are subtracting 1 year (-1y) and 1 day (-1d).

You can also round off dates by adding a forward slash to the date or time unit.

To find products added within the last year, rounded off by month, use the following query:

  1. GET products/_search
  2. {
  3. "query": {
  4. "range": {
  5. "created": {
  6. "gte": "now-1y/M"
  7. }
  8. }
  9. }
  10. }

copy

The keyword now refers to the current date and time.

Rounding relative dates

The following table specifies how relative dates are rounded.

ParameterRounding ruleExample: The value 2022-05-18||/M is rounded to
gtRounds up to the first millisecond that is not in the rounding interval.2022-06-01T00:00:00.000
gteRounds down to the first millisecond.2022-05-01T00:00:00.000
ltRounds down to the last millisecond before the rounded date.2022-04-30T23:59:59.999
lteRounds up to the last millisecond in the rounding interval.2022-05-31T23:59:59.999

Time zone

By default, dates are assumed to be in Coordinated Universal Time (UTC). If you specify a time_zone parameter in the query, the provided date values are converted to UTC. You can specify the time_zone parameter as a UTC offset, such as -04:00, or an IANA time zone ID, such as America/New_York. For example, the following query specifies that the gte date provided in the query is in the -04:00 time zone:

  1. GET /products/_search
  2. {
  3. "query": {
  4. "range": {
  5. "created": {
  6. "time_zone": "-04:00",
  7. "gte": "2022-04-17T06:00:00"
  8. }
  9. }
  10. }
  11. }

copy

The gte parameter in the preceding query is converted to 2022-04-17T10:00:00 UTC, which is the UTC equivalent of 2022-04-17T06:00:00-04:00.

The time_zone parameter does not affect the now value because now always corresponds to the current system time in UTC.

Parameters

The query accepts the name of the field (<field>) as a top-level parameter:

  1. GET _search
  2. {
  3. "query": {
  4. "range": {
  5. "<field>": {
  6. "gt": 10,
  7. ...
  8. }
  9. }
  10. }
  11. }

copy

In addition to operators, you can specify the following optional parameters for the <field>.

ParameterData typeDescription
formatStringA format for dates in this query. Default is the field’s mapped format.
relationStringIndicates how the range query matches values for range fields. Valid values are:
- INTERSECTS (default): Matches documents whose range field value intersects the range provided in the query.
- CONTAINS: Matches documents whose range field value contains the entire range provided in the query.
- WITHIN: Matches documents whose range field value is entirely within the range provided in the query.
boostFloating-pointBoosts the query by the given multiplier. Useful for searches that contain more than one query. Values in the [0, 1) range decrease relevance, and values greater than 1 increase relevance. Default is 1.
time_zoneStringThe time zone used to convert date values to UTC in the query. Valid values are ISO 8601 UTC offsets and IANA time zone IDs. For more information, see Time zone.

If search.allow_expensive_queries is set to false, range queries on text and keyword fields are not run.