Numbers, Booleans and Dates
"Books under 15 that are in stock, published in the last five years." Every part of that request depends on three field types being mapped correctly: numeric, boolean and date. Get them right and the filter is fast and exact; get them wrong and it quietly misbehaves.
The rule
Pick the smallest type that safely fits; dates are stored as instants in UTC.
Numeric types come in whole and decimal families:
| Type | Holds |
|---|---|
byte, short, integer, long |
Whole numbers, growing range (long is 64-bit) |
float, half_float, double |
Decimals, with 32-, 16- and 64-bit precision |
scaled_float |
A decimal stored as a whole number times a fixed scaling_factor |
For money, scaled_float with "scaling_factor": 100 stores 12.5 as 1250 — exact to the cent and compact. Page counts fit in integer. Smaller types save disk, but only if every value fits.
Boolean holds true or false. It also accepts the strings "true" and "false", but nothing else — "yes" or 1 is rejected.
Date fields accept a date string, a date-time string, or a number of milliseconds since the epoch. Internally each date becomes one instant in UTC, so comparisons are exact. The default format accepts ISO strings such as "2019-04-02" and "2019-04-02T10:30:00Z"; you can declare your own:
"published": { "type": "date", "format": "yyyy-MM-dd||epoch_millis" }Numbers and dates are what range queries and date math work on:
GET /books/_search
{
"query": { "range": { "published": { "gte": "now-5y/d" } } }
}now-5y/d means "five years ago, rounded down to the start of that day".