Elasticsearch、Kibana 使用

通过最近一段时间对`ES`的基础使用,也算有了一定的了解,特在此记录一下,避免以后忘记。

ES、Kibana、Elasticsearch-head安装

docker一键部署

docker-compose.yaml文件

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
version: '3'

services:
es:
image: elasticsearch:7.6.2
restart: always
ports:
- "9200:9200"
- "9300:9300"
environment:
- "discovery.type=single-node"
- "ES_JAVA_OPTS=-Xms64m -Xmx512m"
volumes:
- esdata:/usr/share/elasticsearch/data
- esplugins:/usr/share/elasticsearch/plugins
user: root
networks:
- es
kibana:
image: kibana:7.6.2
restart: always
environment:
- ELASTICSEARCH_HOSTS=http://elasticsearch:9200
- I18N_LOCALE=zh-CN
# - ELASTICSEARCH_USERNAME=elastic
# - ELASTICSEARCH_PASSWORD=mypasword
depends_on:
- es
ports:
- "5601:5601"
links:
- "es:elasticsearch"
networks:
- es

volumes:
esdata:
driver: local
esplugins:
driver: local
networks:
es:

快速使用

启动:docker-compose up -d

删除:docker-compose down

查看容器信息:docker inspect container-name-xxx

安装ik分词

docker volume ls 查看所有挂载

docker volume inspect es_esplugins查看挂载信息后,找到挂载地址Mountpoint对应地址。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
root@dns:~/downloads/es_search_fastapi# docker volume inspect es_esplugins
[
{
"CreatedAt": "2021-12-22T15:42:14+08:00",
"Driver": "local",
"Labels": {
"com.docker.compose.project": "es",
"com.docker.compose.version": "1.29.2",
"com.docker.compose.volume": "esplugins"
},
"Mountpoint": "/var/lib/docker/volumes/es_esplugins/_data",
"Name": "es_esplugins",
"Options": null,
"Scope": "local"
}
]
  1. https://github.com/medcl/elasticsearch-analysis-ik 下载对应版本
  2. 解压到 /var/lib/docker/volumes/es_esplugins/_data/ik目录下(没有ik目录,就自己创建一个)
  3. 重启 ES容器 docker restart es_es_1,就可以了

Elasticsearch-head 插件

我使用的是[谷歌插件](https://chrome.google.com/webstore/detail/elasticsearch-head/ffmkiejjmecolpfloofpjologoblkegm?utm_source=chrome-ntp-icon),可以自行搜索下载。

Kibana对ES操作使用

操作

本来想写的,但是觉得这样内容太冗余了。后面放一些我收藏的别人的写的吧,毕竟一直重复也没有任何意义。

注意事项

  1. ik分词会对内容分词,因此需要全匹配的不适用ikkeyword
  2. _reindex时,内容过多会超时,因此加上?wait_for_completion=false,不用等待
  3. ES分页修改查询10000限制,index.max_result_window 下面有修改语句
  4. ES7.0版本以后,一个index对应一个type了,不再是以前的一个index对应多个type,因此index也相当于一张表了

代码记录

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
GET _search
{
"query": {
"match_all": {}
}
}
# 修改查询10000条限制
PUT data_item/_settings
{
"index.max_result_window":1000000
}
# 获取最后一个id,保持增量更新
GET data_item/_search
{
"_source": "item_id",
"sort": [
{
"item_id": {
"order": "desc"
}
}
]
, "size": 1
}
# 搜索推荐词 推荐
GET data_item/_search
{
"suggest": {
"my-suggestion": {
"text": "科技",
"term": {
"suggest_mode": "popular",
"field": "item_title"
}
}
}
}
# 获取索引映射
GET data_item/_mapping
# 删除索引
DELETE data_item
# 新建索引
PUT data_item
{
"mappings" : {
"properties" : {
"area_code" : {
"type" : "integer"
},
"create_time" : {
"type" : "date"
},
"dept_id" : {
"type" : "long"
},
"dept_name" : {
"type" : "text",
"analyzer" : "ik_max_word",
"search_analyzer" : "ik_smart"
},
"item_content" : {
"type" : "text",
"analyzer" : "ik_max_word",
"search_analyzer" : "ik_smart"
},
"item_deadline" : {
"type" : "date"
},
"item_id" : {
"type" : "long"
},
"item_id_key" : {
"type" : "text",
"index" : false
},
"item_imgurl" : {
"type" : "text",
"index" : false
},
"item_pulishdate" : {
"type" : "date"
},
"item_title" : {
"type" : "text",
"analyzer" : "ik_max_word",
"search_analyzer" : "ik_smart"
},
"item_type" : {
"type" : "long"
},
"item_type_name" : {
"type" : "text"
},
"item_url" : {
"type" : "text",
"index" : false
},
"level_code" : {
"type" : "long"
},
"level_code_name" : {
"type" : "text"
}
}
}
}
# 索引内容转移到另一个索引
POST _reindex?wait_for_completion=false
{
"source": {
"index": "data_item",
"size": 5000
},
"dest": {
"index": "data_item_project"
}
}

# 目前我这里查询最复杂的一种
GET data_item/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"dept_id": 8
}
},
{
"term": {
"item_type": 1
}
},
{
"term": {
"level_code": 2
}
},
{
"match": {
"item_title": "创新"
}
}
],
"filter": {
"range": {
"item_pulishdate": {
"gte": "2021-01-11"
}
}
},
"should": [
{
"term": {
"area_code": "510100"
}
},
{
"term": {
"area_code": "510300"
}
}
]
}
},
"highlight": {
"pre_tags": [
"<em>"
],
"post_tags": [
"</em>",
],
"fields": {
"item_title": {}
}
},
"from": 0,
"size": 10
}