Elasticsearch教程(21) 详解mapping之boolean

1 简介

boolean类型非常简单,它就接受真或假。

判断ES接受的值
true,“true”
false,“false”, “”(空字符)

2 创建boolean类型的字段

创建一个books索引,name是书名,is_published是是否发行。

PUT books
{
    "mappings":{
        "properties":{
            "name":{
                "type":"keyword"
            },
            "is_published":{
                "type":"boolean"
            }
        }
    }
}

3 新增数据记录

创建如下7条记录,注意看他们的is_published都不一样。

PUT books/_doc/1
{
  "name": "Java编程思想",
  "is_published": true
}

PUT books/_doc/2
{
  "name": "孙哥说Spring5",
  "is_published": "true"
}

PUT books/_doc/3
{
  "name": "雷丰阳Spring注解",
  "is_published": false
}

PUT books/_doc/4
{
  "name": "小码哥Vue.js教程",
  "is_published": "false"
}

PUT books/_doc/5
{
  "name": "易学教育Flink教程",
  "is_published": ""
}

PUT books/_doc/6
{
  "name": "尚硅谷Flink教程",
  "is_published": null
}

PUT books/_doc/7
{
  "name": "图灵Java面试指导"
}

#上面7个都能成功,这一个会插入失败
PUT books/_doc/8
{
  "name": "尚硅谷Java面试指导",
  "is_published": "啥也不是"
}

返回:
"type" : "illegal_argument_exception",
"reason" : "Failed to parse value [啥也不是] as only [true] or [false] are allowed."
}

4 验证boolean类型查询

4.1 查询is_published=true

GET books/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "is_published": true
          }
        }
      ]
    }
  }
}

返回如下:true和"true"是等价的

{
    "hits":[
        {
            "_index":"books",
            "_type":"_doc",
            "_id":"2",
            "_score":0,
            "_source":{
                "name":"孙哥说Spring5",
                "is_published":"true"
            }
        },
        {
            "_index":"books",
            "_type":"_doc",
            "_id":"1",
            "_score":0,
            "_source":{
                "name":"Java编程思想",
                "is_published":true
            }
        }
    ]
}

4.2 查询is_published=false

GET books/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "is_published": false
          }
        }
      ]
    }
  }
}

返回如下:false,“false"和”"这3个是等价的

{
    "hits":[
        {
            "_index":"books",
            "_type":"_doc",
            "_id":"4",
            "_score":0,
            "_source":{
                "name":"小码哥Vue.js教程",
                "is_published":"false"
            }
        },
        {
            "_index":"books",
            "_type":"_doc",
            "_id":"3",
            "_score":0,
            "_source":{
                "name":"雷丰阳Spring注解",
                "is_published":false
            }
        },
        {
            "_index":"books",
            "_type":"_doc",
            "_id":"5",
            "_score":0,
            "_source":{
                "name":"易学教育Flink教程",
                "is_published":""
            }
        }
    ]
}

4.3 对于null和缺失字段

  • is_published=true的有2个记录,is_published=false的有3条记录。
  • 总有7条记录,还有2条记录它们不是true也不是false。
  • 在日常开发中一定要注意这些值,否则容易漏处理它们。
GET books/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "exists": {
            "field": "is_published"
          }
        }
      ]
    }
  }
}
{
    "hits":[
        {
            "_index":"books",
            "_type":"_doc",
            "_id":"6",
            "_score":0,
            "_source":{
                "name":"尚硅谷Flink教程",
                "is_published":null
            }
        },
        {
            "_index":"books",
            "_type":"_doc",
            "_id":"7",
            "_score":0,
            "_source":{
                "name":"图灵Java面试指导"
            }
        }
    ]
}
  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

瑟 王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值