Elasticsearch——数据类型以及mappings设置

一、数据类型列表
字段类型概述

分类具体类型
字符串类型text,keyword
整数类型integer,long,short,byte
浮点类型double,float,half_float,scaled_float
逻辑类型boolean
日期类型date
范围类型range

二、数据类型说明
类型说明:

数据类型说明
text 类型被全文搜索,字段内容会被分析,text类型的字段不用于排序,很少用于聚合
keyword类型只能通过精确匹配,可用户过滤、排序、聚合
整数类型尽可能选择范围小的数据类型,字段的长度越短,索引和搜索的效率越高。
浮点类型优先考虑使用带缩放因子的scaled_float浮点类型
date类型日期格式的字符串,或long类型的毫秒数,或integer的秒数
binary类型进制字段是指用base64来表示索引中存储的二进制数据,不进行索引,只支持index_name属性

三、Mapping 属性
1、enabled:仅存储、不做搜索和聚合分析

  "enabled":true (缺省)| false

2、index:是否构建倒排索引(即是否分词,设置false,字段将不会被索引)

 "index": true(缺省)| false

3、norms:是否归一化相关参数,存储长度因子和索引时boost,对需要参加评分字段使用,但会额外增加内存消耗

 "norms": {"enable": true, "loading": "lazy"}

4、doc_value:是否开启doc_value,用户聚合和排序分析
对not_analyzed字段,默认都是开启,分词字段不能使用

  "doc_value": true(缺省)| false

5、fielddata:是否为text类型启动fielddata,实现排序和聚合分析
针对分词字段,参与排序或聚合时能提高性能,不分词字段统一建议使用doc_value

  "fielddata": {"format": "disabled"}

6、store:,field values是可索引和搜索的,但是它们不被存储。

  "store": false(默认)| true

7、analyzer:指定分词器,默认分词器为standard analyzer

  "analyzer": "ik"

8、boost:字段级别的分数加权,默认值是1.0

  "boost": 1.23

9、fields:可以对一个字段提供多种索引模式,同一个字段的值,一个分词,一个不分词

  "fields": {"raw": {"type": "string", "index": "not_analyzed"}}

10、ignore_above:超过100个字符的文本,将会被忽略,不被索引

  "ignore_above": 100

11、search_analyzer:设置搜索时的分词器,默认跟analyzer是一致的,比如index时用standard+ngram,搜索时用standard用来完成自动提示功能

  "search_analyzer": "ik"

12、similarity:指定文档的评分模型,默认时TF/IDF算法,指定一个字段评分策略,仅仅对字符串型和分词类型有效。参数有"BM25"(默认),“classic”(TF/IDF), “boolean”(布尔评分模型)

  "similarity": "BM25"

13、trem_vector:默认不存储向量信息,支持参数yes(term存储),with_positions(term+位置),with_offsets(term+偏移量),with_positions_offsets(term+位置+偏移量)对快速高亮fast vector highlighter能提升性能,但开启又会加大索引体积,不适合大数据量用

  "trem_vector": "no"

14、null_value:需要对Null值实现搜索时使用。只有keyword类型才支持设定null_value,该参数可以让值为null的字段变得可index和search。

"null_value": "NULL"

15、 copy_to
这个属性用于将当前字段拷贝到指定字段。

四、实例

{
    "mappings": {
        "blog": { //索引type,7.x开始,不需要在Mapping中指定type信息,默认type为_doc
            "properties": {
                 //标题,不分词,搜索时精确匹配
                "title": {"type": "keyword"}, 
            	//text字段类型,index=true
                "summary": {"type": "text", "index": "true"},
                // integer类型
                "quantity": {"type": "integer"}, 
                // scaled_float类型
                "price": {
                    "type": "scaled_float",  
                    "scaling_factor": 100
                },
                //日期类型
                "pub_date": {"type": "date"},
                //多种日期格式的处理
                "update_date": {
                    "type": "date",
                    "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
                },
                //boolean类型
                "flag": {"type": "boolean"},
                // 预期数量
                "expected_number": {  
                    "type": "integer_range"
                },
                //发展时间线
                "time_line": {
                    "type": "date_range", 
                    "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
                },
                //ip白名单
                "ip_whitelist": {
                    "type": "ip_range"
                }
            }
        }
    }
}

五、其他
1、版本5.X以后

  • 原本type string,其index 可选值为analyzed(默认)和not_analyzed,现在直接拆违type text(index analyzed),type keyword(index not_analyzed)
  • store 可选值为enable或false,指定该字段的原始值是否被写入索引中,默认为enable,即结果中不能返回该字段。
  • index 表示是否用于检索默认enable,可选false
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值