我的ElasticSearch命令简记

常用简单命令

条件删除数据

1
2
3
4
5
6
7
# 条件删除
curl -XPOST "http://localhost:9200/opt-log-index/opt-log-type/_delete_by_query" -H 'Content-Type: application/json' -d'
{
"query": {
"match_all": {}
}
}'

删除index和数据

1
curl -XDELETE "http://localhost:9200/opt-log-index"

获取 mapping 结构

1
2
3
4
5
# 获取所有的index的mapping
curl -XGET "http://localhost:9200/_mapping"

# 获取指定的index的mapping结构
curl -XGET "http://localhost:9200/opt-log-index/_mapping"

创建index和mapping

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 创建index和mapping
curl -XPUT "http://localhost:9200/opt-log-index" -H 'Content-Type: application/json' -d'
{
"settings":{
"analysis":{
"analyzer":{
"ik":{
"tokenizer":"ik_max_word"
}
}
}
},
"mappings":{
"opt-log-type":{
"properties":{
"id":{
"type":"keyword"
},
"operator":{
"type":"keyword"
},
"role":{
"type":"keyword"
},
"operatorName":{
"type":"text",
"analyzer":"ik",
"search_analyzer":"ik_max_word"
},
"remark":{
"type":"keyword"
},
"operateType":{
"type":"keyword"
},
"txId":{
"type":"long"
},
"schemaName":{
"type":"text",
"analyzer":"ik",
"search_analyzer":"ik_max_word"
},
"tableName":{
"type":"text",
"analyzer":"ik",
"search_analyzer":"ik_max_word"
},
"afterData":{
"type":"keyword"
},
"beforeData":{
"type":"keyword"
},
"changeFields":{
"type":"keyword"
},
"createdAt":{
"type":"date",
"format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
},
"updatedAt":{
"type":"date",
"format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
}
}
}'

查询数据

查询所有数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
curl -XGET "http://localhost:9200/opt-log-index/opt-log-type/_search" -H 'Content-Type: application/json' -d'
{
"from": 0,
"size": 20,
"query": {
"bool": {
"must": [
{
"match_all": {}
}
]
}
},
"_source": {
"excludes": [
"beforeData"
]
}
}'

精准匹配

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
curl -XGET "http://localhost:9200/opt-log-index/opt-log-type/_search" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"must": [
{
"term": {
"tableName": "parana_item_detail"
}
}
]
}
},
"_source": {
"excludes": [
"beforeData","schemaName"
]
},
"sort": [
{
"createdAt": {
"order": "desc"
}
}
]
}'