本文來說下Elasticsearch基本操作之文檔操作
文檔概述
文檔概述
- 在創(chuàng)建好索引的基礎(chǔ)上來創(chuàng)建文檔,并添加數(shù)據(jù)。
- 這里的文檔可以類比為關(guān)系型數(shù)據(jù)庫中的表數(shù)據(jù),添加的數(shù)據(jù)格式為 JSON 格式。
創(chuàng)建文檔示例
創(chuàng)建文檔(生成隨機id)
在 apifox 中,向 ES 服務(wù)器發(fā) POST 請求 :http://localhost:9200/person/_doc,請求體內(nèi)容為:
服務(wù)器響應(yīng)結(jié)果如下:
此處發(fā)送請求的方式必須為 POST,不能是 PUT ,否則會發(fā)生錯誤
服務(wù)器響應(yīng)結(jié)果解釋文章來源:http://www.zghlxwxcb.cn/news/detail-789205.html
{
"_index"【索引】: "person",
"_type"【 類型-文檔 】: "_doc",
"_id"【唯一標識】: "Qc8r54wBBLem2BEmnNus",
#可以類比為 MySQL 中的主鍵,隨機生成
"_version"【版本】: 1,
"result"【結(jié)果】: "created",#這里的 create 表示創(chuàng)建成功
"_shards"【分片】: {
"total"【分片 - 總數(shù)】: 2,
"successful"【分片 - 成功】: 1,
"failed"【分片 - 失敗】: 0
},
"_seq_no": 1,
"_primary_term": 1
}
- 注意:上面的數(shù)據(jù)創(chuàng)建后,由于沒有指定數(shù)據(jù)唯一性標識(ID),默認情況下,ES 服務(wù)器會隨機生成一個。
創(chuàng)建文檔(自定義唯一性標識)
在 apifox 中,向 ES 服務(wù)器發(fā) POST 請求 :http://localhost:9200/person/_doc/1,請求體內(nèi)容為:
{
"name":"李四",
"age":22,
"sex":"女"
}
服務(wù)器響應(yīng)結(jié)果如下:
查看文檔示例
根據(jù)主鍵查看文檔
查看文檔時,需要指明文檔的唯一性標識,類似于 MySQL 中數(shù)據(jù)的主鍵查詢。在 apifox 中,向 ES 服務(wù)器發(fā) GET 請求 :http://127.0.0.1:9200/person/_doc/ 1
查詢成功后,服務(wù)器響應(yīng)結(jié)果
服務(wù)器響應(yīng)結(jié)果解釋
{
"_index"【索引】: "person",
"_type"【文檔類型】: "_doc",
"_id"【唯一標識】: "1", #可以類比為 MySQL 中的主鍵
"_version"【版本】: 1,
"_seq_no": 4,
"_primary_term": 1,
"found"【查詢結(jié)果】: true, # true 表示查找到,false 表示未查找到
"_source"【文檔源信息】: {
"name": "李四",
"age": 22,
"sex": "女"
}
}
查看所有文檔
在 apifox 中,向 ES 服務(wù)器發(fā) GET 請求 :http://localhost:9200/person/ _search
查詢成功后,服務(wù)器響應(yīng)結(jié)果
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 5,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "person",
"_type": "_doc",
"_id": "QM_K5owBBLem2BEmAdux",
"_score": 1.0,
"_source": {
"name": "張三",
"age": 20,
"sex": "男"
}
},
{
"_index": "person",
"_type": "_doc",
"_id": "Qc8r54wBBLem2BEmnNus",
"_score": 1.0,
"_source": {
"name": "張三",
"age": 20,
"sex": "男"
}
},
{
"_index": "person",
"_type": "_doc",
"_id": "Qs9754wBBLem2BEm69tA",
"_score": 1.0,
"_source": {
"name": "張三",
"age": 20,
"sex": "男"
}
},
{
"_index": "person",
"_type": "_doc",
"_id": "Q89854wBBLem2BEmXds5",
"_score": 1.0,
"_source": {
"name": "李四",
"age": 22,
"sex": "女"
}
},
{
"_index": "person",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"name": "李四",
"age": 22,
"sex": "女"
}
}
]
}
}
修改文檔示例
全局修改文檔
和新增文檔一樣,輸入相同的 URL 地址請求,如果請求體變化,會將原有的數(shù)據(jù)內(nèi)容覆蓋。在 apifox 中,向 ES 服務(wù)器發(fā) POST 請求 :http://127.0.0.1:9200/person/_doc/1,請求體內(nèi)容為:
{
"name":"李四四",
"age":66,
"sex":"女"
}
修改成功后,服務(wù)器響應(yīng)結(jié)果
服務(wù)器響應(yīng)結(jié)果解釋
{
"_index": "person",
"_type": "_doc",
"_id": "1",
"_version"【版本】: 2,
"result"【結(jié)果】: "updated",# updated 表示數(shù)據(jù)被更新
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 5,
"_primary_term": 1
}
局部修改文檔
修改數(shù)據(jù)時,也可以只修改某一給條數(shù)據(jù)的局部信息。在 apifox 中,向 ES 服務(wù)器發(fā) POST 請求 :http://127.0.0.1:9200/person/_update/1,請求體內(nèi)容為:
修改成功后,服務(wù)器響應(yīng)結(jié)果
根據(jù)唯一性標識,查詢文檔數(shù)據(jù),文檔數(shù)據(jù)已經(jīng)更新
刪除文檔示例
根據(jù)文檔的唯一性標識刪除文檔
- 刪除一個文檔不會立即從磁盤上移除,它只是被標記成已刪除(邏輯刪除)。
在 apifox 中,向 ES 服務(wù)器發(fā) DELETE 請求 :http://127.0.0.1:9200/person/ _doc/1
刪除成功,服務(wù)器響應(yīng)結(jié)果
服務(wù)器響應(yīng)結(jié)果解釋
{
"_index": "person",
"_type": "_doc",
"_id": "1",
"_version"【版本】: 4, #對數(shù)據(jù)的操作,都會更新版本
"result"【結(jié)果】: "deleted", # deleted 表示數(shù)據(jù)被標記為刪除
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 7,
"_primary_term": 1
}
刪除后再查詢當前文檔信息
如果刪除一個并不存在的文檔,返回結(jié)果為 not_found
條件刪除文檔
一般刪除數(shù)據(jù)都是根據(jù)文檔的唯一性標識進行刪除,實際操作時,也可以根據(jù)條件對多條數(shù)據(jù)進行刪除。
- 首先分別增加多條數(shù)據(jù)
向 ES 服務(wù)器發(fā) POST 請求 :http://127.0.0.1:9200/person/ _delete_by_query ,請求體內(nèi)容為:
{
"query":{
"match":{
"name":"張三"
}
}
}
服務(wù)器響應(yīng)結(jié)果解釋
{
"took"【耗時】: 1426,
"timed_out" 【是否超時】: false,
"total" 【總數(shù)】: 4,
"deleted"【刪除數(shù)量】: 4,
"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": []
}
本文小結(jié)
本文記錄了Elasticsearch基本操作之文檔操作文章來源地址http://www.zghlxwxcb.cn/news/detail-789205.html
到了這里,關(guān)于Elasticsearch基本操作之文檔操作的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!