ELK五:映射
一、映射定义
映射,用来定义一个文档及其包含的字段如何存储和索引的过程。我们使用映射来定义:
- 哪些字符串应该被视为全文字段
- 哪些字段包含数字、日期或地理位置
- 定义日期的格式
- 自定义的规则,用来控制动态添加字段的映射
映射类型:
- 元字段-----元字段用于自定义如何处理文档关联的元数据,如包括 文档的_index、_type、_id、_source字段
- 字段或属性-----映射类型包含与文档相关的字段或者属性的列表
字段的数据类型:
- 简单类型----如文本text、关键字keyword、日期date、整型long、双精度double、布尔boolean或ip
- 可以是支持json的层次结构性质的类型,如对象或嵌套
- 特殊类型,如geo_point、geo_shape或completion
映射约束:
在索引中定义大多的字段,有可能导致映射爆炸,导致内存不足以及难以恢复的情况。
为此,我们可以手动或动态的创建字段映射的数量:
- index.mapping.total_fileds.limit:索引中的最大字段数。字段和对象映射以及字段别名都计入此限制,默认为1000
- index.mapping.depth.limit:字段的最大深度,以内部对象的数量来衡量。默认为20
- index.mapping.nested_fields.limit:索引中嵌套字段的最大数量,默认为50。索引1个包含100个嵌套字段的文档,实际上索引101个文档,因为每个嵌套文档都被索引为单独的隐藏文档。
二、映射示例
设置映射:
PUT s2 { "mappings": { "doc": { "properties": { "name": { "type": "text" }, "age": { "type": "long" }, "desc": { "type": "text" } } } } }
查询映射的设置:
GET s18/_mapping
{ "s18" : { "mappings" : { "properties" : { "age" : { "type" : "long" }, "b" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "name" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "sex" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "tags" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } } } } } }
插入数据
put s2/doc/1 { "name": "zining", "age": 32, "desc": "低调" }
注意:
dynamic默认为true。可以新增,可以作为主查询字段。
为flase时。可以新增,但不可以作为主查询字段,但可以作为辅助查询字段。
为strict时。不可以新增。
三、映射其它设置
ignore_above:超过ignore_above的字段,不会被索引,当然也会查询不到。
对于字符串数组,ignore_above将分别应用于每个数组元素,并且超过字段串元素ignore_above将不会将索引或存储。
index:index为true,此字段将创建索引,可以作为主查询条件。当为false,此字段将不会被创建索引,不可以作为主查询条件。
put s8 { "mappings": { "doc": { "properties": { "t1": { "type": "text", "index": true } "t2": { "type": "text", "index": false } } } } }
PUT s8/doc/1 { "t1": "abcddef", "t2": "asdfasdf" }
GET s8/doc/_search { "query": { "t1": "abcddef" } }
copy_to:
PUT s9 { "mappings": { "doc": { "properties": { "t1": { "type": "text", "copy_to": "full_name" }, "t2": { "type": "text", "copy_to": "full_name" }, "full_name": { "type": "text" } } } } }
PUT s9/doc/1 { "t1": "x" "t2": "y" } GET s9/doc/1 { "query": { "match": { "full_name": "x" } } }
PUT s9 { "mappings": { "doc": { "properties": { "t1": { "type": "text", "copy_to": ["f1", "f2"] }, "t2": { "type": "text", "copy_to": ["f1", "f2"] }, "f1": { "type": "text" }, "f2": { "type": "text" } } } } }
嵌套类型:
PUT w1 { "mappings": { "doc": { "properties": { "name": { "type": "text" }, "age": { "type": "long" }, "info": { "properties": { "addr": { "type": "text" }, "tel": { "type": "long" } } } } } } }
PUT w1/doc/1 { "name": "jerry", "age": 78, "info": { "addr": "bj", "tel": "2342" } }
GET w1/doc/_search { "query": { "match": { "info.tel": "2342" } } }
设置mapping的同时,设置settings
settings设置主分片、复制分片示例:
PUT w2 { "mappings": { "doc": { "properties": { "name": { "type": "text", "index": true }, "age": { "type": "long", "ignore_above": 128 }, "desc": { "type": "keyword", "ignore_above": 128, "copy_to": ["t1", "t2"] }, "tags": { "type": "keyword", "index": false, "copy_to": ["t1", "t2"] }, "info": { "properties": { "addr": { "type": "text" }, "tel": { "type": "long" } } }, "t1": { "type": "text" }, "t2": { "type": "text" } } } }, "settings": { "number_of_shards": 3, "number_of_replicas": 3 } }
posted on 2018-03-30 19:07 myworldworld 阅读(253) 评论(0) 收藏 举报