ELK合集(七):Elasticsearch关键术语之Field字段 持续更新

Elasticsearch关键术语 系列博文 目的只用来了解概念 ; 其中 涉及到的配置和使用 是为了方便日后使用时查询的

Field字段

数据类型

简单类型
  • text
    在这里插入图片描述

    • 概念
      • 一段文本 全文本 是非结构化的文本数据 ; 数组类型[]在ES属于Text类型
    • 使用时机
      • 当一个字段需要全文搜索时
    • 作用
      • 索引时 分词 (如果想精确匹配 可在mapping时将text类型设置为keyword类型 keyword不会做分词处理 用字段名.keyword搜索可精确匹配)
      • 不能排序
      • 也很少用于聚合
    • 配置
      • 不需要存储 默认存储
        "index": "false"
        
        语法示例
        PUT indexname
        {
            "mappings": {
                "blog": {
                    "properties": {
                        "summary": {
                        	"type": "text", 
                        	"index": "false"
                       	}
                    }
                }
            }
        }
        
  • keyword
    在这里插入图片描述

    • 概念
      • 包括 数字 日期 一个具体的字符串
    • 使用时机
      • 当一个字段需要按精确值 进行过滤 排序 聚合等操作时
    • 作用
      • 索引时 不分词
    • 配置
      • 不需要存储 默认存储
        "index": "true"
        
        PUT indexname
        {
            "mappings": {
                "blog": {
                    "properties": {
                        "tags": {"type": "keyword", "index": "true"}
                    }
                }
            }
        }
        
  • 数字类型

    • 使用时机
      • 尽可能选择范围小的 索引和搜索效率越高
    • 分类 (8种)
      • byte – 有符号的8位整数 [-128 ~ 127]
      • short – 有符号的16位整数 [-32768 ~ 32767]
      • integer – 有符号的32位整数 [ − 2 31 -2^{31} 231 ~ 2 31 2^{31} 231-1]
      • long – 有符号的64位整数 [ − 2 63 -2^{63} 263 ~ 2 63 2^{63} 263-1]
      • float – 32位单精度浮点数
      • double – 64位双精度浮点数
  • date

    • 概念

      • 日期类型
    • 默认时区

      • UTC格式
    • 默认日期格式 (接受数据的格式)

      • strict_date_optional_timeSolr默认日期格式 或 epoch_millis时间毫秒值
        # 语法示例0 : strict_date_optional_time
        PUT indexname/blog/1
        { "pub_date": "2018-10-10T12:00:00Z" }
        
        # 语法示例1 : strict_date_optional_time
        PUT indexname/blog/2
        { "pub_date": "2018-10-10" }
        
        # 语法示例2 : epoch_millis
        PUT website/blog/13
        { "pub_date": "1589584930103" } 
        
    • 配置

      • 配置日期格式
        # 添加映射
        PUT indexname
        {
            "mappings": {
                "blog": {
                    "properties": {
                        "date": {
                            "type": "date",  
                            "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" # 可以接受的日期格式 多个格式用||连接
                        }
                    }
                }
            }
        }
        
  • boolean

    • 真值
      • true
      • “true”
      • “on”
      • “yes”
      • “1”
    • 假值
      • false
      • “false”
      • “off”
      • “no”
      • “0”
      • “”
      • 0.0
      • 0
  • binary

    • 使用时机
      • 当字段值是Base64编码的字符串时
  • ip

  • null


复杂类型
  • 数组

    • 使用时机
      • ES没有专门的数组类型 插入数据时 数据是数组 直接使用[]即可 但[]内必须是同一种数据类型
    • 注意
      • 动态添加数据时 数组中的第一值的类型 就是数组的数据类型
      • 数组可包含null值 空数组[]会被当作没有值的字段
  • Object

    • 概念
      • 对象类型
    • 注意
      • 对象可多层嵌套

    示例

    # 插入对象类型的数据
    PUT employee/developer/1
    {
        "name": "ma_shoufeng",
        "address": {
            "region": "China",
            "location": {"province": "GuangDong", "city": "GuangZhou"}
        }
    }
    
    # 内部存储方式
    {
        "name":                       "ma_shoufeng",
        "address.region":             "China",
        "address.location.province":  "GuangDong", 
        "address.location.city":      "GuangZhou"
    }
    
    # 映射结构
    PUT employee
    {
        "mappings": {
            "developer": {
                "properties": {
                    "name": { "type": "text", "index": "true" }, 
                    "address": {
                        "properties": {
                            "region": { "type": "keyword", "index": "true" },
                            "location": {
                                "properties": {
                                    "province": { "type": "keyword", "index": "true" },
                                    "city": { "type": "keyword", "index": "true" }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    
  • nested

    • 概念
      • 嵌套类型 (是Object类型的一个特例)
    • 使用时机
      • 当插入数据为array类型 且需要索引和搜索时
    • 本质
      • 嵌套对象实质是将每个对象分离出来 作为隐藏文档进行索引

    示例0 : array类型的对象 mapping中设置的数据类型是Object 搜索会有问题

      	```bash
      	# 添加数据
      	PUT game_of_thrones/role/1
      	{
      	    "group": "stark",
      	    "performer": [
      	        {"first": "John", "last": "Snow"},
      	        {"first": "Sansa", "last": "Stark"}
      	    ]
      	}
      	```
    
    # 内部存储结构
    {
        "group":             "stark",
        "performer.first": [ "john", "sansa" ],
        "performer.last":  [ "snow", "stark" ]
    }
    
      ```bash
      # 存储分析
      可以看出 user.first和user.last会被平铺为多值字段 这样一来 John和Snow之间的关联性就丢失了
      ```
    
    # 查询问题分析
    在查询时 可能出现John Stark的结果
    

    示例1 : 可用nested类型解决object类型的不足

    # 添加映射
    PUT game_of_thrones
    {
        "mappings": {
            "role": {
                "properties": {
                    "performer": {"type": "nested" }
                }
            }
        }
    }
    
# 添加数据
PUT game_of_thrones/role/1
{
    "group" : "stark",
    "performer" : [
        {"first": "John", "last": "Snow"},
        {"first": "Sansa", "last": "Stark"}
    ]
}
# 查询数据
GET game_of_thrones/_search
{
    "query": {
        "nested": {
            "path": "performer",
            "query": {
                "bool": {
                    "must": [
                        { "match": { "performer.first": "John" }},
                        { "match": { "performer.last":  "Snow" }} 
                    ]
                }
            }, 
            "inner_hits": {
                "highlight": {
                    "fields": {"performer.first": {}}
                }
            }
        }
    }
}


参考
阮一名资料
官方文档
百度

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Cheese海盐芝士

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

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

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

打赏作者

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

抵扣说明:

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

余额充值