Boolean field type

A Boolean field type takes true or false values, or "true" or "false" strings. You can also pass an empty string ("") in place of a false value.

Example

Create a mapping where a, b, and c are Boolean fields:

  1. PUT testindex
  2. {
  3. "mappings" : {
  4. "properties" : {
  5. "a" : {
  6. "type" : "boolean"
  7. },
  8. "b" : {
  9. "type" : "boolean"
  10. },
  11. "c" : {
  12. "type" : "boolean"
  13. }
  14. }
  15. }
  16. }

copy

Index a document with Boolean values:

  1. PUT testindex/_doc/1
  2. {
  3. "a" : true,
  4. "b" : "true",
  5. "c" : ""
  6. }

copy

As a result, a and b will be set to true, and c will be set to false.

Search for all documents where c is false:

  1. GET testindex/_search
  2. {
  3. "query": {
  4. "term" : {
  5. "c" : false
  6. }
  7. }
  8. }

copy

Parameters

The following table lists the parameters accepted by Boolean field types. All parameters are optional.

ParameterDescription
boostA floating-point value that specifies the weight of this field toward the relevance score. Values above 1.0 increase the field’s relevance. Values between 0.0 and 1.0 decrease the field’s relevance. Default is 1.0.
doc_valuesA Boolean value that specifies whether the field should be stored on disk so that it can be used for aggregations, sorting or scripting. Default is false.
indexA Boolean value that specifies whether the field should be searchable. Default is true.
metaAccepts metadata for this field.
null_valueA value to be used in place of null. Must be of the same type as the field. If this parameter is not specified, the field is treated as missing when its value is null. Default is null.
storeA Boolean value that specifies whether the field value should be stored and can be retrieved separately from the _source field. Default is false.

Boolean values in aggregations and scripts

In aggregations on Boolean fields, key returns numeric values (1 for true or 0 for false), and key_as_string returns strings ("true" or "false"). Scripts return true and false for Boolean values.

Example

Run a terms aggregation query on the field a:

  1. GET testindex/_search
  2. {
  3. "aggs": {
  4. "agg1": {
  5. "terms": {
  6. "field": "a"
  7. }
  8. }
  9. },
  10. "script_fields": {
  11. "a": {
  12. "script": {
  13. "lang": "painless",
  14. "source": "doc['a'].value"
  15. }
  16. }
  17. }
  18. }

copy

The script returns the value of a as true, key returns the value of a as 1, and key_as_string returns the value of a as "true":

  1. {
  2. "took" : 1133,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 1,
  13. "relation" : "eq"
  14. },
  15. "max_score" : 1.0,
  16. "hits" : [
  17. {
  18. "_index" : "testindex",
  19. "_type" : "_doc",
  20. "_id" : "1",
  21. "_score" : 1.0,
  22. "fields" : {
  23. "a" : [
  24. true
  25. ]
  26. }
  27. }
  28. ]
  29. },
  30. "aggregations" : {
  31. "agg1" : {
  32. "doc_count_error_upper_bound" : 0,
  33. "sum_other_doc_count" : 0,
  34. "buckets" : [
  35. {
  36. "key" : 1,
  37. "key_as_string" : "true",
  38. "doc_count" : 1
  39. }
  40. ]
  41. }
  42. }
  43. }