ES
操作索引库
1、Mapping
Mapping
是 Elasticsearch 索引的“表结构”,用于定义字段的数据类型、分词方式、存储方式等,相当于数据库的表结构定义(DDL)。
常见的 mapping 属性有:
type
:字段数据类型,常见的简单类型有:- 字符串:
text
(可分词的文本)、keyword
(精确值,不能分词,例如:国家、品牌) - 数值:
long
、integer
、byte
、double
、float
- 布尔:
boolean
- 日期:
date
- 对象:
object
- 字符串:
index
:是否创建索引,默认为 trueanalyzer
:使用哪种分词器properties
:该字段的子字段
2、操作
2.1.创建索引库
es 中通过 Restful 请求操作索引库、文档,请求内容用 DSL 语句来表示。创建索引库和 mapping 的 DSL 语法如下:
json
PUT /indeName
{
"mappings": {
"properties": {
"fieldName01":{
"type": "text",
"analyzer": "ik_smart"
},
"fieldName02":{
"type": "keyword",
"index": "false"
},
"fieldName03":{
"properties": {
"子字段": {
"type": "keyword"
}
}
},
// ...略
}
}
}
示例:

2.2.查询索引库
json
GET /indexName
示例:
json
GET /heima
结果:
json
{
"heima": {
"aliases": {},
"mappings": {// 字段的映射关系
"properties": {
"email": {
"index": false,
"type": "keyword"
},
"info": {
"analyzer": "ik_smart",
"type": "text"
},
"name": {
"properties": {
"firstName": {
"type": "keyword"
},
"lastName": {
"type": "keyword"
}
}
}
}
},
"settings": {// 其它设置
"index": {
"creation_date": "1742264558371",
"number_of_replicas": "1",
"number_of_shards": "1",
"provided_name": "heima",
"routing": {
"allocation": {
"include": {
"_tier_preference": "data_content"
}
}
},
"uuid": "5pR2rWmwQcyFkArWy2Empg",
"version": {
"created": "8070099"
}
}
}
}
}
2.3.修改索引库
不能修改 index 中已有的字段,只能添加新的字段到索引中:
json
// 修改索引库,添加新字段
PUT /heima/_mapping
{
"properties": {
"age": {
"type": "integer"
}
}
}