【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"
            }
        }
    }
}

三、新增数据记录

创建如下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."
}

四、验证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缺失字段

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

返回出在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面试指导"
            }
        }
    ]
}

五、在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" ]
}

返回结果如下:

"buckets" : [
        {
          "key" : 0,
          "key_as_string" : "false",
          "doc_count" : 3
        },
        {
          "key" : 1,
          "key_as_string" : "true",
          "doc_count" : 2
        }
]

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

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以使用Java客户端创建索引index和映射mapping,下面是示例代码: ```java import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest; import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.mapper.ObjectMapper; import org.elasticsearch.index.mapper.ObjectMapper.Builder; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class CreateIndexAndMapping { private RestHighLevelClient client; public CreateIndexAndMapping(RestHighLevelClient client) { this.client = client; } public void createIndex(String indexName) throws IOException { CreateIndexRequest request = new CreateIndexRequest(indexName); CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT); boolean acknowledged = response.isAcknowledged(); if (acknowledged) { System.out.println("Index created successfully."); } } public void createMapping(String indexName, String typeName) throws IOException { PutMappingRequest request = new PutMappingRequest(indexName); request.type(typeName); XContentBuilder builder = XContentFactory.jsonBuilder(); builder.startObject(); { builder.startObject("properties"); { builder.startObject("title"); { builder.field("type", "text"); } builder.endObject(); builder.startObject("author"); { builder.field("type", "text"); } builder.endObject(); builder.startObject("publish_date"); { builder.field("type", "date"); } builder.endObject(); builder.startObject("price"); { builder.field("type", "double"); } builder.endObject(); builder.startObject("description"); { builder.field("type", "text"); } builder.endObject(); } builder.endObject(); } builder.endObject(); request.source(builder); PutMappingResponse response = client.indices().putMapping(request, RequestOptions.DEFAULT); boolean acknowledged = response.isAcknowledged(); if (acknowledged) { System.out.println("Mapping created successfully."); } } } ``` 上述代码中,`createIndex`方法用于创建索引,`createMapping`方法用于创建映射。在`createMapping`方法中,我们使用`XContentBuilder`来构建映射的JSON格式,然后将其作为请求的source进行设置。在这个例子中,我们定义了一个包含`title`、`author`、`publish_date`、`price`和`description`等字段的文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

瑟 王

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

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

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

打赏作者

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

抵扣说明:

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

余额充值