Field datatypes 字段的数据类型
- 核心数据类型
-
- 字符串类型: text(分词),keyword(不分词)一个用于全文检索,一个用于聚合和排序。
- 数值型: long,integer,short,byte,double,float,half_float,scaled_float
- 日期:date
- 布尔:boolean
- 二进制:binary
- 范围类型:integer_range,float_range,long_range,double_range,date_range
- 复杂数据类型
-
- 数组 array
- 对象 object
- 嵌套类型 nested object
- 地理类型
-
- geo_point
- geo_shape
- 专用类型
-
- 记录ip地址 ip
- 实现自动补全 completion
- 记录分词数 token_count
- multi-fields多字段
# --允许对一个字段采用不同配置。
{ "mappings": { "doc":{ "properties": { "name":{ "type": "text", "fields": { "pinyin":{ "type": "text", "analyzer": "pinyin" }}}}}}} GET my_index/_search { "query": { "match": { "name.pinyin": "hanhan" }}}
Meta-Fields 元数据类型
- _index,_uid,_type,_id
- 文档源:_source, _size
- 索引:_all ,_field_names
- 路由:_routing
- 其他:_meta
Dynamic mapping
dynamic field mapping 动态字段映射
mapping中字段类型一旦设定后 禁止直接修改。因为lucene实现的倒排索引生成后不允许修改、允许字段新增
dynamic 参数控制字段的新增
-true 允许自动新增字段(默认的)
-false 不允许自动新增字段,但是文档可以正常写入,但无法对字段进行查询等操作。
-strict 文档不能写入
- date detection 日期检测 默认为true
#当创建一个日期格式的文档时dynamic_date_formats为 # ["strict_date_optional_time","yyyy/MM/dd HH:mm:ss Z||yyyy/MM/dd Z"] PUT t-index/doc/1 { "create_date":"2018/07/03" } --------------》 { "t-index": { "mappings": { "doc": { "properties": { "create_date": { "type": "date", #自动识别日期为date类型 "format": "yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis" }}}}}} #禁用日期检测 设置date_detection为false的情况 PUT t-index { "mappings": { "doc":{ "date_detection": false } } } PUT t-index/doc/2 { "new_date":"2018/05/20" } GET t-index/_mapping --------------------------》 { "t-index": { "mappings": { "doc": { "date_detection": false, "properties": { "new_date": { "type": "text", #日期为text类型 "fields": { "keyword": { "type": "keyword", "ignore_above": 256 }}}}}}}}}
- dynamic_date_formats 自定义日期格式
PUT t2-index { "mappings": { "doc": { "dynamic_date_formats": ["yyyy-MM-dd"] } } } PUT t2-index/doc/1 { "create_time":"2018-07-03" } GET t2-index/_mapping --------------------》 { "t2-index": { "mappings": { "doc": { "dynamic_date_formats": [ "yyyy-MM-dd" ], "properties": { "create_time": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 }}}}}}}}
- numeric_detection 数字检测 默认是关闭的
PUT t-index { "mappings": { "doc": { "numeric_detection": true #打开数字检测 } } } PUT t-index/doc/1 { "my_float":"1.2", "my_integer":"1" } GET t-index/_mapping --------------------》 { "t-index": { "mappings": { "doc": { "numeric_detection": true, "properties": { "my_float": { "type": "float" #自动检测1.2为float }, "my_integer": { "type": "long" #自动检测1为long }}}}}}
dyamic-templates 动态模板
格式为:
"dynamic_templates": [ { "my_template_name": { ... match conditions ... "mapping": { ... } } }, ... ] #举个栗子 { "mappings": { "doc":{ "date_detection": false, #关闭时间检测 "dynamic_templates":[ #关键字 { "my_template":{ #模板名称 "match": "*", #匹配所有字段 "match_mapping_type":"string", #匹配条件 "mapping":{ # mapping "type":"text", #设置字段类型为text "fields":{ #对一个字段设置多个索引模式,一个分词一个不分词 "keyword":{ "type":"keyword", "ignore_above":265 }}}}}]}}}
匹配条件有:match_mapping_type, match, match_pattern, unmatch, path_match, path_unmatch.
- match_mapping_type
自动检测类型有 :boolean, date, double, long, object, string
-
PUT my_index { "mappings": { "doc":{ "dynamic_templates":[ { "my_string":{ "match_mapping_type":"string", "mapping":{ "type":"text", "fields":{ "keyword":{ "type":"keyword", "ignore_above":265 } } } } } ] } } } PUT my_index/doc/1 { "name":"Jahn smis" } GET my_index/_mapping -------------------> { "my_index": { "mappings": { "doc": { "dynamic_templates": [ { "my_string": { "match_mapping_type": "string", "mapping": { "fields": { "keyword": { "ignore_above": 265, "type": "keyword" } }, "type": "text" } } } ], "properties": { "name": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 265 } } } } } } } }
- match,unmatch 匹配字段名称
PUT my_index { "mappings": { "_doc": { "dynamic_templates": [ { "longs_as_strings": { "match_mapping_type": "string", "match": "long_*", "unmatch": "*_text", "mapping": { "type": "long" } } } ] } } }
- path_match ,path_umatch
PUT my_index { "mappings": { "_doc": { "dynamic_templates": [ { "full_name": { "path_match": "name.*", "path_unmatch": "*.middle", "mapping": { "type": "text", "copy_to": "full_name" } } } ] } } }
- {name} {dynamic_type}
"norms":false norms是指时间评分因子,如果不关心评分可以禁用