Range field types
The following table lists all range field types that OpenSearch supports.
Field data type | Description |
---|---|
integer_range | A range of integer values. |
long_range | A range of long values. |
double_range | A range of double values. |
float_range | A range of float values. |
ip_range | A range of IP addresses in IPv4 or IPv6 format. Start and end IP addresses may be in different formats. |
date_range | A range of date values. Start and end dates may be in different formats. Internally, all dates are stored as unsigned 64-bit integers representing milliseconds since the epoch. |
Example
Create a mapping with a double range and a date range:
PUT testindex
{
"mappings" : {
"properties" : {
"gpa" : {
"type" : "double_range"
},
"graduation_date" : {
"type" : "date_range",
"format" : "strict_year_month||strict_year_month_day"
}
}
}
}
Index a document with a double range and a date range:
PUT testindex/_doc/1
{
"gpa" : {
"gte" : 1.0,
"lte" : 4.0
},
"graduation_date" : {
"gte" : "2019-05-01",
"lte" : "2019-05-15"
}
}
You can use a Term query or a Range query to search for values within range fields.
Term query
A term query takes a value and matches all range fields for which the value is within the range.
The following query will return document 1 because 3.5 is within the range [1.0, 4.0].
GET testindex/_search
{
"query" : {
"term" : {
"gpa" : {
"value" : 3.5
}
}
}
}
Range query
A range query on a range field returns documents within that range. Along with the field to be matched, you can further specify a date format or relational operators with the following optional parameters:
Parameter | Description |
---|---|
format | A format for dates in this query. Default is the field’s mapped format. |
relation | Provides a relation between the query’s date range and the document’s date range. There are three types of relations that you can specify: 1. intersects matches documents for which there are dates that belong to both the query’s date range and document’s date range. This is the default.2. contains matches documents for which the query’s date range is a subset of the document’s date range.3. within matches documents for which the document’s date range is a subset of the query’s date range. |
To use a date format other than the field’s mapped format in a query, specify it in the format
field.
To see the full description of range query usage, including all range query parameters, see Range query.
Query for all graduation dates in 2019, providing the date range in a “MM/dd/yyyy” format:
GET testindex1/_search
{
"query": {
"range": {
"graduation_date": {
"gte": "01/01/2019",
"lte": "12/31/2019",
"format": "MM/dd/yyyy",
"relation" : "within"
}
}
}
}
The above query will return document 1 for the within
and intersects
relations but will not return it for the contains
relation.
IP address ranges
You can specify IP address ranges in two formats: as a range and in CIDR notation.
Create a mapping with an IP address range:
PUT testindex
{
"mappings" : {
"properties" : {
"ip_address_range" : {
"type" : "ip_range"
},
"ip_address_cidr" : {
"type" : "ip_range"
}
}
}
}
Index a document with IP address ranges in both formats:
PUT testindex/_doc/2
{
"ip_address_range" : {
"gte" : "10.24.34.0",
"lte" : "10.24.35.255"
},
"ip_address_cidr" : "10.24.34.0/24"
}
Parameters
The following table lists the parameters accepted by range field types. All parameters are optional.
Parameter | Description |
---|---|
boost | A floating-point value that specifies the weight of this field toward the relevance score. Values above 1.0 increase the field’s relevance. Values between 0.0 and 1.0 decrease the field’s relevance. Default is 1.0. |
coerce | A Boolean value that signals to truncate decimals for integer values and to convert strings to numeric values. Default is true . |
index | A Boolean value that specifies whether the field should be searchable. Default is true . |
store | A Boolean value that specifies whether the field value should be stored and can be retrieved separately from the _source field. Default is false . |