mapping
映射属性mapping
映射属性官方网址:https://www.elastic.co/guide/en/elasticsearch/reference/7.12/dynamic-mapping.html
mapping
是对索引库中文档的约束,常见的mapping
属性包括
type
: 字段数据类型,常见的简单类型有:
text
(可分词的文本);keyword
(精确值,例如: 品牌、国家、ip
地址)。如下:email
字段就是不可拆分字段。info
属于可拆分字段。long、integer、short、byte、double、float、
boolean
date
object
index
:是否创建索引,默认为true
analyzer
:使用哪种分词器properties
:该字段的子字段{
"age": 21,
"weight": 52.1,
"info":"我们在学ES",
"isMarried": false,
"email":"[email protected]",
"score": [99.1,99.5,98.9],
"name":{
"firstName":"云",
"LastName":"赵"
}
}
ES
中通过Restful
请求操作索引库、文档。请求内容用DSL
语句来表示。创建索引库和mapping
的DSL
语法如下:
PUT /索引库名称
"mappings": {
"properties": {
"字段名":{
"type":"text",
"analyzer":"ik_smart"
}
"字段名2":{
"type":"keyword",
"index":"false"
}
"字段名3":{
"properties": {
"子字段": {"type":"keyword"}
}
}
},
// 略
}
索引库的CRUD
# 创建索引库
PUT /hhyy
{
"mappings": {
"properties": {
"info":{
"type":"text",
"analyzer": "ik_smart"
},
"email":{
"type": "keyword",
"index": false
},
"name":{
"type": "object",
"properties": {
"firstName":{
"type":"keyword"
},
"lastName":{
"type":"keyword"
}
}
}
}
}
}
# 创建
PUT /hhyy
# 删除
DELETE /hhyy
# 查询
GET /hhyy
# 修改[添加新字段],只能添加,不能更新
# 更新索引过于消耗资源
PUT /hhyy/_mapping
{
"properties":{
"age":{
"type":"integer"
}
}
}
新增文档的DSL
语法如下:
POST /索引库名/_doc/文档id
{
"字段1":"值1",
"字段2":"值2",
"字段3":{
"子属性1":"值3",
"子属性2":"值4"
},
}
示例:
# 插入文档
POST /hhyy/_doc/1
{
"info":"我们学习ES",
"email":"[email protected]",
"name":{
"firstName":"Jack",
"lastName":"hh"
}
}
# 获取
GET /hhyy/_doc/1
# 删除
DELETE /hhyy/_doc/1
修改
# 修改文档
# 全量修改,会删除旧文档,添加新文档
# 若id存在就修改,不存在就新增
PUT /hhyy/_doc/2
{
"info":"我们学习ES1111",
"email":"[email protected]",
"name":{
"firstName":"Jack",
"lastName":"hh"
}
}
GET /hhyy/_doc/2
# 增量修改,
POST /hhyy/_update/2
{
"doc":{
"info":"我们都要努力学习ES"
}
}
GET /hhyy/_doc/2
上一篇:一、初识 Elasticsearch:概念,安装,设置分词器
下一篇:三、RestClient(writing)