使用的ES版本为7.11.2,相关操作都是基于这个7.x版本下的,其他版本可能部分语法不兼容
1.创建mapping
1.1.创建新的mapping
PUT my_index
{
"mappings": {
"dynamic": "false",
"properties": {
"acknowledged": {
"type": "integer"
},
"acknowledged_time": {
"type": "date"
},
"group": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"arrival_time": {
"type": "date"
},
"summary": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"remark": {
"type": "text",
"analyzer": "ik_max_word"
},
"severity": {
"type": "integer"
},
"status": {
"type": "integer"
},
"total": {
"type": "integer"
}
}
}
}
1.2.字段说明
是否确认:acknowledged,0未确认,1已确认
确认时间:acknowledged_time
来源:group
到达时间:arrival_time
描述:summary
备注:remark
等级:severity
状态:status,1打开,2关闭
次数:total
1.3.查看mapping
GET my_index/_mapping
1.4.在已有的索引中,增加新的字段
例如:增加字段:来源source
PUT my_index/_mapping
{
"properties": {
"source": {
"type": "keyword"
}
}
}
2.插入数据
2.1. POST插入数据
post插入数据时,_id是自动生成的
POST my_index/_doc
{
"acknowledged":0,
"acknowledged_time":"2022-06-30T12:34:43.002Z",
"arrival_time":"2022-06-30T12:34:00.000Z",
"group":"IC卡系统",
"remark":"重启中可忽略",
"severity":1,
"status":1,
"summary":"主机手动重启",
"total":1
}
2.2. PUT插入数据
PUT插入数据时,指定_id
PUT my_index/_doc/8j55f4e32b843fk12
{
"acknowledged":0,
"acknowledged_time":"2022-07-11T01:07:03.002Z",
"arrival_time":"2022-07-11T01:34:00.000Z",
"group":"核心业务系统",
"remark":"",
"severity":3,
"status":1,
"summary":"deployment进程数量小于1",
"total":1
}
查看该数据是否插入
GET my_index/_search
2.3.批量插入
POST _bulk
{
"index":{
"_index":"my_index"}}
{
"severity" : 1,"summary" : "1分钟内交易响应率小于50.0%","total" : 6,"arrival_time" : "2022-07-12T14:01:54.000Z","acknowledged" : 0,"acknowledged_time" : "2022-07-12T14:23:53.002Z","remark" : "单笔交易量异常","source" : "河南银行","group" : "电子支付系统","status" : 1}
{
"index":{
"_index":"my_index","_id":"eu3r45gtr34ie2386ekwe55yt23" } }
{
"severity" : 3,"summary" : "1分钟内交易响应率小于50.0%","total" : 6,"arrival_time" : "2022-07-12T14:31:54.000Z","acknowledged" : 0,"acknowledged_time" : "2022-07-12T14:33:53.002Z","remark" : "单笔交易量异常","source" : "河南银行","group" : "电子支付系统","status" : 1}
2.4.对索引中值为空的字段赋值
例:source字段为空的赋值
inline被弃用,可用source代替
POST my_index/_update_by_query
{
"script": {
"source": "if (ctx._source.source == null) {ctx._source.source= '河南银行'}",
"lang": "painless"
}
}
3.删除索引
3.1.删除单个索引
delete /索引名
3.2.删除全部索引
delete /_all
或
DELETE /*
4.删除数据
4.1. 删除索引下的某条数据
例如:删除核心业务系统的数据
POST my_index/_delete_by_query
{
"query":{
"term": {
"group": {
"value": "核心业务系统"
}
}
}
}
4.2.删除索引中的某个字段
ES中无法直接增删索引中的字段,只能能够覆盖,即重建新的索引
例:删除索引中的source字段
- 先将source字段中的数据删除,否则后面reindex时,索引中会自动增加source字段和类型
POST my_index/_update_by_query
{
"script": {
"source": "ctx._source.remove(\"source\")",
"lang": "painless"
}
}
- 创建新的索引,该索引中不包含source字段
- 进行数据迁移
POST _reindex { "source": { "index": "my_index" }, "dest": { "index": "my_index_bak" } }
- 删除原来的索引
delete /my_index
5.修改数据
5.1.修改某个字段的数据
例如:将id为4XEu8IEBgjRHnDUDs_As数据中的确认状态acknowledged该为1已确认
POST my_index/_doc/4XEu8IEBgjRHnDUDs_As/_update?pretty
{
"doc":{
"acknowledged":1
}
}
上面的方法被弃用,推荐 {index}/_update/{id}
POST my_index/_update/4XEu8IEBgjRHnDUDs_As
{
"doc":{
"acknowledged":1
}
}
5.2.查询指定id下的数据
GET my_index/_doc/4XEu8IEBgjRHnDUDs_As
5.3.按条件批量修改
例如:将等级为5的数据全部修改为等级为0
POST my_index/_update_by_query
{
"script": {
"source": "ctx._source['severity']=0"
},
"query": {
"bool": {
"must": [
{
"term": {
"severity": {
"value": 5
}
}
}
]
}
}
}
6.统计总存储空间占用
GET /_cat/indices?v
es 中会默认存在一个名为.kibana的索引
表头 | 含义 |
---|---|
health | green(集群完整) yellow(单点正常、集群不完整) red(单点不正常) |
status | 是否能使用 |
index | index |
uuid | 索引统一编号 |
pri | 主节点几个 |
rep | 从节点几个 |
docs.count | 文档数 |
docs.deleted | 文档被删了多少 |
store.size | 整体占空间大小 |
pri.store.size | 主节点占 |
7.查看某个索引下的数据
GET 索引名/类型/_search
GET my_index/_search
8.精确值匹配
8.1.term
term 查询被用于精确值匹配,这些精确值可能是数字、时间、布尔或者那些 not_analyzed 的字符串。
例如:查询描述summar为“1分钟内交易响应率小于85.0%”的数据
GET my_index/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"summary": {
"value": "1分钟内交易响应率小于85.0%"
}
}
}
]
}
}
}
因为summary的类型是text,分词后全值匹配检索,将无法检索。在搜索字段后增加.keyword搜索。
GET my_index/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"summary.keyword": {
"value": "1分钟内交易响应率小于85.0%"
}
}
}
]
}
}
}
8.2.terms
和term查询一样,但是它允许指定多个值进行匹配
例如:查询等级severity为1、2、3的数据
GET my_index/_search
{
"query": {
"terms": {
"severity": [
1,
2,
3
]
}
}
}
9.match查询
9.1.match_all全部查询
match_all 查询简单的匹配所有文档。在没有指定查询方式时,它是默认的查询
GET my_index/_search
{
"query":