Range field types

Range field types represent a continuous range of values between an upper and lower bound. For example, a range can represent any date in October or any integer from 0 to 9. They are defined using the operators gt or gte for the lower bound, and lt or lte for the upper bound. They can be used for querying, and have limited support for aggregations. The only supported aggregations are histogram, cardinality.

The following range types are supported:

integer_range

A range of signed 32-bit integers with a minimum value of -231 and maximum of 231-1.

float_range

A range of single-precision 32-bit IEEE 754 floating point values.

long_range

A range of signed 64-bit integers with a minimum value of -263 and maximum of 263-1.

double_range

A range of double-precision 64-bit IEEE 754 floating point values.

date_range

A range of date values. Date ranges support various date formats through the format mapping parameter. Regardless of the format used, date values are parsed into an unsigned 64-bit integer representing milliseconds since the Unix epoch in UTC. Values containing the now date math expression are not supported.

ip_range

A range of ip values supporting either IPv4 or IPv6 (or mixed) addresses.

Below is an example of configuring a mapping with various range fields followed by an example that indexes several range types.

  1. PUT range_index
  2. {
  3. "settings": {
  4. "number_of_shards": 2
  5. },
  6. "mappings": {
  7. "properties": {
  8. "expected_attendees": {
  9. "type": "integer_range"
  10. },
  11. "time_frame": {
  12. "type": "date_range",
  13. "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
  14. }
  15. }
  16. }
  17. }
  18. PUT range_index/_doc/1?refresh
  19. {
  20. "expected_attendees" : {
  21. "gte" : 10,
  22. "lt" : 20
  23. },
  24. "time_frame" : {
  25. "gte" : "2015-10-31 12:00:00",
  26. "lte" : "2015-11-01"
  27. }
  28. }

date_range types accept the same field parameters defined by the date type.

Example indexing a meeting with 10 to 20 attendees, not including 20.

Example date range using date time stamp.

The following is an example of a term query on the integer_range field named “expected_attendees”. 12 is a value inside the range, so it will match.

  1. GET range_index/_search
  2. {
  3. "query" : {
  4. "term" : {
  5. "expected_attendees" : {
  6. "value": 12
  7. }
  8. }
  9. }
  10. }

The result produced by the above query.

  1. {
  2. "took": 13,
  3. "timed_out": false,
  4. "_shards" : {
  5. "total": 2,
  6. "successful": 2,
  7. "skipped" : 0,
  8. "failed": 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value": 1,
  13. "relation": "eq"
  14. },
  15. "max_score" : 1.0,
  16. "hits" : [
  17. {
  18. "_index" : "range_index",
  19. "_type" : "_doc",
  20. "_id" : "1",
  21. "_score" : 1.0,
  22. "_source" : {
  23. "expected_attendees" : {
  24. "gte" : 10, "lt" : 20
  25. },
  26. "time_frame" : {
  27. "gte" : "2015-10-31 12:00:00", "lte" : "2015-11-01"
  28. }
  29. }
  30. }
  31. ]
  32. }
  33. }

The following is an example of a date_range query over the date_range field named “time_frame”.

  1. GET range_index/_search
  2. {
  3. "query" : {
  4. "range" : {
  5. "time_frame" : {
  6. "gte" : "2015-10-31",
  7. "lte" : "2015-11-01",
  8. "relation" : "within"
  9. }
  10. }
  11. }
  12. }

Range queries work the same as described in range query.

Range queries over range fields support a relation parameter which can be one of WITHIN, CONTAINS, INTERSECTS (default).

This query produces a similar result:

  1. {
  2. "took": 13,
  3. "timed_out": false,
  4. "_shards" : {
  5. "total": 2,
  6. "successful": 2,
  7. "skipped" : 0,
  8. "failed": 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value": 1,
  13. "relation": "eq"
  14. },
  15. "max_score" : 1.0,
  16. "hits" : [
  17. {
  18. "_index" : "range_index",
  19. "_type" : "_doc",
  20. "_id" : "1",
  21. "_score" : 1.0,
  22. "_source" : {
  23. "expected_attendees" : {
  24. "gte" : 10, "lt" : 20
  25. },
  26. "time_frame" : {
  27. "gte" : "2015-10-31 12:00:00", "lte" : "2015-11-01"
  28. }
  29. }
  30. }
  31. ]
  32. }
  33. }

IP Range

In addition to the range format above, IP ranges can be provided in CIDR notation:

  1. PUT range_index/_mapping
  2. {
  3. "properties": {
  4. "ip_allowlist": {
  5. "type": "ip_range"
  6. }
  7. }
  8. }
  9. PUT range_index/_doc/2
  10. {
  11. "ip_allowlist" : "192.168.0.0/16"
  12. }

Parameters for range fields

The following parameters are accepted by range types:

coerce

Try to convert strings to numbers and truncate fractions for integers. Accepts true (default) and false.

boost

Mapping field-level query time boosting. Accepts a floating point number, defaults to 1.0.

index

Should the field be searchable? Accepts true (default) and false.

store

Whether the field value should be stored and retrievable separately from the _source field. Accepts true or false (default).