• 【Elasticsearch教程7】Mapping字段类型之boolean


    一、boolean简介

    boolean类型非常简单,它就接受

    传给ES的值判断为
    true,“true”
    false,“false”, “”(空字符)
    nullES会忽略值为null的字段

    二、创建boolean类型的字段

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

    PUT books
    {
        "mappings":{
            "properties":{
                "name":{
                    "type":"keyword"
                },
                "is_published":{
                    "type":"boolean"
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    三、新增数据记录

    创建如下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	 #null字段会被忽略	
    }
    
    PUT books/_doc/7
    {
      "name": "图灵Java面试指导"  #该文档不存在is_published字段
    }
    
    #上面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."
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    四、验证boolean类型查询

    4.1 查询is_published=true

    GET books/_search
    {
      "query": {
        "bool": {
          "filter": [
            {
              "term": {
                "is_published": true
              }
            }
          ]
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    返回如下: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
                }
            }
        ]
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    4.2 查询is_published=false

    GET books/_search
    {
      "query": {
        "bool": {
          "filter": [
            {
              "term": {
                "is_published": false
              }
            }
          ]
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    返回如下: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":""
                }
            }
        ]
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    4.3 对于null缺失字段

    • 总有7条记录,还有2条记录它们不是true也不是false。
    • is_published字段进行exists查询,可查出这2条记录。
    • 在日常开发中一定要注意这些值,否则容易漏处理它们。
    GET books/_search
    {
      "query": {
        "bool": {
          "must_not": [
            {
              "exists": {
                "field": "is_published"
              }
            }
          ]
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    返回出在is_published上不存在值的2个文档

    {
        "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面试指导"
                }
            }
        ]
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    五、在boolean上进行聚合查询

    在boolean上进行terms聚合时,聚合结果中key为0(假)和1(真),key_as_string为false和true。
    按照is_published进行分组聚合:

    GET books/_search
    {
      "aggs": {
        "publish_state": {
          "terms": {
            "field": "is_published"
          }
        }
      },
      "sort": [ "is_published" ]
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    返回结果如下:

    "buckets" : [
            {
              "key" : 0,
              "key_as_string" : "false",
              "doc_count" : 3
            },
            {
              "key" : 1,
              "key_as_string" : "true",
              "doc_count" : 2
            }
    ]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    如果感觉这篇文章对你有帮助,点个免费的赞和关注吧。

  • 相关阅读:
    深圳NPDP认证|如何做好一个B端产品经理?
    Python面试题_第 (5) 章
    ES索引误删的名场面
    web前端期末大作业 在线电影网页设计与制作 HTML+CSS+JavaScript仿叮当电影网页制作
    2022-11-06
    springboot使用@Validated校验不生效
    【C++】class的设计与使用(五)静态类成员
    SSM+图书馆电子文件资源管理 毕业设计-附源码191614
    【专题】测试人员为什么需要学会做业务总结?
    用户行为日志采集脚本lg.sh
  • 原文地址:https://blog.csdn.net/winterking3/article/details/126545873