mapping类似数据库中的schema的定义,
mapping 定义索引中的字段名称,字段数据类型,索引的其他配置
mapping会把json文档映射成lucene所需的扁平格式
GET index/_mapping
1.字段的数据类型
简单类型:text/keyword,date,integer/floating,boolean,ipv4,ipv6
复杂类型:对象和嵌套对象,object,
特殊类型:geo_point,geo_shape,percolator
2.dynamic mapping
dynamic mapping 机制使得无需手动定义mapping,ES会自动根据文档信息,推算字段的类型。
但是有时候推算不对,比如地理位置;
dynamic 可以设置为:true,false,strict
# 设置索引的dynamic mapping
PUT index {
"mappings":{
"_doc":{
"dynamic":"false"
}
}
}
es 不允许对索引更改字段类型。
3.显示定义mapping
PUT movies
{
"mappings":{
//define your mapping here
}
}
为了减少手写的工作量和出错率,可以创建一个临时的index,写入样本数据,读取它的mapping定义,再在这个定义上修改自己想要的。
# 控制是否索引该字段
PUT users
{
"mappings":{
//define your mapping here
"properties":{
"mobile":{
"type":"text",
"index":false
}
}
}
}
# 四种 不同级别的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 offects
text类型默认postions,其他默认docs
PUT users
{
"mappings":{
//define your mapping here
"properties":{
"mobile":{
"type":"text",
"index_options":"offsets"
}
}
}
}
# null_value,是的null字段可以搜索
PUT users
{
"mappings":{
//define your mapping here
"properties":{
"mobile":{
"type":"text",
"null_value":"NULL"
}
}
}
}
# copy_to设置
PUT users
{
"mappings":{
//define your mapping here
"properties":{
"firstname":{
"type":"text",
"copy_to":"fullName"
}
}
}
}
# 数组类型,ES没有数组类型,但是任何字段,都可以包含多个相同类型的数值
PUT user/_doc/2
{
"name":"aganliang",
"interests":["reading","music"]
}