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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
| 一、安装
1、jdk 和 环境变量
支持jdk-1.7以上,推荐jdk-1.8
在环境变量配置:JAVA_HOME
2、安装
有2种方式下载,推荐缓存rpm包到本地yum源
1)直接使用rpm
wget https://download.elastic.co/elas ... ticsearch-2.4.0.rpm
2)使用yum源
[iyunv@vm220 ~]# vim /etc/yum.repos.d/elasticsearch.repo
[elasticsearch-2.x]
name=Elasticsearch repository for 2.x packages
baseurl=https://packages.elastic.co/elasticsearch/2.x/centos
gpgcheck=1
gpgkey=https://packages.elastic.co/GPG-KEY-elasticsearch
enabled=1
[iyunv@vm220 ~]# yum install elasticsearch
3)启动服务
[iyunv@vm220 ~]# chkconfig elasticsearch on
[iyunv@vm220 ~]# service elasticsearch start
3、调整配置
[iyunv@vm220 ~]# mkdir -p /data/elasticsearch
[iyunv@vm220 ~]# chown elasticsearch:elasticsearch /data/elasticsearch
[iyunv@vm220 ~]# grep ^[^#] /etc/elasticsearch/elasticsearch.yml
cluster.name: es-test
node.name: node-1
path.data: /data/elasticsearch
path.logs: /var/log/elasticsearch
[iyunv@vm220 ~]# service elasticsearch restart
二、使用 REST API
1、能干啥
Check your cluster, node, and index health, status, and statistics
Administer your cluster, node, and index data and metadata
Perform CRUD (Create, Read, Update, and Delete) and search operations against your indexes
Execute advanced search operations such as paging, sorting, filtering, scripting, aggregations, and many others
使用 curl 来操作 API 的方式:
curl -X<REST Verb> <Node>:<Port>/<Index>/<Type>/<ID>
2、管理
1)健康状态
[iyunv@vm220 ~]# curl 'localhost:9200/_cat/health?v'
epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1473669150 16:32:30 es-test green 1 1 0 0 0 0 0 0 - 100.0%
health 的状态包括:green, yellow, red.
Green means everything is good (cluster is fully functional),
yellow means all data is available but some replicas are not yet allocated (cluster is fully functional)
red means some data is not available for whatever reason
2)列出节点
[iyunv@vm220 ~]# curl 'localhost:9200/_cat/nodes?v'
host ip heap.percent ram.percent load node.role master name
127.0.0.1 127.0.0.1 6 16 0.00 d * node-1
3)列出索引
[iyunv@vm220 ~]# curl 'localhost:9200/_cat/indices?v'
health status index pri rep docs.count docs.deleted store.size pri.store.size
3、CRUD操作
1)创建索引
[iyunv@vm220 ~]# curl -XPUT 'localhost:9200/customer?pretty'
{
"acknowledged" : true
}
创建了一个索引“customer”,且告知返回时使用一个 pretty-print 的方式(json)
再次列出索引
[iyunv@vm220 ~]# curl 'localhost:9200/_cat/indices?v'
health status index pri rep docs.count docs.deleted store.size pri.store.size
yellow open customer 5 1 0 0 650b 650b
请对比一下之前的执行结果。
请注意,这里的 health 是 yellow,因为我们目前只有一个es节点,没有副本,未做到高可用。
2)给上述索引创建一个 doc
索引 customer 类型为: "external" ,ID为:1
[iyunv@vm220 ~]# curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '
{
"name": "John Doe"
}'
3)获取 doc
[iyunv@vm220 ~]# curl -XGET 'localhost:9200/customer/external/1?pretty'
4)重建索引
[iyunv@vm220 ~]# curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '
{
"name": "Kelly Doe"
}'
5)创建索引时,不指定ID,将随机生成
[iyunv@vm220 ~]# curl -XPOST 'localhost:9200/customer/external?pretty' -d '
{
"name": "Calvin Kern"
}'
6)删除索引
[iyunv@vm220 ~]# curl -XDELETE 'localhost:9200/customer?pretty'
7)更新 doc 中的数据
[iyunv@vm220 ~]# curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '
{
"doc": { "name": "Eric Mood" }
}'
更新并增加:
[iyunv@vm220 ~]# curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '
{
"doc": { "name": "Eric Mood", "age": 110 }
}'
8)删除索引太直接了,如何只删除其中某个doc呢?
[iyunv@vm220 ~]# curl -XDELETE 'localhost:9200/customer/external/1?pretty'
4、查看数据
1)导入测试数据:
wget https://github.com/bly2k/files/blob/master/accounts.zip?raw=true -O accounts.zip
unzip accounts.zip
curl -XPOST 'localhost:9200/bank/account/_bulk?pretty' --data-binary "@accounts.json"
curl 'localhost:9200/_cat/indices?v'
2)search
查看所有的数据:
方式一:
curl 'localhost:9200/bank/_search?q=*&pretty'
方式二:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_all": {} }
}'
查看指定的数据:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": {
"bool": {
"must": [
{ "match": { "age": "40" } }
],
"must_not": [
{ "match": { "state": "ID" } }
]
}
}
}'
筛选数据:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": {
"bool": {
"must": { "match_all": {} },
"filter": {
"range": {
"balance": {
"gte": 20000,
"lte": 30000
}
}
}
}
}
}'
汇总数据:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state"
}
}
}
}'
备注:具体内容请参考官网doc
ZYXW、参考
1、官网
https://www.elastic.co/guide/en/ ... p-repositories.html
https://www.elastic.co/guide/en/ ... -configuration.html
https://www.elastic.co/guide/en/ ... tup-dir-layout.html
https://www.elastic.co/guide/en/ ... g_your_cluster.html
|