Elasticsearch Mapping的解析、数据类型、Mapping 支持属性、Mapping 字段设置流程

什么是Mapping(映射)
映射(Mapping)相当于数据表的表结构。ElasticSearch中的映射(Mapping)用来定义一个文档,可以定义所包含的字段以及字段的类型、分词器及属性等等。

映射可以分为动态映射和静态映射。

动态映射 (dynamic mapping):在关系数据库中,需要事先创建数据库,然后在该数据库实例下创建数据表,然后才能在该数据表中插入数据。而ElasticSearch中不需要事先定义映射(Mapping),文档写入ElasticSearch时,会根据文档字段自动识别类型,这种机制称之为动态映射。
静态映射 :在ElasticSearch中也可以事先定义好映射,包含文档的各个字段及其类型等,这种方式称之为静态映射。
//静态映射:若映射(mapping)已经创建,写入数据时,elasticsearch会根据映射和写入数据的key进行对比,然后写入相应的映射中;
//动态映射:如果mapping没有创建,elasticsearch将会根据写入的数据的key自行创建相应的mapping,并写入数据。
PUT /myindex/article/1
{
  "post_date": "2018-05-10",
  "title": "Java",
  "content": "java is the best language",
  "author_id": 119
}

//可以使用_mapping查询索引的mapping
GET /myindex/article/_mapping

//返回结果
{
    "myindex": {
        "mapping": {
            "article": {
                "properties": {
                    "author_id": {
                        "type": "long"
                    },
                    "content": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    },
                    "post_date": {
                        "type": "date"
                    },
                    "title": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    }
                }
            }
        }
    }
}

//手动创建mapping
PUT /lib6
{
  "settings": {
     "number_of_shards": 3,
     "number_of_replicas": 0
  },
  "mappings": {
     "books": {
        "properties": {
            "title": {"type": "text"},
            "name": {"type": "text", "ayalyzer": "standard"},
            "publish_date": {"type": "date", "index": false},
            "price": {"type": "double"},
            "number": {"type": "integer"}
        }
     }
  }
}


数据类型及支持属性
由上可见elasticsearch时支持数据类型的。

核心类型(Core datatype)
字符串:string,string类型包含 text 和 keyword。

text:该类型被用来索引长文本,在创建索引前会将这些文本进行分词,转化为词的组合,建立索引;允许es来检索这些词,text类型不能用来排序和聚合。
keyword:该类型不需要进行分词,可以被用来检索过滤、排序和聚合,keyword类型自读那只能用本身来进行检索(不可用text分词后的模糊检索)。
数指型:long、integer、short、byte、double、float

日期型:date

布尔型:boolean

二进制型:binary

PUT /myindex/article/2
{
  "post_date": "2018-05-12",
  "title": "html",
  "content": "I like html",
  "author_id": 120
}

PUT /myindex/article/3
{
  "post_date": "2018-05-16",
  "title": "es",
  "content": "Es is distributed document store",
  "author_id": 110
}

//查不出数据
GET /myindex/article/_search?q=post_date:2018

//返回结果
{
    "took": 198,
    "time_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 0,
        "max_score": null,
        "hits": []
    }
}

//查出数据
GET /myindex/article/_search?q=post_date:2018-05-10

//返回结果
{
    "took": 84,
    "time_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 1,
        "hits": [
            {
                "_index": "myindex",
                "_type": "article",
                "_id": "1",
                "_score": 1,
                "_source": {
                    "post_date": "2018-05-10",
                    "title": "Java",
                    "content": "java is the best language",
                    "author_id": 119
                }
            }
        ]
    }
}

//查出数据
GET /myindex/article/_search?q=content:html

//返回结果
{
    "took": 45,
    "time_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0.2876821,
        "hits": [
            {
                "_index": "myindex",
                "_type": "article",
                "_id": "2",
                "_score": 0.2876821,
                "_source": {
                    "post_date": "2018-05-12",
                    "title": "html",
                    "content": "I like html",
                    "author_id": 120
                }
            }
        ]
    }
}

除字符串类型以外,其他类型必须要进行精确查询,因为除字符串外其他类型不会进行分词。

复杂数据类型(Complex datatypes)
数组类型(Array datatype),数组类型不需要专门指定数组元素的type,例如:

字符型数组:[“one”,“two”]

整型数组*:[1, 2]

数组型数组:[1, [2, 3]] 等价于 [1, 2, 3]

对象数组:[{“name”: “Mary”, “age”: 12}, {“name”: “John”, “age”: 10}]

对象类型(Object datatype):object 用于单个Json对象

//object类型
PUT /lib/person/1
{
  "name": "Tom",
  "age": 25,
  "birthday": "1985-12-12",
  "address": {
     "country": "china",
     "province": "hubei",
     "city": "wuhan"
  }
}

//底层存储格式——符合面向对象思想
{
  "name": ["Tom"],
  "age": [25],
  "birthday": ["1985-12-12"],
  "address.country": ["china"],
  "address.province": ["hubei"],
  "address.city": ["wuhan"]
}

嵌套类型(Nested datatype):nested 用于Json数组

{
  "persons": [
     {"name": "lisi", "age": 27},
     {"name": "wangwu", "age": 26},
     {"name": "zhangsan", "age": 23}
  ]
}

//底层存储
{
  "persons": ["lisi", "wangwu", "zhangsan"],
  "persons": [27, 26, 23]
}


地理位置类型(Geo datatypes)
地理坐标类型(Geo-point datatype):geo_point 用于经纬度坐标

地理形状类型(Geo-Shape datatype):geo_shape 用于类似于多边形的复杂形状

特定类型(Specialised datatypes)
IPv4 类型(IPv4 datatype):ip 用于IPv4 地址

Completion 类型(Completion datatype):completion 提供自动补全建议

Token count 类型(Token count datatype):token_count 用于统计做子标记的字段的index数目,该值会一直增加,不会因为过滤条件而减少

mapper-murmur3 类型:通过插件,可以通过_murmur3_来计算index的哈希值

附加类型(Attachment datatype):采用mapper-attachments插件,可支持_attachments_索引,例如 Microsoft office 格式,Open Documnet 格式, ePub,HTML等

Mapping 支持属性
enabled:仅存储、不做搜索和聚合分析
"enabled":true (缺省)| false
1
index:是否构建倒排索引(即是否分词,设置false,字段将不会被索引)
"index": true(缺省)| false
1
index_option:存储倒排索引的哪些信息
4个可选参数
docs:索引文档号
freqs:文档号+词频
positions:文档号+词频+位置,通常用来距离查询
offsets:文档号+词频+位置+偏移量,通常被使用在高亮字段
分词字段默认时positions,其他默认时docs
"index_options": "docs"
1
norms:是否归一化相关参数、如果字段仅用于过滤和聚合分析、可关闭
分词字段默认配置,不分词字段:默认{“enable”: false},存储长度因子和索引时boost,建议对需要参加评分字段使用,会额外增加内存消耗
"norms": {"enable": true, "loading": "lazy"}
1
doc_value:是否开启doc_value,用户聚合和排序分析
对not_analyzed字段,默认都是开启,分词字段不能使用,对排序和聚合能提升较大性能,节约内存
"doc_value": true(缺省)| false
1
fielddata:是否为text类型启动fielddata,实现排序和聚合分析
针对分词字段,参与排序或聚合时能提高性能,不分词字段统一建议使用doc_value
"fielddata": {"format": "disabled"}
1
store:是否单独设置此字段的是否存储而从_source字段中分离,只能搜索,不能获取值
"store": false(默认)| true
1
coerce:是否开启自动数据类型转换功能,比如:字符串转数字,浮点转整型
"coerce: true(缺省)| false"
1
multifields:灵活使用多字段解决多样的业务需求

dynamic:控制mapping的自动更新

"dynamic": true(缺省)| false | strict
1
data_detection:是否自动识别日期类型
data_detection:true(缺省)| false
1
dynamic和data_detection的详解:Elasticsearch dynamic mapping(动态映射) 策略

analyzer:指定分词器,默认分词器为standard analyzer
"analyzer": "ik"
1
boost:字段级别的分数加权,默认值是1.0
"boost": 1.23
1
fields:可以对一个字段提供多种索引模式,同一个字段的值,一个分词,一个不分词
"fields": {"raw": {"type": "string", "index": "not_analyzed"}}
1
ignore_above:超过100个字符的文本,将会被忽略,不被索引
"ignore_above": 100
1
include_in_all:设置是否此字段包含在_all字段中,默认时true,除非index设置成no
"include_in_all": true
1
null_value:设置一些缺失字段的初始化,只有string可以使用,分词字段的null值也会被分词
"null_value": "NULL"
1
position_increament_gap:影响距离查询或近似查询,可以设置在多值字段的数据上或分词字段上,查询时可以指定slop间隔,默认值时100
"position_increament_gap": 0
1
search_analyzer:设置搜索时的分词器,默认跟analyzer是一致的,比如index时用standard+ngram,搜索时用standard用来完成自动提示功能
"search_analyzer": "ik"
1
similarity:默认时TF/IDF算法,指定一个字段评分策略,仅仅对字符串型和分词类型有效
"similarity": "BM25"
1
trem_vector:默认不存储向量信息,支持参数yes(term存储),with_positions(term+位置),with_offsets(term+偏移量),with_positions_offsets(term+位置+偏移量)对快速高亮fast vector highlighter能提升性能,但开启又会加大索引体积,不适合大数据量用
"trem_vector": "no"
1
Mapping 字段设置流程

--------------------- 
作者:迷途码界 
来源:CSDN 
原文:https://blog.csdn.net/zx711166/article/details/81667862 
版权声明:本文为博主原创文章,转载请附上博文链接!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值