<commit_before>[[boolean]]
=== Boolean field type
++++
<titleabbrev>Boolean</titleabbrev>
++++

Boolean fields accept JSON `true` and `false` values, but can also accept
strings which are interpreted as either true or false:

[horizontal]
False values::

    `false`, `"false"`, `""` (empty string)

True values::

    `true`, `"true"`

For example:

[source,console]
--------------------------------------------------
PUT my-index-000001
{
  "mappings": {
    "properties": {
      "is_published": {
        "type": "boolean"
      }
    }
  }
}

POST my-index-000001/_doc/1
{
  "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 <<search-aggregations-bucket-terms-aggregation,`terms`
aggregation>>  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 `1` and `0`:

[source,console]
--------------------------------------------------
POST my-index-000001/_doc/1
{
  "is_published": true
}

POST my-index-000001/_doc/2
{
  "is_published": false
}

GET my-index-000001/_search
{
  "aggs": {
    "publish_state": {
      "terms": {
        "field": "is_published"
      }
    }
  },
  "script_fields": {
    "is_published": {
      "script": {
        "lang": "painless",
        "source": "doc['is_published'].value"
      }
    }
  }
}
--------------------------------------------------

[[boolean-params]]
==== Parameters for `boolean` fields

The following parameters are accepted by `boolean` fields:

[horizontal]

<<doc-values,`doc_values`>>::

    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`.

<<mapping-index,`index`>>::

    Should the field be searchable? Accepts `true` (default) and `false`.

<<null-value,`null_value`>>::

    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.

<<mapping-store,`store`>>::

    Whether the field value should be stored and retrievable separately from
    the <<mapping-source-field,`_source`>> field. Accepts `true` or `false`
    (default).

<<mapping-field-meta,`meta`>>::

    Metadata about the field.
<commit_msg>Convert `boolean` field example to runtime fields (#71341)<commit_after>[[boolean]]
=== Boolean field type
++++
<titleabbrev>Boolean</titleabbrev>
++++

Boolean fields accept JSON `true` and `false` values, but can also accept
strings which are interpreted as either true or false:

[horizontal]
False values::

    `false`, `"false"`, `""` (empty string)

True values::

    `true`, `"true"`

For example:

[source,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>
    }
  }
}
----
// TEST[s/_search/_search?filter_path=hits.hits/]
<1> Indexing a document with `"true"`, which is interpreted as `true`.
<2> Searching for documents with a JSON `true`.

////
[source,console-result]
----
{
  "hits": {
    "hits": [
      {
        "_id": "1",
        "_index": "my-index-000001",
        "_score": "$body.hits.hits.0._score",
        "_source": {
          "is_published": "true"
        }
      }
    ]
  }
}
----
////

Aggregations like the <<search-aggregations-bucket-terms-aggregation,`terms`
aggregation>>  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`:

[source,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)"
    }
  }
}
----
// TEST[s/_search/_search?filter_path=aggregations,hits.hits/]

////
[source,console-result]
----
{
  "aggregations": {
    "publish_state": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": 0,
          "key_as_string": "false",
          "doc_count": 1
        },
        {
          "key": 1,
          "key_as_string": "true",
          "doc_count": 1
        }
      ]
    }
  },
  "hits": {
    "hits": [
      {
        "_id": "2",
        "_index": "my-index-000001",
        "_score": null,
        "_source": {
          "is_published": false
        },
        "sort": [0],
        "fields": {"weight": [0]}
      },
      {
        "_id": "1",
        "_index": "my-index-000001",
        "_score": null,
        "_source": {
          "is_published": true
        },
        "sort": [1],
        "fields": {"weight": [10]}
      }
    ]
  }
}
----
////

[[boolean-params]]
==== Parameters for `boolean` fields

The following parameters are accepted by `boolean` fields:

[horizontal]

<<doc-values,`doc_values`>>::

    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`.

<<mapping-index,`index`>>::

    Should the field be searchable? Accepts `true` (default) and `false`.

<<null-value,`null_value`>>::

    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.

<<mapping-store,`store`>>::

    Whether the field value should be stored and retrievable separately from
    the <<mapping-source-field,`_source`>> field. Accepts `true` or `false`
    (default).

<<mapping-field-meta,`meta`>>::

    Metadata about the field.

{'fraction_non_alphanumeric': 0.19259723964868256, 'fraction_numerical': 0.0208594730238394, 'mean_word_length': 2.460119370591427, 'pattern_counts': {'":': 76, '<': 43, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5316094', 'n_tokens_mistral': 2194, 'n_tokens_neox': 2067, 'n_words': 561}