创建映射字段
PUT /索引库名/_mapping
{
"properties": {
"字段名": {
"type": "类型",
"index": true,
"store": true,
"analyzer": "分词器"
}
}
}
https://www.elastic.co/guide/en/elasticsearch/reference/7.3/mapping-params.html
字段名:任意填写,下面指定许多属性,例如:
- type:类型,可以是text、long、short、date、integer、object等
- index:是否索引,默认为true
- store:是否存储,默认为false
analyzer:指定分词器
PUT /lagou-company-index/_mapping/
{
"properties": {
"name": {
"type": "text",
"analyzer": "ik_max_word"
},
"job": {
"type": "text",
"analyzer": "ik_max_word"
},
"logo": {
"type": "keyword",
"index": "false"
},
"payment": {
"type": "float"
}
}
}
给lagou-company-index这个索引库设置了4个字段:
name:企业名称
- job: 需求岗位
- logo:logo图片地址
- payment:薪资
查看索引映射
GET /索引名称/_mapping
查看所有索引映射关系
GET _all/_mapping
修改索引映射关系
PUT /索引库名/_mapping
{
"properties": {
"字段名": {
"type": "类型",
"index": true,
"store": true,
"analyzer": "分词器"
}
}
}
:::info 注意:修改映射只能增加字段, 做其它更改只能删除索引 重新建立映射 :::
一次性创建索引和映射
put /索引库名称
{
"settings": {
"索引库属性名": "索引库属性值"
},
"mappings": {
"properties": {
"字段名": {
"映射属性名": "映射属性值"
}
}
}
}
PUT /lagou-employee-index
{
"settings": {},
"mappings": {
"properties": {
"name": {
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
老版本type
# 获取某个索引信息
GET /library/_mapping
# 获取某个索引下某个type的映射信息
GET /library/_mapping/books
# 获取这个集群内所有的映射信息
GET /_all/_mapping
# 获取这个集群内某两个或多个type的映射信息
GET /_all/_mapping/books,bank_account
Hello hello are you you can call me if you want to come over for a little time and I have a lot of of them them and the other two are is
# 更新修改Mapping映射
# 很遗憾,mapping一旦建立,就不能修改现有的字段映射
# 如果要推到现有的映射,你得重新建立一个索引,然后重新定义映射
# 然后把之前索引里的数据导入到新建立的索引里
# ------具体方法-------
# 1. 给现有的索引定义一个别名,并且把现有的索引指向这个别名,运行步骤2
# 2. 运行: PUT /现有索引/_alias/别名A
# 3. 新创建一个索引,定义好最新的映射
# 4. 将别名指向新的索引,并且取消之前索引的指向,运行步骤5
# 5. 运行
POST /_aliases
{
"actions": [
{"remove": {"index":"现有索引名", "alias":"别名A"}},
{"add": {"index":"新建索引名", "alias":"别名A"}}
]
}
# 注: 通过这几个步骤就实现了索引的平滑过渡,并且是零停机的
# 删除映射
DELETE /library/books
DELETE /library/books/_mapping
DELETE /library/_mapping/books