說明
本文來自於博客園,用以記錄通過訪問接口的方式來訪問 es 的簡單操作。
大部分源自原文,但是因為ES版本不同,做了部分內容修改。此文ES版本7.6

es & mysql 概念對比表。type類型將會在以后的版本中被移除
es 接口訪問示例
1.文檔操作
1.索引操作
建立索引就相當於創建數據庫,創建數據庫就要創建表結構。es的建立索引就是創建數據庫這一操作。
創建索引:
PUT http://localhost:9200/zq_test # 建立一個名為 zq_test 的索引
返回結果:
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "zq_test"
}
獲取索引
GET http://localhost:9200/zq_test
返回結果:
{
"zq_test": {
"aliases": {},
"mappings": {},
"settings": {
"index": {
"creation_date": "1623325887846",
"number_of_shards": "1",
"number_of_replicas": "1",
"uuid": "V1Ewmh1sSAmPwtiUqgfY-Q",
"version": {
"created": "7060099"
},
"provided_name": "zq_test"
}
}
}
}
2.設置mapping
設置Index的Mapping,也就是設置數據的表結構
刪除索引:
DELEET http://localhost:9200/zq_test
返回:
{
"acknowledged": true
}
重新創建索引:
PUT http://localhost:9200/zq_test # 並提交一下參數,content-type:application/json
{
"mappings": {
"properties": {
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"age": {
"type": "long",
"index": true
},
"gender": {
"type": "keyword"
}
}
}
}
返回:
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "zq_test"
}
name:text類型,會進行分詞,支持模糊檢索。
name.keyword : 這相當於是嵌套了一個字段,keyword類型,只能精確匹配,不支持分詞。超過256字符長度不索引,也就沒法搜索到。
age:long類型,支持精確匹配。index 為 true 才可以匹配
gender:keyword類型,只能精確匹配,不支持分詞。
3.插入文檔
插入文檔,即使用接口,將 json 文檔放入 es 索引中。
請求必須指定文檔的索引名稱,唯一的文檔 ID, 以及請求體中一個或多個鍵值對。
PUT http://localhost:9200/zq_test/_doc/2
# json
{
"name": "tom",
"age": 5,
"gender": "man"
}
返回:
{
"_index": "zq_test",
"_type": "_doc",
"_id": "2",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1
}
4.獲取文檔
根據 id 獲取文檔。
GET http://localhost:9200/zq_test/_doc/1
返回:
{
"_index": "zq_test",
"_type": "_doc",
"_id": "1",
"_version": 1,
"_seq_no": 0,
"_primary_term": 1,
"found": true,
"_source": {
"name": "yezi test",
"age": 25,
"gender": "man"
}
}
5.刪除文檔
根據 id 刪除文檔
DELETE http://localhost:9200/zq_test/_doc/2
返回:
{
"_index": "zq_test",
"_type": "_doc",
"_id": "2",
"_version": 2,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 1
}
加入兩個測試數據
PUT http://localhost:9200/zq_test/_doc/3
{
"name": "ceshi01",
"age": 35,
"gender": "man"
}
PUT http://localhost:9200/zq_test/_doc/4
{
"name": "ceshi02",
"age": 40,
"gender": "woman"
}
根據篩選條件刪除數據
# 根據年齡刪除數據
Post http://localhost:9200/zq_test/_doc/_delete_by_query
# json body
{
"query": {
"match": {
"age": 35
}
}
}
返回:
{
"took": 13,
"timed_out": false,
"total": 1,
"deleted": 1,
"batches": 1,
"version_conflicts": 0,
"noops": 0,
"retries": {
"bulk": 0,
"search": 0
},
"throttled_millis": 0,
"requests_per_second": -1.0,
"throttled_until_millis": 0,
"failures": []
}
可使用
{"query": {"match_all":{}}}的方式刪除所有
6.更新文檔
覆蓋更新(先刪除后添加):
更新完成后,可以看到原有 name age gender只剩下 age,相當於刪除原有。
Put http://localhost:9200/zq_test/_doc/4
# json body
{
"age": 500
}
返回:
{
"_index": "zq_test",
"_type": "_doc",
"_id": "4",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 3,
"_primary_term": 1
}
部分更新(不刪除,更新相應字段) 建議用此
POST http://localhost:9200/zq_test/_doc/4/_update
# json body
{
"doc": {
"age": 10000,
"gender": "aha"
}
}
返回:
{
"_index": "zq_test",
"_type": "_doc",
"_id": "4",
"_version": 6,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 13,
"_primary_term": 1
}
# 對應的數據如下
{
"_index": "zq_test",
"_type": "_doc",
"_id": "4",
"_version": 6,
"_seq_no": 13,
"_primary_term": 1,
"found": true,
"_source": {
"name": "jerry",
"age": 10000,
"gender": "aha"
}
}
根據查詢條件更新
POST http://localhost:9200/zq_test/_doc/_update_by_query
# json body 2021-06-18 18:18 試驗失敗
{
"script": {
"source": "ctx._source.age=5", # ctx._source 指源文檔
"lang": "painless"
},
"query": {
"term": {
"name": "ceshi01"
}
}
}
返回:
{
"_index": "zq_test",
"_type": "_doc",
"_id": "_update_by_query",
"found": false
}
7.批量獲取
批量獲取文檔(不指定索引庫)
GET http://localhost:9200/_mget
# json 鏈接中沒有指明索引庫,但是在 json body 中進行了指定
{
"docs": [
{
"_index": "zq_test",
"_type": "_doc",
"_id": 4
},
{
"_index": "zq_test_copy",
"_type": "_doc",
"_id": 4
}
]
}
返回:
{
"docs": [
{
"_index": "zq_test",
"_type": "_doc",
"_id": "4",
"_version": 3,
"_seq_no": 4,
"_primary_term": 1,
"found": true,
"_source": {
"name": "jerry",
"age": 500,
"gender": "man"
}
},
{
"_index": "zq_test_copy",
"_type": "_doc",
"_id": "4",
"_version": 2,
"_seq_no": 1,
"_primary_term": 1,
"found": true,
"_source": {
"name": "jerry",
"age": 500,
"gender": "man"
}
}
]
}
批量獲取文檔(指定索引庫或類型)
# 一
GET http://localhost:9200/zq_test/_mget
# json 中指明 _id, 鏈接中指明 index
{
"docs": [
{
"_id": 4
},
{
"_id": 3
}
]
}
返回:
{
"docs": [
{
"_index": "zq_test",
"_type": "_doc",
"_id": "4",
"_version": 3,
"_seq_no": 4,
"_primary_term": 1,
"found": true,
"_source": {
"name": "jerry",
"age": 500,
"gender": "man"
}
},
{
"_index": "zq_test",
"_type": "_doc",
"_id": "3",
"_version": 1,
"_seq_no": 5,
"_primary_term": 1,
"found": true,
"_source": {
"name": "ceshi01",
"age": 35,
"gender": "man"
}
}
]
}
# 二
GET http://localhost:9200/zq_test/_doc/_mget
# json 中指明 _id, 鏈接中指明 index 和 _doc
{
"ids": [3, 4]
}
返回:
{
"docs": [
{
"_index": "zq_test",
"_type": "_doc",
"_id": "3",
"_version": 1,
"_seq_no": 5,
"_primary_term": 1,
"found": true,
"_source": {
"name": "ceshi01",
"age": 35,
"gender": "man"
}
},
{
"_index": "zq_test",
"_type": "_doc",
"_id": "4",
"_version": 3,
"_seq_no": 4,
"_primary_term": 1,
"found": true,
"_source": {
"name": "jerry",
"age": 500,
"gender": "man"
}
}
]
}
8.批處理操作
ES允許我們在一次請求中做多種操作,比如在一次請求中同時進行增刪改查操作:
批處理操作:
POST http://localhost:9200/_bulk # 下邊進行了一個批處理操作,分別包含添加、刪除、更新操作,
但是提交的參數必須是一行一行的
json,且結尾必須有換行符,要這樣ES才好處理,因為不是標准的json格式,這里PostMan識別json格式錯誤,但是問題不大。
json body:
{"index": {"_index": "zq_test", "_id": 5}}
{"name": "bulk", "age": 16, "gender": "woman"}
{"delete": {"_id": 5, "_index": "zq_test"}}
{"update": {"_id": 4, "_index": "zq_test"}}
{"doc": {"name": "jiuge", "age": 333, "gender": "sex"}}
# 注意,上面的 json body 必須每行都有換行符,即要多一個空行。另外已經刪除的 5 不能 update
返回:
{
"took": 4,
"errors": false,
"items": [
{
"index": {
"_index": "zq_test",
"_type": "_doc",
"_id": "5",
"_version": 16,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 29,
"_primary_term": 1,
"status": 201
}
},
{
"delete": {
"_index": "zq_test",
"_type": "_doc",
"_id": "5",
"_version": 17,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 30,
"_primary_term": 1,
"status": 200
}
},
{
"update": {
"_index": "zq_test",
"_type": "_doc",
"_id": "4",
"_version": 7,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 31,
"_primary_term": 1,
"status": 200
}
}
]
}
9.重建索引
重新建立索引,相當於拷貝一份.
POST http://localhost:9200/_reindex
json body:
{
"source": {
"index": "zq_test"
},
"dest":{
"index": "zq2"
}
}
返回:
{
"took": 184,
"timed_out": false,
"total": 2,
"updated": 0,
"created": 2,
"deleted": 0,
"batches": 1,
"version_conflicts": 0,
"noops": 0,
"retries": {
"bulk": 0,
"search": 0
},
"throttled_millis": 0,
"requests_per_second": -1.0,
"throttled_until_millis": 0,
"failures": []
}
2.查詢操作
1.多索引和多類型查詢
# 1 查詢所有索引下所有類型的數據,查詢條件為 name=jiuge
GET http://localhost:9200/_search?q=name:jiuge # 可以不帶 _all
# 2 獲取所有索引下類型為 _doc 的 數據,查詢條件為 name = jiuge
GET http://localhost:9200/_all/_doc/_search?q=name:jiuge
# 3 獲取zq_test索引和 zq2 索引下類型為_doc 的數據,查詢條件下為 name=jiuge
GET http://localhost:9200/zq_test,zq2/_doc/_search?q=name:jiuge
# *4 獲取zq_test 索引下類型為 teacher和student的數據,查詢條件為 name=jiuge
GET http://localhost:9200/zq_test/teacher,student/_search?q=name:jiuge # 因為es7 及其之后,type默認為_doc, 且在 es8 后,type 將取消,因此此訪問方式僅作參考,不必使用。
返回:
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 16,
"successful": 16,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1.7917595,
"hits": [
{
"_index": "zq_test",
"_type": "_doc",
"_id": "4",
"_score": 1.7917595,
"_source": {
"name": "jiuge",
"age": 333,
"gender": "sex"
}
},
{
"_index": "zq2",
"_type": "_doc",
"_id": "4",
"_score": 0.6931471,
"_source": {
"name": "jiuge",
"age": 333,
"gender": "sex"
}
}
]
}
}
2. 查詢字符串查詢
通過 GET 訪問的方式,在 URL 后面添加查詢字符串的方式查詢。
GET http://localhost:9200/zq_test/_search?from=0&size=10&q=name:ceshi006 OR name:ceshi007&sort=age:asc&_source=name,age
說明:
- from=0&size=10 : 分頁獲取
- q=name:ceshi006 OR name:ceshi007: 表示查詢 name 為 ceshi006 或 ceshi007 的文檔。OR 可以換做 AND
- sort=age:asc:排序,表示按照 age 進行排序。asc 降序, desc 升序
- _source=name,age:投影,選擇返回的字段,多個字段用 , 隔開
返回:
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "zq_test",
"_type": "_doc",
"_id": "6",
"_score": null,
"_source": {
"name": "ceshi006",
"age": 66
},
"sort": [
66
]
},
{
"_index": "zq_test",
"_type": "_doc",
"_id": "7",
"_score": null,
"_source": {
"name": "ceshi007",
"age": 77
},
"sort": [
77
]
}
]
}
}
可使用插入文檔的方式放入多個數據供實驗. 更多查詢參數和語法[點此查看]([Search API | Elasticsearch Guide 7.13] | Elastic)
3. 請求體查詢
通過請求體進行查詢。
GET http://localhost:9200/zq_test/_search # 也通過 json body
json body:
{
"query":{
"match":{
"name": "ceshi006"
}
},
"sort": [
{
"age": "asc"
}
],
"size": 5,
"from": 0
}
返回:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "zq_test",
"_type": "_doc",
"_id": "6",
"_score": null,
"_source": {
"name": "ceshi006",
"age": 66,
"gender": "man"
},
"sort": [
66
]
}
]
}
}
更多操作參數[點此查看]([Request body search | Elasticsearch Guide 7.13] | Elastic)
