elasticsearch之基本命令

基本命令

  • 环境:Mac 10.13.4 、elasticsearch 5.5.1、jdk8

索引(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
$ put http://[ip]:[port]/[索引名]
## 创建时指定分片(3个主分片,2个副本分片)
参数:
{
"settings" : {
"index" : {"number_of_shards" : 3, "number_of_replicas" : 2}
}
}
## 修改副本分片
$ put http://[ip]:[port]/[索引名]/_settings
参数:
{
"number_of_replicas" : 1}
}
## 创建索引,并添加log类型,字段为message,字段类型为string(已存在索引会失败)
$ put http://[ip]:[port]/[索引名]
参数:
{
"mappings": {
"log" : {
"properties" {
"message" :{"type" : "string"}
}
}
}
}

查询索引

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$ get http://[ip]:[port]/[索引名]/[_mapping,_settings,_aliase]
{
"[索引名]": {
"aliases": {},
"mappings": {},
"settings": {
"index": {
"creation_date": "1524617001036",
"number_of_shards": "3",
"number_of_replicas": "1",
"uuid": "jk7IHv-jQpyd4jwOeo86ag",
"version": {
"created": "5050199"
},
"provided_name": "[索引名]"
}
}
}
}

删除索引

1
2
$ delete http://[ip]:[port]/[索引名],[索引名],...
或者使用_all或*删除全部索引(注意:务必要在配置文件中禁用 action.destructive_requires_name:true)

修改索引

修改索引副本数量

1
2
3
4
5
$ put http://[ip]:[port]/[索引名]/_settings
参数:
{
"number_of_replicas" : 1
}

打开/关闭索引

1
2
3
4
5
6
## 因为关闭索引磁盘空间并不会释放,造成磁盘空间浪费,因此一般禁用该功能,settings,cluster.indices.close.enable:false
$ post http://[ip]:[port]/[索引名]/[_open,_close]
返回值:
{
"acknowledged": true
}

映射

修改字段

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
## name为对象类型(Object datatype)
$ put http://[ip]:[port]/[索引名]/_mappings/user
参数:
{
"properties": {
"name": {
"properties": {
"last": {"type" : "string"}
}
},
"user_id": {
"type": "string",
"index": "not_analyzed"
}
}
}

文档(Document)

增删改查

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
## 添加文档
$ put/post http://[ip]:[port]/[索引名]/[类型]/[_id]
参数:{"computer":"huawei"}
返回值:
{
"_index": "index1_test",
"_type": "test",
"_id": "3",
"_version": 1,
"result": "created",
"_shards": { "total": 2,"successful": 1,"failed": 0},
"created": true
}
## 删除文档
$ delete http://[ip]:[port]/[索引名]/[类型]/[_id]
返回值:
{
"found": true,
"_index": "index1_test",
"_type": "test",
"_id": "3",
"_version": 2,
"result": "deleted",
"_shards": { "total": 2,"successful": 1,"failed": 0}
}
## 修改文档
$ post http://[ip]:[port]/[索引名]/[类型]/[_id]/_update
参数:
{
"doc" : {
"computer" : "apple"
}
}
## 查询文档
$ get http://[ip]:[port]/[索引名]/[类型]/[_id]
返回值:
{
"_index": "index1_test",
"_type": "test1",
"_id": "100",
"_version": 2,
"found": true,
"_source": { "computer": "apple"}
}
真的如上面这么简单,就不是es了。=!=
坚持原创技术分享,您的支持将鼓励我继续创作!
Fork me on GitHub