BlueXIII's Blog

热爱技术,持续学习

0%

Elasticsearch学习笔记

GUI

elasticvue

  • chrome插件
  • 查询 + 管理

elasticsearch-head

https://github.com/mobz/elasticsearch-head

  • chrome插件
  • 查询 + 管理

Kaizen

https://www.elastic-kaizen.com/

  • java桌面版
  • 纯查询

elasticsearch-hq

https://github.com/ElasticHQ/elasticsearch-HQ

docker run -p 5000:5000 elastichq/elasticsearch-hq
open http://localhost:5000/

  • docker run
  • 监控 + 简单查询

djavu

https://github.com/appbaseio/dejavu/

1
2
docker run -p 1358:1358 -d appbaseio/dejavu
open http://localhost:1358/

参考文档

https://www.ruanyifeng.com/blog/2017/08/elasticsearch.html
https://cloud.tencent.com/developer/article/1547867
https://juejin.cn/post/6844903919013855240
https://www.elastic.co/guide/cn/elasticsearch/guide/current/query-dsl-intro.html
https://n3xtchen.github.io/n3xtchen/elasticsearch/2017/07/05/elasticsearch-23-useful-query-example
https://www.jianshu.com/p/eb30eee13923
https://zhuanlan.zhihu.com/p/34240906
https://feifeiyum.github.io/2020/02/27/es-mapping/
https://cloud.tencent.com/developer/article/1189279

Kibana教程

https://www.ruanyifeng.com/blog/2017/08/elasticsearch.html
https://www.jianshu.com/p/7ca9e5b02ef6
https://www.elastic.co/guide/cn/kibana/current/setup.html

KibanaDevTool

查询

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
# 查看
GET /_cat

# 查看Index
GET /_cat/indices?v

# 查看健康
GET /_cat/health?v

# 查看全部mapping
GET /_mapping?pretty=true


# 创建索引
PUT /customer?pretty

# 创建文档
PUT /customer/_doc/1?pretty
{
"name": "John Doe"
}

# 根据ID查询文档
GET /customer/_doc/1?pretty

# 查询所有文档
GET /customer/_search?q=*&sort=name:asc&pretty

# 查看索引的mapping
GET /customer/_mapping?pretty=true

# 查看索引的settings
GET /customer/_settings?pretty=true


GET /bookdb_index/book/_search?q=guide

{
"query": {
"multi_match" : {
"query" : "guide",
"fields" : ["_all"]
}
}
}

查询2

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
70
# 查询
GET /crawl_log/_search/
{
"query": {
"match_all": {}
}
}

# 基本匹配
GET /bookdb_index/book/_search?q=guide

GET /bookdb_index/book/_search
{
"query": {
"multi_match" : {
"query" : "guide",
"fields" : ["_all"]
}
}
}

# 词条查询
POST /bookdb_index/book/_search
{
"query": {
"term" : {
"publisher": "manning"
}
},
"_source" : ["title","publish_date","publisher"]
}

# 多词条查询
POST /bookdb_index/book/_search
{
"query": {
"terms" : {
"publisher": ["oreilly", "packt"]
}
}
}

# 排序
POST /bookdb_index/book/_search
{
"query": {
"term" : {
"publisher": "manning"
}
},
"_source" : ["title","publish_date","publisher"],
"sort": [
{ "publish_date": {"order":"desc"}}
]
}

# 范围查询
POST /bookdb_index/book/_search
{
"query": {
"range" : {
"publish_date": {
"gte": "2015-01-01",
"lte": "2015-12-31"
}
}
},
"_source" : ["title","publish_date","publisher"]
}

查询3

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
# match_all
POST /crawl_log/_search
{
"query": {
"match_all": {}
}
}

# term
POST /crawl_log/_search
{
"query":{
"term":{
"task_id":1
}
}
}

# from size
POST /crawl_log/_search
{
"query":{
"term":{
"task_id":1
},
},
"from": 10,
"size": 10
}

# match
POST /crawl_log/_search
{
"query": {
"match": {
"message": "中埃建交五十周年"
}
}
}

# match_phase
POST /crawl_log/_search
{
"query": {
"match_phase": {
"message": "中埃建交五十周年"
}
}
}

日志查询

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
GET /crawl_log

GET /crawl_log/_search


POST /crawl_log/_search
{
"query": {
"match_all": {}
}
}

POST /crawl_log/_search
{
"query": {
"bool": {
"must": [{
"term": {
"task_id": "1"
}
},
{
"match": {
"level_name": "INFO"
}
}
],
"must_not": [],
"should": []
}
},
"from": 0,
"size": 10,
"sort": [],
"aggs": {}
}

管理

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
# 创建一个索引
PUT twitte
{
"settings" : {
"number_of_shards" : 3,
"number_of_replicas" : 2
}
}

# 创建mapping映射
PUT twitte
{
"settings" : {
"index" : {
"number_of_shards" : 3,
"number_of_replicas" : 2
}
},
"mappings" : {
"type1" : {
"properties" : {
"field1" : { "type" : "text" }
}
}
}
}

# 查看索引的定义信息
GET /twitte

# 删除索引
DELETE /twitte

别名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 查看别名
GET /_cat/aliases

# 增加一个别名
POST /_aliases
{"actions": [{"add": {"index": "twitte", "alias": "alias1"}}]}

# 删除别名
POST /_aliases
{"actions": [{"remove": {"index": "twitte", "alias": "alias1"}}]}

# 一个别名对应多个索引
POST /_aliases
{"actions": {"add": {"indices": ["twitte", "twitte2"], "alias": "alias1"}}}

# 一个别名对应多个索引(通配符)
POST /_aliases
{"actions": [{"add": {"index": "twitte*", "alias": "alias1"}}]}

索引复制

1
2
3
4
5
6
7
8
9
10
11
POST /_reindex?slices=9&refresh&wait_for_completion=false

POST _reindex?wait_for_completion=false
{
"source": {
"index": "crawl_log"
},
"dest": {
"index": "crawl_log_bak"
}
}

mapping

  • 字符串: text和keyword
  • 整数 : byte, short, integer, long
  • 浮点数: float, double
  • 布尔型: boolean
  • 日期: date
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 创建索引
PUT /my-index
{
"mappings" {
"properties": {
"age": {"type": "integer"},
"email": {"type": "keyword"},
"name": {"type": "text"}
}
}
}

# 添加字段
PUT /my-index/_mapping
{
"properties": {
"content": {"type": "text"}
}
}

清空数据

1
2
3
4
5
6
7
8
# 清空索引
POST /policies_hydrabot/_delete_by_query?refresh&slices=5&pretty
{
"query": {
"match_all": {}
}
}

Curl请求示例

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
curl -X GET 'http://localhost:9200/_cat/indices?v'
curl -X GET 'http://localhost:9200/_mapping?pretty=true'
curl -X GET 'http://localhost:9200/crawl_log/_mapping?pretty=true'

curl -X PUT 'localhost:9200/weather'

curl -X DELETE 'localhost:9200/weather'

curl -X PUT 'http://localhost:9200/accounts/person/1' \
-H 'Content-Type: application/json' \
-d '{
"user": "张三",
"title": "工程师",
"desc": "数据库管理"
}'


curl -X POST 'http://localhost:9200/accounts/person' \
-H 'Content-Type: application/json' \
-d '{
"user": "李四",
"title": "工程师",
"desc": "系统管理"
}'


curl 'localhost:9200/accounts/person/1?pretty=true'
curl 'localhost:9200/weather/beijing/abc?pretty=true'

curl -X DELETE 'localhost:9200/accounts/person/1'

curl 'localhost:9200/accounts/person/_search'


curl 'localhost:9200/accounts/person/_search' \
-H 'Content-Type: application/json' \
-d '
{
"query" : { "match" : { "desc" : "软件" }}
}'