Using the Range Fields type
Sometimes, we have values that represent a continuous range of values between an upper and lower bound. Some of the common scenarios of this are as follows:
- Price range (that is, from $4 to $10)
- Date interval (that is, from 8 A.M. to 8 P.M., December 2020, Summer 2021, Q3 2020, and so on)
In this case, for most queries, pointing to a value in the middle of them is not easy in Elasticsearch; for example, the worst case is to convert continuous values into discrete ones by extracting all the values using a prefixed interval. This kind of situation will largely increase the size of the index and reduce performance (queries).
Range mappings were created to provide continuous value support in Elasticsearch. For this reason, when it is not possible to store the exact value, but we have a range, we need to use range types.
Getting ready
You will need an up-and-running Elasticsearch installation, as we described in the Downloading and installing Elasticsearch recipe of Chapter 1, Getting Started.
To execute the commands in this recipe, you can use any HTTP client, such as curl (https://curl.haxx.se/), Postman (https://www.getpostman.com/), or similar. I suggest using the Kibana console, which provides code completion and better character escaping for Elasticsearch.
How to do it...
We want to use range types to implement stock mark values that are defined by low and high price values and the timeframe of the transaction. To achieve this, follow these steps:
- To populate our stock, we need to create an index with range fields. Let's use the following mapping:
PUT test-range { "mappings": { "properties": { "price": { "type": "float_range" }, "timeframe": { "type": "date_range" } } } }
- Now, we can store some documents, as shown here:
PUT test-range/_bulk {"index":{"_index":"test-range","_id":"1"}} {"price":{"gte":1.5,"lt":3.2},"timeframe":{"gte":"2022-01-01T12:00:00","lt":"2022-01-01T12:00:01"}} {"index":{"_index":"test-range","_id":"2"}} {"price":{"gte":1.7,"lt":3.7},"timeframe":{"gte":"2022-01-01T12:00:01","lt":"2022-01-01T12:00:02"}} {"index":{"_index":"test-range","_id":"3"}} {"price":{"gte":1.3,"lt":3.3},"timeframe":{"gte":"2022-01-01T12:00:02","lt":"2022-01-01T12:00:03"}}
- Now, we can execute a query for filtering on price and timeframe values to check the correct indexing of the data:
GET test-range/_search { "query": { "bool": { "filter": [ { "term": { "price": { "value": 2.4 } } }, { "term": { "timeframe": { "value": "2022-01-01T12:00:02" } } } ] } } }
The result will be something similar to the following:
{ …truncated… "hits" : [ { "_index" : "test-range", "_id" : "3", "_score" : 0.0, "_source" : { "price" : { "gte" : 1.3, "lt" : 3.3 }, "timeframe" : { "gte" : "2022-01-01T12:00:02", "lt" : "2022-01-01T12:00:03" …truncated… }
How it works…
Not all the base types that support ranges can be used in ranges. The possible range types that are supported by Elasticsearch are as follows:
integer_range
: This is used to store signed 32-bit integer values.float_range
: This is used to store signed 32-bit floating-point values.long_range
: This is used to store signed 64-bit integer values.double_range
: This is used to store signed 64-bit floating-point values.date_range
: This is used to store date values as 64-bit integers.ip_range
: This is used to store IPv4 and IPv6 values.
These range types are very useful for all cases where the values are not exact.
When you're storing a document in Elasticsearch, the field can be composed using the following parameters:
gt
orgte
for the lower bound of the rangelt
orlte
for the upper bound of the rangeNote
Range types can be used for querying values, but they have limited support for aggregation: they only support histogram and cardinality aggregations.