Skip to content

Casting Values

Edit page

Cast allows explicit conversion from one data type to another as long as types are compatible.

SEARCH /bank/
{
"_query": {
"balance" : {
"_integer": 2221.82
}
}
}

In the above example cast 2221.82 to integer (the fraction is truncated, giving 2221), which later is internally converted to 2221.0 (as the “balance” field is a float). A cast is the one way to convert a value: without it, a value must already be exactly of the field’s type (see Exact Values). A cast truncates a fraction but never wraps: a value outside the type’s range, such as {"_positive": -1}, answers 400.

The following example will convert the integer 84535 to text "84535":

SEARCH /bank/
{
"_query": {
"contact.postcode" : {
"_text": 84535
}
}
}
TypesCompatible Types
_integer_positive, _float, _boolean, _text
_positive_integer, _float, _boolean, _text
_float_integer, _positive, _boolean, _text
_boolean_integer, _positive, _float, _text
_text_integer, _positive, _float, _boolean, _datetime, _keyword and objects
_datetime, _uuid, the geospatial words_text

The query DSL accepts these cast words: _float, _positive, _integer, _boolean, _keyword (and its legacy spellings _term and _string), _text, _datetime, _date, _time, _timedelta, _uuid, and the geospatial ones, _ewkt, _point, _circle, _polygon, _chull, _multipoint, _multicircle, _multipolygon, _multichull, _multiconvex, _geometrycollection and _geometryintersection. A word that is not in the list, such as _geospatial, answers 400 (“Invalid operator”).

_date, _time and _timedelta find the documents whose field holds that date, time or time delta, on a field of that type or with no field at all (the values indexed globally). Globally a time and a time delta share one term space, so _time 10:30:00 also finds a +10:30:00 time delta, and the reverse. A _date is cast as a _datetime. A _datetime can also be spelled as a map of _date and _time, where the _date is a map of _year, _month and _day; a _date that is text there answers 400 (”‘_date’ must be a map value”), and a value that is not a date answers 400 (“Error format in _datetime”).

SEARCH /bank/
{
"_query": {
"joined": {
"_date": "2020-01-01"
}
}
}

Sources: QueryDSL::process in src/query_dsl.cc, src/reserved/types.h:28-53; the truncation and the range checks, Cast::integer and Cast::positive in src/cast.cc.