一、简述
布尔字段接受JSON true和false值,但也可以接受解释为true或false的字符串 (即是:”true”,”false”):
| 布尔值 | es值 |
|---|---|
| true | true,”true” |
| false | false,”false” |
二、实例
1、定义Boolean类型字段
定义文档字段 is_published 为Boolean类型:
PUT boolean_index{"mappings": {"_doc": {"properties": {"is_published": {"type": "boolean"}}}}}
2、插入布尔类型字符串
2.1 插入布尔类型字符串:”true” , “false”
POST boolean_index/_doc/1{"is_published": "true"}POST boolean_index/_doc/2{"is_published": "false"}
2.2 查询布尔值为true的文档
执行查询
GET boolean_index/_search{"query": {"term": {"is_published": true}}}
查询结果
{"took" : 0,"timed_out" : false,"_shards" : {"total" : 5,"successful" : 5,"skipped" : 0,"failed" : 0},"hits" : {"total" : 1,"max_score" : 0.2876821,"hits" : [{"_index" : "boolean_index","_type" : "_doc","_id" : "1","_score" : 0.2876821,"_source" : {"is_published" : "true"}}]}}
三、布尔字段聚合
像term聚合使用1和0作为键,使用字符串“true”和“false”作为键字符串。布尔字段在脚本中使用时,返回1和0:
3.1 聚合查询布尔字段
GET boolean_index/_search{"aggs": {"publish_state": {"terms": {"field": "is_published"}}},"script_fields": {"is_published": {"script": {"lang": "painless","source": "doc['is_published'].value"}}}}
3.2 聚合结果
{"took" : 1,"timed_out" : false,"_shards" : {"total" : 5,"successful" : 5,"skipped" : 0,"failed" : 0},"hits" : {"total" : 2,"max_score" : 1.0,"hits" : [{"_index" : "boolean_index","_type" : "_doc","_id" : "2","_score" : 1.0,"fields" : {"is_published" : [false]}},{"_index" : "boolean_index","_type" : "_doc","_id" : "1","_score" : 1.0,"fields" : {"is_published" : [true]}}]},"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}]}}}
四、布尔字段定义参数解析
详见:布尔字段定义参数
