elasticsearch/docs/reference/elasticsearch/mapping-reference/boolean.md
Colleen McGinnis 9bcd59596d
[docs] Prepare for docs-assembler (#125118)
* reorg files for docs-assembler and create toc.yml files

* fix build error, add redirects

* only toc

* move images
2025-03-20 12:09:12 -05:00

172 lines
5.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
navigation_title: "Boolean"
mapped_pages:
- https://www.elastic.co/guide/en/elasticsearch/reference/current/boolean.html
---
# Boolean field type [boolean]
Boolean fields accept JSON `true` and `false` values, but can also accept strings which are interpreted as either true or false:
False values
: `false`, `"false"`, `""` (empty string)
True values
: `true`, `"true"`
For example:
```console
PUT my-index-000001
{
"mappings": {
"properties": {
"is_published": {
"type": "boolean"
}
}
}
}
POST my-index-000001/_doc/1?refresh
{
"is_published": "true" <1>
}
GET my-index-000001/_search
{
"query": {
"term": {
"is_published": true <2>
}
}
}
```
1. Indexing a document with `"true"`, which is interpreted as `true`.
2. Searching for documents with a JSON `true`.
Aggregations like the [`terms` aggregation](/reference/aggregations/search-aggregations-bucket-terms-aggregation.md) use `1` and `0` for the `key`, and the strings `"true"` and `"false"` for the `key_as_string`. Boolean fields when used in scripts, return `true` and `false`:
```console
POST my-index-000001/_doc/1?refresh
{
"is_published": true
}
POST my-index-000001/_doc/2?refresh
{
"is_published": false
}
GET my-index-000001/_search
{
"aggs": {
"publish_state": {
"terms": {
"field": "is_published"
}
}
},
"sort": [ "is_published" ],
"fields": [
{"field": "weight"}
],
"runtime_mappings": {
"weight": {
"type": "long",
"script": "emit(doc['is_published'].value ? 10 : 0)"
}
}
}
```
## Parameters for `boolean` fields [boolean-params]
The following parameters are accepted by `boolean` fields:
[`doc_values`](/reference/elasticsearch/mapping-reference/doc-values.md)
: Should the field be stored on disk in a column-stride fashion, so that it can later be used for sorting, aggregations, or scripting? Accepts `true` (default) or `false`.
[`index`](/reference/elasticsearch/mapping-reference/mapping-index.md)
: Should the field be quickly searchable? Accepts `true` (default) and `false`. Fields that only have [`doc_values`](/reference/elasticsearch/mapping-reference/doc-values.md) enabled can still be queried using term or range-based queries, albeit slower.
[`ignore_malformed`](/reference/elasticsearch/mapping-reference/ignore-malformed.md)
: Trying to index the wrong data type into a field throws an exception by default, and rejects the whole document. If this parameter is set to true, it allows the exception to be ignored. The malformed field is not indexed, but other fields in the document are processed normally. Accepts `true` or `false`. Note that this cannot be set if the `script` parameter is used.
[`null_value`](/reference/elasticsearch/mapping-reference/null-value.md)
: Accepts any of the true or false values listed above. The value is substituted for any explicit `null` values. Defaults to `null`, which means the field is treated as missing. Note that this cannot be set if the `script` parameter is used.
`on_script_error`
: Defines what to do if the script defined by the `script` parameter throws an error at indexing time. Accepts `fail` (default), which will cause the entire document to be rejected, and `continue`, which will register the field in the documents [`_ignored`](/reference/elasticsearch/mapping-reference/mapping-ignored-field.md) metadata field and continue indexing. This parameter can only be set if the `script` field is also set.
`script`
: If this parameter is set, then the field will index values generated by this script, rather than reading the values directly from the source. If a value is set for this field on the input document, then the document will be rejected with an error. Scripts are in the same format as their [runtime equivalent](docs-content://manage-data/data-store/mapping/map-runtime-field.md).
[`store`](/reference/elasticsearch/mapping-reference/mapping-store.md)
: Whether the field value should be stored and retrievable separately from the [`_source`](/reference/elasticsearch/mapping-reference/mapping-source-field.md) field. Accepts `true` or `false` (default).
[`meta`](/reference/elasticsearch/mapping-reference/mapping-field-meta.md)
: Metadata about the field.
`time_series_dimension`
: (Optional, Boolean)
Marks the field as a [time series dimension](docs-content://manage-data/data-store/data-streams/time-series-data-stream-tsds.md#time-series-dimension). Defaults to `false`.
The `index.mapping.dimension_fields.limit` [index setting](/reference/elasticsearch/index-settings/time-series.md) index setting limits the number of dimensions in an index.
Dimension fields have the following constraints:
* The `doc_values` and `index` mapping parameters must be `true`.
## Synthetic `_source` [boolean-synthetic-source]
::::{important}
Synthetic `_source` is Generally Available only for TSDB indices (indices that have `index.mode` set to `time_series`). For other indices synthetic `_source` is in technical preview. Features in technical preview may be changed or removed in a future release. Elastic will work to fix any issues, but features in technical preview are not subject to the support SLA of official GA features.
::::
`boolean` fields support [synthetic `_source`](/reference/elasticsearch/mapping-reference/mapping-source-field.md#synthetic-source) in their default configuration.
Synthetic source may sort `boolean` field values. For example:
$$$synthetic-source-boolean-example$$$
```console
PUT idx
{
"settings": {
"index": {
"mapping": {
"source": {
"mode": "synthetic"
}
}
}
},
"mappings": {
"properties": {
"bool": { "type": "boolean" }
}
}
}
PUT idx/_doc/1
{
"bool": [true, false, true, false]
}
```
Will become:
```console-result
{
"bool": [false, false, true, true]
}
```