Mapping 映射
映射是定义文档的存储和索引方式的过程。
es的mapping有点类型EDB的表结构,在mysql中表结构中有字段名称,类型、长度、索引等
在mapping中也存在字段名称、字段类型,分词器、评分、索引的等。
es支持的数据类型
常见类型
- 数字类型
- long
- integer
- short
- byte
- double
- float
- half_float
- scaled_float
- unsingned_float
- keywords
- keyword:适用于索引结构化的字段,只能被精准匹配(exact_value),id为keyword
- constant_keyword:始终包含相同值的关键字类型
- wildcard
- Dates:时间类型
- alias:别名
- binary:二进制
- range:区间了欧系
text:全文检索字段,生成倒排索引(是为了做文本检索),一般不创建正排索引用于排序和聚合。
对象关系类型
object:单个json对象
- nested:json数组
- flattened:将json对象索引成单个字段
结构化类型:
- geo_point:经纬度积分
- geo_shape:多边形等复杂形状
- point:笛卡尔坐标系
- shape:笛卡尔任意几何图形
特殊类型
- ip:用于ipv4和IPV6
- completion
array数组
es中数组不需要专门的数据类型,任何字段都可以包含一个或者多个值,但是类型需要相同
新增
date_nanos:date plus 纳秒
features
两种映射类型
- dynamic field mapping 自动映射
- 整数 :long
- 浮点性:float
- true/false : boolean
- 日期:date
- 数组:取决于数组中的类型
- 对象:object
- 字符串:不是数字和日期那么是text和keyword类型
- expilcit field mapping 手动映射(除了以上都是需要手动的)
以下相当于手动建立表结构
PUT /product
{
"mapping":{
"properties":{
"field":{
"mapping_parameter": "parameter_value"
}
}
}
}
映射参数
- index:是否对当前字段创建倒排索引,默认为true
- analyzer:分词器
- boost:评分权重
- coerce:是否允许类型转换
- copy_to:可以将多个字段的值复制到组字段中
- doc_value:可以提升排序和聚合的效率 ,如果不需要建立正排索引可以禁用,以节省磁盘空间。
- dynamic:控制是否可以动态的创建新字段
#Dynamic mapping
DELETE product_mapping
GET product_mapping/_mapping
PUT /product_mapping/_doc/1
{
"name": "xiaomi phone",
"desc": "shouji zhong de zhandouji",
"count": 123456,
"price": 123.123,
"date": "2020-05-20",
"isdel": false,
"tags": [
"xingjiabi",
"fashao",
"buka"
]
}
#手工创建mapping(fields的mapping只能创建,无法修改)
#语法
GET product/_mapping
PUT /product
{
"mappings": {
"properties": {
"date": {
"type": "text"
}
}
}
}
GET product/_mapping
#1 index
#案例
PUT /product
{
"mappings": {
"properties": {
"date": {
"type": "text"
},
"desc": {
"type": "text",
"analyzer": "english"
},
"name": {
"type": "text",
"index": "false"
},
"price": {
"type": "long"
},
"tags": {
"type": "text",
"index": "true"
},
"parts": {
"type": "object"
},
"partlist": {
"type": "nested"
}
}
}
}
#插入数据
GET product/_mapping
PUT /product/_doc/1
{
"name": "xiaomi phone",
"desc": "shouji zhong de zhandouji",
"count": 123456,
"price": 3999,
"date": "2020-05-20",
"isdel": false,
"tags": [
"xingjiabi",
"fashao",
"buka"
],
"parts": {
"name": "adapter",
"desc": "5V 2A"
},
"partlist": [
{
"name": "adapter",
"desc": "5V 2A"
},
{
"name": "USB-C",
"desc": "5V 2A 1.5m"
},
{
"name": "erji",
"desc": "boom"
}
]
}
#查看
GET /product/_search
{
"query": {
"match_all": {}
}
}
#验证
GET /product/_search
{
"query": {
"match": {
"name": "xiaomi"
}
}
}
#copy_to
PUT copy_to
{
"mappings": {
"properties": {
"field1": {
"type": "text",
"copy_to": "field_all"
},
"field2": {
"type": "text",
"copy_to": "field_all"
},
"field_all": {
"type": "text"
}
}
}
}
PUT copy_to/_doc/1
{
"field1": "field1",
"field2": "field2"
}
GET copy_to/_search
GET copy_to/_search
{
"query": {
"match": {
"field_all": {
"query": "field1 field2"
}
}
}
}
#coerce:是否允许强制类型转换
PUT coerce
{
"mappings": {
"properties": {
"number_one": {
"type": "integer"
},
"number_two": {
"type": "integer",
"coerce": false
}
}
}
}
PUT coerce/_doc/1
{
"number_one": "10"
}
#//拒绝,因为设置了false
PUT coerce/_doc/2
{
"number_two": "10"
}
DELETE coerce
PUT coerce
{
"settings": {
"index.mapping.coerce": false
},
"mappings": {
"properties": {
"number_one": {
"type": "integer",
"coerce": true
},
"number_two": {
"type": "integer"
}
}
}
}
PUT coerce/_doc/1
{
"number_one": "10"
}
#拒绝,因为设置了false
PUT coerce/_doc/2
{
"number_two": "10"
}
PUT /product/_mapping
{
"properties": {
"date": {
"type": "text"
}
}
}
#7- 7
PUT dynamic
{
"mappings": {
"dynamic": false,
"properties": {
"user": {
"properties": {
"date": {
"type": "text"
},
"desc": {
"type": "text",
"analyzer": "english"
},
"name": {
"type": "text",
"index": "false"
},
"price": {
"type": "long"
}
}
}
}
}
}
PUT /product/_mapping
{
"properties": {
"date": {
"type": "text"
}
}
}
#7-11
GET /product/_mapping
#给city创建一个keyword
PUT fields_test
{
"mappings": {
"properties": {
"city": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
}
}
}
}
}
}
PUT fields_test/_doc/1
{
"city": "New York"
}
PUT fields_test/_doc/2
{
"city": "York"
}
GET fields_test/_mapping
GET fields_test/_search
{
"query": {
"match": {
"city": "york"
}
},
"sort": {
"city.raw": "asc"
},
"aggs": {
"cities": {
"terms": {
"field": "city.raw"
}
}
}
}
#忽略类型错误-常用于数据同步
PUT ignore_malformed
{
"mappings": {
"properties": {
"number_one": {
"type": "integer",
"ignore_malformed": true
},
"number_two": {
"type": "integer"
}
}
}}
PUT ignore_malformed/_doc/1
{
"text": "Some text value",
"number_one": "foo"
}
#//虽然有异常 但是不抛出
PUT ignore_malformed/_doc/2
{
"text": "Some text value",
"number_two": "foo"
}
GET my_index/_search
#//数据格式不对
#fielddata
#每个tag产品的数量 "size":0, 不显示原始结果
GET /product/_search
{
"aggs": {
"tag_agg_group": {
"terms": {
"field": "tags"
}
}
},
"size":0
}
GET /product/_mapping
#将文本field的fielddata属性设置为true
PUT /product/_mapping
{
"properties": {
"tags": {
"type": "text",
"fielddata": true
}
}
}