elasticsearch常用curl命令


基本概念

Index:Elastic 數據管理的頂層單位就叫做 Index(索引)每個 Index (即數據庫)的名字必須是小寫。
Document:Index 里面單條的記錄稱為 Document(文檔)。許多條 Document 構成了一個 Index。
Document 使用 JSON 格式表示
type:將document進行分組,這種分組就叫做 Type,它是虛擬的邏輯分組,用來過濾 Document。(具體理解可能有偏差歡迎留言糾正)
根據規划,Elastic 6.x 版只允許每個 Index 包含一個 Type,7.x 版將會徹底移除 Type。
常用操作命令

新建index

請求方式 PUT,其中index_01表示數據庫名稱(數據庫名稱必須為小寫)

192.168.1.145:9200/index_01
1
返回結構:acknowledged:true表示操作成功

{
"acknowledged": true,
"shards_acknowledged": true,
"index": "index_01"
}
1
2
3
4
5
刪除index

請求方式 DELETE (與新建index的區別是請求方式使用了DELETE)

192.168.1.145:9200/index_01
1
返回結構:

{
"acknowledged": true
}
1
2
3
新增記錄

請求方式: PUT
index_01: 為index(相當於關系型數據庫中的數據庫)
type_01:表示數據放在type_01類型下
1: 表示指定ID為1(可以為任意字符)

192.168.1.145:9200/index_01/type_01/1
參數:{
"name":"張三",
"sex":"男",
"age":18
}
1
2
3
4
5
6
返回結構:

{
"_index": "index_01",
"_type": "type_01",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
1
2
3
4
5
6
7
8
9
10
11
12
13
表示添加ID為1的數據創建成功;如果不想在添加的時候指定ID只需要把請求方式給成PUT,然后將路徑下最后的1去除即可;

POST 192.168.1.145:9200/index_01/type_01
請求參數:
{
"name":"李四",
"sex":"女",
"age":20
}

1
2
3
4
5
6
7
8
返回參數:

{
"_index": "index_01",
"_type": "type_01",
"_id": "AWu2mg3fQX3MA7vSN8IN", //表示elasticsearch自動生成的ID
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
1
2
3
4
5
6
7
8
9
10
11
12
13
請求記錄

請求方式GET pretty=true表示以易讀的格式化返回

192.168.1.145:9200/index_01/type_01/1?pretty=true
1
返回參數:

{
"_index": "index_01",
"_type": "type_01",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"name": "張三",
"sex": "男",
"age": 18
}
}
1
2
3
4
5
6
7
8
9
10
11
12
如果Id不正確,查詢不到數據時,found字段就是false

刪除記錄

請求方式DELETE

192.168.1.145:9200/index_01/type_01/1
1
返回參數:

{
"found": true,
"_index": "index_01",
"_type": "type_01",
"_id": "1",
"_version": 2,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
表示刪除成功

更新記錄

請求方式PUT
和添加的時候請求方式一樣(elasticsearch會檢測有想相同ID自動更新)

192.168.1.145:9200/index_01/type_01/1
請求參數:
{
"name":"張三01",
"sex":"男",
"age":19
}
1
2
3
4
5
6
7
返回參數:

{
"_index": "index_01",
"_type": "type_01",
"_id": "1",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": false
}
1
2
3
4
5
6
7
8
9
10
11
12
13
可以看到與添加時返回參數的區別 “created”: false, “result”: “updated”,"_version": 2,每次進行修改對應的版本都會加1

查詢所有數據

請求方式GET
直接請求/Index/Type/_search,就會返回所有記錄

192.168.1.145:9200/index_01/type_01/_search
1
返回結果:

{
"took": 4,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "index_01",
"_type": "type_01",
"_id": "AWu2mg3fQX3MA7vSN8IN",
"_score": 1,
"_source": {
"name": "李四",
"sex": "女",
"age": 20
}
},
{
"_index": "index_01",
"_type": "type_01",
"_id": "1",
"_score": 1,
"_source": {
"name": "張三01",
"sex": "男",
"age": 19
}
}
]
}
}
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
其中
took:表示查詢消耗的時間(毫秒)
timed_out:是否超時
hits:表示命中的記錄 ,里面的字段 total:表示返回的記錄數,max_score:最高匹配程度,hits:返回查詢出來的數組

條件查找

請求方式POST

192.168.1.145:9200/index_01/type_01/_search
請求參數:
{
"query": {
"match": {
"name": "李四"
}
}
}
1
2
3
4
5
6
7
8
9
使用match查詢方法,查找指定條件 name 字段為“李四”的所有數據
返回參數:

{
"took": 10,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.51623213,
"hits": [
{
"_index": "index_01",
"_type": "type_01",
"_id": "AWu2mg3fQX3MA7vSN8IN",
"_score": 0.51623213,
"_source": {
"name": "李四",
"sex": "女",
"age": 20
}
}
]
}
}
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
elasticsearch默認一次返回10條記錄,可以通過size字段改變這個設置

{
"query": {
"match": {
"name": "李四"
}
},
"size":1 //只返回一條數據
}
1
2
3
4
5
6
7
8
還可以通過from字段(默認從位置0開始查詢),指定開始查詢位置
從第一條開始查詢,查詢一條數據(相當於mysql中limit(from,size))

{
"query": {
"match": {
"name": "李四"
}
},
"size":1,
"from":1
}
1
2
3
4
5
6
7
8
9
邏輯運算

請求方式POST
從位置0開始查詢查詢5條匹配的數據 name中包含 “李四”或者“張三”的數據

192.168.1.145:9200/index_01/type_01/_search
請求參數:
{
"query": {
"match": {
"name": "李四 張三"
}
},
"size":5,
"from":0
}
1
2
3
4
5
6
7
8
9
10
11
返回結構

{
"took": 11,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.51623213,
"hits": [
{
"_index": "index_01",
"_type": "type_01",
"_id": "AWu2mg3fQX3MA7vSN8IN",
"_score": 0.51623213,
"_source": {
"name": "李四",
"sex": "女",
"age": 20
}
},
{
"_index": "index_01",
"_type": "type_01",
"_id": "1",
"_score": 0.5063205,
"_source": {
"name": "張三01",
"sex": "男",
"age": 19
}
}
]
}
}
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
這種查詢方式相當於mysql中的 or查詢
如果需要and條件查詢 需要使用bool查詢方式

192.168.1.145:9200/index_01/type_01/_search
請求參數:
{
"query": {
"bool":{
"must":[
{"match": { "name": "李" }},
{"match": { "name": "四"}}
]
}
},
"size":5,
"from":0
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
返回結果:

{
"took": 13,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.51623213,
"hits": [
{
"_index": "index_01",
"_type": "type_01",
"_id": "AWu2mg3fQX3MA7vSN8IN",
"_score": 0.51623213,
"_source": {
"name": "李四",
"sex": "女",
"age": 20
}
}
]
}
}
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
至上是elasticsearch基本的操作功能;
---------------------


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM