Boolean 字段类型

Boolean 字段类型接受 truefalse 值,或 "true""false" 字符串。您也可以传递空字符串("")来替代 false 值。

示例

创建一个映射,其中 a、b 和 c 是 Boolean 字段:

PUT testindex
{
  "mappings" : {
    "properties" :  {
      "a" : {
        "type" : "boolean"
      },
      "b" : {
        "type" : "boolean"
      },
      "c" : {
        "type" : "boolean"
      }
    }
  }
}

索引一个包含 Boolean 值的文档:

PUT testindex/_doc/1 
{
  "a" : true,
  "b" : "true",
  "c" : ""
}

结果,ab 将被设置为 truec 将被设置为 false

搜索所有 c 为 false 的文档:

GET testindex/_search 
{
  "query": {
      "term" : {
        "c" : false
    }
  }
}

参数

下表列出了 Boolean 字段类型接受的参数。所有参数均为可选。

参数 描述
boost 浮点值,指定此字段对相关性分数的权重。大于 1.0 的值会增加字段的相关性。0.0 到 1.0 之间的值会降低字段的相关性。默认值为 1.0。
doc_values 布尔值,指定是否应将字段存储在磁盘上,以便用于聚合、排序或脚本操作。默认值为 true
index 布尔值,指定字段是否应可搜索。默认值为 true
meta 接受此字段的元数据。
null_value 用于替代 null 的值。必须与字段类型相同。如果未指定此参数,当字段值为 null 时,该字段将被视为缺失。默认值为 null
store 布尔值,指定字段值是否应存储,并可从 _source 字段单独检索。默认值为 false

聚合和脚本中的 Boolean 值

在 Boolean 字段的聚合中,key 返回数值(true 为 1,false 为 0),而 key_as_string 返回字符串("true""false")。脚本为 Boolean 值返回 truefalse

示例

对字段 a 运行词项聚合查询:

GET testindex/_search
{
  "aggs": {
    "agg1": {
      "terms": {
        "field": "a"
      }
    }
  },
  "script_fields": {
    "a": {
      "script": {
        "lang": "painless",
        "source": "doc['a'].value"
      }
    }
  }
}

脚本返回 a 的值为 truekey 返回 a 的值为 1key_as_string 返回 a 的值为 "true"

{
  "took" : 1133,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "testindex",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "fields" : {
          "a" : [
            true
          ]
        }
      }
    ]
  },
  "aggregations" : {
    "agg1" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : 1,
          "key_as_string" : "true",
          "doc_count" : 1
        }
      ]
    }
  }
}