您当前的位置: 首页 >  ar

陈橙橙丶

暂无认证

  • 6浏览

    0关注

    107博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

elasticSearch核心概念的介绍(七):常见的数据类型

陈橙橙丶 发布时间:2022-02-18 21:49:33 ,浏览量:6

常见的数据类型

在上一章介绍了分词器和ik分词器的使用,有兴趣的可以参考一下 elasticSearch核心概念的介绍(六):常见的中文分词器 在这一章我们介绍常见的字段类型

  • 数据类型
    • 核心数据类型
    • 复杂数据类型
    • 专用数据类型

核心数据类型

  • 字符串

    • text
      • 用于全文索引,该类型的字段将通过分词器进行分词
    • keyword
      • 不分词,只能搜索该字段的完整的值
  • 数值型

    • long,integer,short,byte,double,float,half_float,scaled_float
    • 布尔-boolean
    • 二进制-binary
      • 该类型的字段把值当作经过base64编码的字符串,默认不存储,且不可搜索
    • 范围类型
      • 范围类型标识值是一个范围,而不是一个具体的值
      • integer_range,float_range,long_range,double_range,date_range
      • 例如age的类型是intege_range,那么值可以是{“gte”:20,“lte”:40};搜索 “term”:{“age”,21}可以搜索该值
    • 日期-date
      • 由于json没有date类型,所以es通过识别字符串是否符合format定义的格式来判断是否为date类型
      • format默认为:strict_date_optional_time || epoch_millis
      • 格式
        • “2022-01-01” “2022/01/01 12:10:30” 这种字符串格式
        • 从开始纪元(1970年1月1日0点) 开始的毫秒数
        • 从开始纪元开始的秒数
  • 示例

    • 请求

      • 创建索引和mapping
      curl -X PUT "http://172.45.25.150:9200/nba/_mapping" -H 'Content-Type:application/json' -d '
      {
          "properties":{
              "name":{
                  "type":"text"
              },
              "team_name":{
                  "type":"text"
              },
              "position":{
                  "type":"text"
              },
              "play_year":{
                  "type":"text"
              },
              "jerse_no":{
                  "type":"keyword"
              },
              "title":{
                  "type":"text"
              },
              "age":{
                  "type":"integer_range"
              },
              "date":{
                  "type":"date"
              }
          }
      }
      '
      
    • 新增一条id为1的doc数据(测试age范围类型和date)

      curl -X POST "http://172.45.25.150:9200/nba/_doc/1" -H 'Content-Type:application/json' -d '
      {
          "name":"菜x坤",
          "team_name":"勇士",
          "position":"得分后卫",
          "play_year":10,
          "jerse_no":"31",
          "title":"打球最帅的明显",
          "age":{
              "gte":18,
              "lte":40
          },
          "date":"2022-01-01"
      }
      '
      
    • 新增一条id为2的doc数据(测试date)

      curl -X POST "http://172.45.25.150:9200/nba/_doc/2" -H 'Content-Type:application/json' -d '
      {
          "name":"杨超越",
          "team_name":"猴急",
          "position":"得分后卫",
          "play_year":10,
          "jerse_no":"32",
          "title":"打球最可爱的明显",
          "age":{
              "gte":18,
              "lte":35
          },
          "date":1645190312090
      }
      '
      

      查询

      • 请求
      curl -X POST "http://172.45.25.150:9200/nba/_search" -H 'Content-Type:application/json' -d '
      {
          "query":{
              "term":{
                  "age":35
              }
          }
      }
      '
      
      • 响应

        {
            "took": 0,
            "timed_out": false,
            "_shards": {
                "total": 1,
                "successful": 1,
                "skipped": 0,
                "failed": 0
            },
            "hits": {
                "total": {
                    "value": 2,
                    "relation": "eq"
                },
                "max_score": 1.0,
                "hits": [
                    {
                        "_index": "nba",
                        "_type": "_doc",
                        "_id": "1",
                        "_score": 1.0,
                        "_source": {
                            "name": "菜x坤",
                            "team_name": "勇士",
                            "position": "得分后卫",
                            "play_year": 10,
                            "jerse_no": "31",
                            "title": "打球最帅的明显",
                            "age": {
                                "gte": 18,
                                "lte": 40
                            },
                            "date": "2022-01-01"
                        }
                    },
                    {
                        "_index": "nba",
                        "_type": "_doc",
                        "_id": "2",
                        "_score": 1.0,
                        "_source": {
                            "name": "杨超越",
                            "team_name": "猴急",
                            "position": "得分后卫",
                            "play_year": 10,
                            "jerse_no": "32",
                            "title": "打球最可爱的明显",
                            "age": {
                                "gte": 18,
                                "lte": 35
                            },
                            "date": 1645190312090
                        }
                    }
                ]
            }
        }
        

复杂数据类型

  • 数组类型Array

    • ES 中没有专门的数字类型,直接使用[]定义即可,数组中所有的值必须是同一种类型,不支持混合数据类型的数组
      • 字符串数组 [“one”,“two”]
      • 整数数组 [1,2]
      • Object数组[{“name”:“zhangsan”,“age”,18},{“name”:“lisi”,“age”:19}]
      • 同一个数组只能存同类型的数据,不能混存,例如[1,“abc”]

    Object对象类型

    • 请求

      curl -X POST "http://172.45.25.190:9200/nba/_doc/4" -H 'Content-Type:application/json' -d '
      {
      	"name":"吴一凡",
      	"team_name":"湖人",
      	"position":"得分后卫",
      	"play_year":10,
      	"jerse_no":"33",
      	"title":"最会说唱的明心",
      	"date":1641886870,
      	"array":[
      	"one","two"
      	],
      	"address":{
      		"region":"China",
      		"location":{
      			"province":"ShanDong",
      			"city":"QingDao"
      		}
      	}
      }
      
      '
      
    • 查询

      curl -X POST "http://172.45.25.190:9200/nba/_search" -H 'Content-Type:application/json' -d '
      {
          "query":{
              "match":{ 
                  "address.region":"China"
              }
          }
      }
      '
      
    • 响应

      {
          "took": 10,
          "timed_out": false,
          "_shards": {
              "total": 1,
              "successful": 1,
              "skipped": 0,
              "failed": 0
          },
          "hits": {
              "total": {
                  "value": 1,
                  "relation": "eq"
              },
              "max_score": 0.2876821,
              "hits": [
                  {
                      "_index": "nba",
                      "_type": "_doc",
                      "_id": "4",
                      "_score": 0.2876821,
                      "_source": {
                          "name": "吴一凡",
                          "team_name": "湖人",
                          "position": "得分后卫",
                          "play_year": 10,
                          "jerse_no": "33",
                          "title": "最会说唱的明心",
                          "date": 1641886870,
                          "array": [
                              "one",
                              "two"
                          ],
                          "address": {
                              "region": "China",
                              "location": {
                                  "province": "ShanDong",
                                  "city": "QingDao"
                              }
                          }
                      }
                  }
              ]
          }
      }
      

专业数据类型

  • IP类型

    • IP类型的字段用于存储IPv4或IPv6的地址,本质上是一个长整型字段
  • 示例

    • 修改索引mapping

      curl -X POST "http://172.45.25.190:9200/nba/_mapping" -H 'Content-Type:application/json' -d '
      {
          "properties":{
              "name":{
                  "type":"text"
              },
              "team_name":{
                  "type":"text"
              },
              "position":{
                  "type":"text"
              },
              "play_year":{
                  "type":"text"
              },
              "jerse_no":{
                  "type":"keyword"
              },
              "title":{
                  "type":"text"
              },
              "age":{
                  "type":"integer_range"
              },
              "date":{
                  "type":"date"
              },
              "ip_addr":{
                  "type":"ip"
              }
          }
      }
      '
      
    • 插入文档数据

      curl -X POST "http://172.45.25.190:9200/nba/_doc" -H 'Content-Type:application/json' -d '
      
      {
          "name":"库里",
          "team_name":"勇士",
          "position":"组织后卫",
          "play_year":"10",
          "jerse_no":"30",
          "ip_addr":"192.168.0.1"
      }
      '
      
    • 查询

      curl -X POST "http://172.45.25.190:9200/nba/_search" -H 'Content-Type:application/json' -d '
      {
          "query":{
              "term":{
                  "ip_addr":"192.168.0.1/16"
              }
          }
      }
      '
      

      192.168.0.1/16 查询 192.168.0.1-192.168.0.16之间的范围数据

    • 响应

      {
          "took": 0,
          "timed_out": false,
          "_shards": {
              "total": 1,
              "successful": 1,
              "skipped": 0,
              "failed": 0
          },
          "hits": {
              "total": {
                  "value": 1,
                  "relation": "eq"
              },
              "max_score": 1.0,
              "hits": [
                  {
                      "_index": "nba",
                      "_type": "_doc",
                      "_id": "5",
                      "_score": 1.0,
                      "_source": {
                          "name": "库里",
                          "team_name": "勇士",
                          "position": "组织后卫",
                          "play_year": "10",
                          "jerse_no": "30",
                          "ip_addr": "192.168.0.1"
                      }
                  }
              ]
          }
      }
      
关注
打赏
1648473527
查看更多评论
立即登录/注册

微信扫码登录

0.1728s