作用

定义数据库中的表的结构的定义,通过mapping来控制索引存储数据的设置
a. 定义Index下的字段名(Field Name)
b. 定义字段的类型,比如数值型、字符串型、布尔型等
c. 定义倒排索引相关的配置,比如documentId、记录position、打分等

获取索引mapping

不进行配置时,自动创建的mapping
注入测试数据

POST test/doc/1
{
  "name":"zyd",
  "age":16
}

请求:

GET test/_mapping

返回

{
  "test": {  #索引名称
    "mappings": {  #mapping设置
      "doc": {  	#type名称
        "properties": {  	#字段属性
          "age": {
            "type": "long"  #字段类型,字符串默认类型
          },
          "name": {
            "type": "text",
            "fields": {  #子字段属性设置
              "keyword": {  #分词类型(不分词)
                "type": "keyword",  
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}

自定义Mapping

request

PUT my_index								
{
  "mappings":{
    "doc":{									#类型名称
      "dynamic":false, 					
      "properties":{		
        "title":{
          "type":"text"				#字段类型
        },
        "name":{
          "type":"keyword"
        },
        "age":{
          "type":"integer"
        }
      }
    }
  }
}

response

{
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "my_index"
}

Dynamic Mapping

es依靠json文档字段类型来实现自动识别字段类型,支持的类型

JSON类型 es类型
null 忽略
boolean boolean
浮点类型 float
整数 long
object object
array 由第一个非null值的类型决定
string 匹配为日期则设为data类型(默认开启) 匹配为数字的话设为float或long类型(默认关闭) 设为text类型,并附带keyword的子字段

注意:
mapping中的字段类型一旦设定后,禁止修改
原因:Lucene实现的倒排索引生成后不允许修改(提高效率)
如果要修改字段的类型,需要从新建立索引,然后做reindex操作
dynamic设置
a. true:允许自动新增字段(默认的配置)
b. False:不允许自动新增字段,但是文档可以正常写入,无法对字段进行查询操作
c. strict:文档不能写入(如果写入会报错)

PUT my_index1
{
"mappings":{
"doc":{
"dynamic":false,
"properties":{
"user":{
"properties":{
"name":{
"type":"text"
},"social_networks":{
"dynamic":true,
"properties":{}
}
}
}
}
}
}
}

copy_to

将该字段的值复制到目标字段,实现_all的作用,不会出现在_source中,只用来搜索

PUT my_index3
{

"mappings": {

"my_type": {

"properties": {

"first_name": {

"type":"keyword",

"copy_to":"full_name"

},

"last_name": {

"type":"keyword",

"copy_to":"full_name"

},

"full_name": {

"type":"text",

"fielddata":true

}

}

}

}

}

插入数据

PUT my_index3/my_type/1
{

"first_name": "John",

"last_name": "Smith"

}

获取

GET my_index3/_search
{

"query": {

"match": {

"full_name": {

"query":"John Smith",

"operator":"and"

}

}

}

}

结果

{
  "took": 215,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.5753642,
    "hits": [
      {
        "_index": "my_index3",
        "_type": "my_type",
        "_id": "1",
        "_score": 0.5753642,
        "_source": {
          "first_name": "John",
          "last_name": "Smith"
        }
      }
    ]
  }
}

Index属性

Index属性,控制当前字段是否索引,默认为true,即记录索引,false不记录,即不可以搜索,比如:手机号、身份证号等敏感信息,不希望被检索
1、创建mapping

PUT my_index
{
  "mappings": {
    "doc":{
      "properties": {
        "cookie":{
          "type":"text",
          "index": false
        }
      }
    }
  }
}

1、创建文档

PUT my_index/doc/1
{
  "cookie":"123",
  "name":"home"
}

查询

GET my_index/_search
{
 "query": {
   "match": {
     "cookie":"123"
   }
 } 
}
#报错
GET my_index/_search
{
 "query": {
   "match": {
     "name":"home"
   }
 } 
}

Index_options用于控制倒排索引记录的内容,有如下4中配置
docs:只记录docid
freqs:记录docid和term frequencies(词频)
position:记录docid、term frequencies、term position
Offsets:记录docid、term frequencies、term position、character offsets

text类型默认配置为position,其默认认为docs
记录的内容越多,占用的空间越大
数据类型

核心数据类型

字符串型: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

多字段特性multi-fields

允许对同一个字段采用不同的配置,比如分词,例如对人名实现拼音搜索,只需要在人名中新增一个子字段为pinyin即可
1、创建mapping

PUT my_index1
{
  "mappings": {
    "doc":{
      "properties":{
        "username":{
          "type": "text", 
          "fields": {
            "pinyin":{
              "type": "text"
            }
          }
        }
      }
    }
  }
}

创建文档

PUT my_index1/doc/1
{
  "username":"haha heihei"
}

查询

GET my_index1/_search
{
  "query": {
    "match": {
      "username.pinyin": "haha"
    }
  }
}

Dynamic Mapping

es可以自动识别文档字段类型,从而降低用户使用成本

PUT /test_index/doc/1
{
  "username":"alfred",
  "age":1
}

{
  "test_index": {
    "mappings": {
      "doc": {
        "properties": {
          "age": {
            "type": "long"
          },
          "username": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}

age自动识别为long类型,username识别为text类型

PUT test_index/doc/1
{
  "username":"samualz",
  "age":14,
  "birth":"1991-12-15",
  "year":18,
  "tags":["boy","fashion"],
  "money":"100.1"
}

{
  "test_index": {
    "mappings": {
      "doc": {
        "properties": {
          "age": {
            "type": "long"
          },
          "birth": {
            "type": "date"
          },
          "money": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "tags": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "username": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "year": {
            "type": "long"
          }
        }
      }
    }
  }
}

日期的自动识别可以自行配置日期格式,以满足各种需求
1、自定义日期识别格式

PUT my_index
{
  "mappings":{
    "doc":{
      "dynamic_date_formats": ["yyyy-MM-dd","yyyy/MM/dd"]
    }
  }
}

关闭日期自动识别

PUT my_index
{
  "mappings": {
    "doc": {
      "date_detection": false
    }
  }
}

字符串是数字时,默认不会自动识别为整形,因为字符串中出现数字时完全合理的
Numeric_datection可以开启字符串中数字的自动识别

PUT my_index
{
  "mappings":{
    "doc":{
      "numeric_datection": true
    }
  }
}