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面试指导"
            }
        }
    ]
}

猜你喜欢

转载自blog.csdn.net/winterking3/article/details/108323817