Elasticsearch 之 Mapping
什么是mapping
- Mapping 用来定义存储结构及索引过程,类似关系型数据中的schema 定义
- 定义索引中的字段名称
- 定义字段的数据类型
- 定义字段、倒排索引的相关配置
Mapping-Type
- mapping对应索引中的一个Type
- 一个type有一个mapping定义
- ES 7.0 开始,不需要在Mapping 定义中指定type信息,type默认为_doc
字段的数据类型
创建索引
# 执行以下语句创建一个索引
PUT t_student
# 执行结果
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "t_student"
}
此时索引已经创建成功了,我们看一下索引的mapping结构
# 执行以下语句查询mapping结构
GET t_student/_mapping
# 执行结果
{
"t_student" : {
"mappings" : {
}
}
}
我们发现这时的mapping是空的,这时我们往里写下数据:
# 写数据
PUT t_student/_doc/1
{
"name":"zhangsan"
}
# 执行结果
{
"_index" : "t_student",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
# 查询数据
GET t_student/_search
# 查询结果
{
"took" : 2,
"timed_out" : false