1. 创建mapping
我们可以在创建index时设置mapping。每个mapping含有Meta-field(元数据类型)以及properties(字段的集合)。
PUT index_name
{
"mappings" : {
"_doc" : {
/*
这里面可以设置元数据类型,例如_source、_id
*/
"properties" : {
"name" : {
"type" : "text",
"index" : true //字段参数。表示该字段是否被索引,默认为true。设为false则 not queryable
},
"date" : {
"type" : "date",
"index" : true,
"format": "strict_date_optional_time||epoch_millis" //日期的格式
}
}
}
}
}
其中,ES字段的核心数据类型如下,具体细节可以看(https://blog.youkuaiyun.com/interstellar_/article/details/81356734)。注意在实际应用中应使用尽量小的数据类型
- string:字符串。
text
与keyword
- number: 数字。
long
,integer
,short
,byte
,double
,float
,half_float
,scaled_float
- date:日期。
date
- boolean:布尔型。
boolean
- binary:二进制。
binary
- range: 范围值。
integer_range
,float_range
,long_range
,double_range
,date_range
除此之外,还有复杂的数组,对象,内嵌对象类型,特有类型。详情可参阅官方文档(https://www.elastic.co/guide/en/elasticsearch/reference/6.3/mapping-types.html)。
mapping中的元数据类型可看()
mapping中的相关参数可看(https://blog.youkuaiyun.com/interstellar_/article/details/81359301)