一 自定义mapping
- Mapping类似于数据库中表定义,定义Index字段名,字段类型及倒排索引相关配置;
- Mapping中字段类型一旦设定后,禁止修改,若修改,重新建立新索引,进行reindex操作;
- 通过dynamic参数控制字段新增,参数有true/false/strict;
##dynamic控制字段新增
##true:默认允许自动新增字段; false:不允许自动新增字段,文档可以正常写入,但是无法对该字段进行查询;strict:文档不能写入;
PUT my_index
{
"mappings": {
"doc": {
"dynamic":false,
"properties": {
"title": {
"type": "text"
},
"name": {
"type": "keyword"
},
"age": {
"type": "integer"
}
}
}
}
}
GET my_index/_mapping
PUT my_index/doc/1
{
"title":"hello,world",
"desc":"nothing here" ##dynamic为false,可以添加文档,但是不能查询该字段;
}
二 常用参数设置
- copy_to
将该字段的值复制到目标字段,不会出现在_source中,只用来搜索;
PUT my_index
{
"mappings": {
"doc": {
"properties": {
"first_name": {
"type": "text",
"copy_to": "full_name"
},
"last_name": {
"type": "text",
"copy_to": "full_name"
},
"full_name": {
"type": "text"
}
}
}
}
}
PUT my_index/doc/1
{
"first_name": "John",
"last_name": "Smith"
}
GET my_index/_search
{
"query": {
"match": {
"full_name": { ##full_name中同时包含John和Smith
"query": "John Smith",
"operator": "and"
}
}
}
}
- index
控制当前字段是否时索引,默认为true,即记录索引,false不记录,即不可搜索;
PUT my_index
{
"mappings": {
"doc": {
"properties": {
"cookie": {
"type": "text",
"index": false
}
}
}
}
}
PUT my_index/doc/1
{
"cookie":"name=alfred"
}
GET my_index/_search
{
"query":{
"match": {
"cookie": "name" ##Cannot search on field [cookie] since it is not indexed
}
}
}
- index_options
用于控制倒排索引记录的内容;
docs: 记录doc id
freqs: 记录doc id 和 term frequencies
positions: 记录doc id/term frequencies/term position
offsets: 记录doc id/term frequencies/term position/character offsets
- null_value
当字段遇到null值时的处理策略,默认为null,即空值,es会自动忽略,可以通过设定该字段的默认值;
PUT my_index
{
"mappings": {
"my_type": {
"properties": {
"status_code": {
"type": "keyword",
"null_value": "NULL"
}
}
}
}
}
PUT my_index/my_type/1
{
"status_code": null
}
PUT my_index/my_type/2
{
"status_code": []
}
GET my_index/_search
{
"query": {
"term": {
"status_code": "NULL"
}
}
}
三 数据类型
字符串型: 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
记录字符串hash值: murmur3
percolator
join
允许对用一个字段采用不同的配置,比如分词,常见例子如对任命实现拼音搜索,仅需再人名中增加pinyin子字段即可;
四 Dynamic Mapping
dynamic_date_formats:自定义日期类型
PUT my_index
{
"mappings":{
"my_type":{
"dynamic_date_formats":["MM/dd/yyyy"]
}
}
}
不定义mapping,添加文档后的日期格式识别为text类型
numeric_detection: 开启字符串中数字自动识别
##将字符串中的数字识别为数值类型
PUT my_index
{
"mappings":{
"my_type":{
"numeric_dectection":true
}
}
}
PUT my_index/my_type/1
{
"my_float":"1.0",
"my_integer":"1"
}
允许根据es自动识别的数据类型/字段名等来动态设定字段类型;
##match_mapping_type匹配自动识别字段类型,match,unmatch匹配字段名,path_match,path_unmath匹配路径
PUT my_product_index
{
"mappings": {
"doc": {
"dynamic_templates": [
{
"strings": {
"match_mapping_type": "string",
"mapping": {
"type": "keyword"
}
}
}
]
}
五 Index Template
- 用于在新建索引时自动应用预先设定的配置,简化所以创建,若有多个模板,根据order大小,大的会覆盖掉小的;
PUT _template/test_template
{
"index_patterns": ["te*", "bar*"],
"order":0,
"settings": {
"number_of_shards": 1
},
"mappings": {
"doc": {
"_source": {
"enabled": false
},
"properties": {
"name": {
"type": "keyword"
}
}
}
}
}
PUT _template/test_template2
{
"index_patterns": ["test*"],
"order":1,
"settings": {
"number_of_shards": 1
},
"mappings": {
"doc": {
"_source": {
"enabled": true
}
}
}
}
PUT test_index
GET test_index/ ##order大的会覆盖小的