日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Elasticsearch 2.20入门篇:基本操作

發布時間:2025/7/14 53 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Elasticsearch 2.20入门篇:基本操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

????前面我們已經安裝了Elasticsearch ,下一步我們要對Elasticsearch進行一些基本的操作。基本的操作主要有,建索引庫,插入數據,查詢數據,修改數據,刪除數據,刪除索引庫。

????備注:如果沒有特殊說明,本文章及后面所有的文章都在2.20版本中進行驗證,其他版本不能確定是否可用。

????由于官方文檔都是使用curl來進行實例操作,不太直觀,我更喜歡用圖形化界面來進行驗證。在本文及以后的例子中,我都是已RESTClient3.5來作為操作的工具。下載地址為http://code.fosshub.com/WizToolsorg-RESTClient/downloads,下載的文件是restclient-ui-3.5-jar-with-dependencies.jar。

程序運行: java -jar restclient-ui-3.5-jar-with-dependencies.jar

建索引庫

執行PUT localhost:9200/customer?pretty

返回表示建庫成功:

{"acknowledged"?:?true }

說明:http方法PUT,url為localhost:9200/customer?pretty

查詢庫

執行GET http://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 ? ? ? ? ? ? ? ? ? 795b ? ? ? ?795b?

表示已經建成了一個索引customer,主分片是5個,健康度是黃色,狀態是活動,文檔數為0。

插入數據

執行?PUT localhost:9200/customer/external/1?pretty

參數:{ ?"name": "John Doe" }

注意:Method選擇PUT,Body要設置成application/x-www-form-urlencoded; charset=UTF-8

返回值為:

{"_index"?:?"customer","_type"?:?"external","_id"?:?"1","_version"?:?1,"_shards"?:?{"total"?:?2,"successful"?:?1,"failed"?:?0},"created"?:?true }

界面如下:

我們再次執行庫查詢,發現文檔數是1:GET http://localhost:9200/_cat/indices?v ?

health status ?index ? ? ?pri rep ? ? ? ?docs.count docs.deleted store.size pri.store.size?

yellow open ? customer ? 5 ? 1 ? ? ? ? ?1 ? ? ? ? ? ?0 ? ? ?3.5kb ? ? ? ? ?3.5kb?

查詢數據

執行:GET?http://localhost:9200/customer/external/1?pretty

返回:

{"_index"?:?"customer","_type"?:?"external","_id"?:?"1","_version"?:?1,"found"?:?true,"_source"?:?{"name"?:?"John?Doe"} }

可以看到_source的內容就是我們剛才插入的數據。

本文由賽克藍德(secisland)原創,轉載請標明作者和出處。

修改數據

執行:POST localhost:9200/customer/external/1/_update?pretty

參數:

{"doc":?{?"name":?"secisland?Doe"?} }

返回結果:

{"_index"?:?"customer","_type"?:?"external","_id"?:?"1","_version"?:?2,"_shards"?:?{"total"?:?2,"successful"?:?1,"failed"?:?0} }

表示執行成功。

然后我們在查詢一下數據

GET http://localhost:9200/customer/external/1?pretty

{"_index"?:?"customer","_type"?:?"external","_id"?:?"1","_version"?:?2,"found"?:?true,"_source"?:?{"name"?:?"secisland?Doe"} }

可以看出文檔的內容由John Doe修改成了secisland?Doe。

刪除文檔

執行:DELETE localhost:9200/customer/external/1?pretty

返回:

{"found"?:?true,"_index"?:?"customer","_type"?:?"external","_id"?:?"1","_version"?:?3,"_shards"?:?{"total"?:?2,"successful"?:?1,"failed"?:?0} }

然后我們查詢庫的狀態:

GET http://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 ? ? ?3.6kb ? ? ? ? ?3.6kb?

從中可以看出,數據庫中已經沒有記錄了。

刪除索引庫

執行:DELETE localhost:9200/customer?pretty

返回:

{"acknowledged"?:?true }

表示刪除成功

然后我們查詢庫的狀態:

GET http://localhost:9200/_cat/indices?v

返回:

health status index pri rep docs.count docs.deleted store.size pri.store.size?

從中可以看出已經沒有任何的庫了。

賽克藍德(secisland)后續會逐步對Elasticsearch的最新版本的各項功能進行分析,近請期待。也歡迎加入secisland公眾號進行關注。

轉載于:https://my.oschina.net/secisland/blog/613927

總結

以上是生活随笔為你收集整理的Elasticsearch 2.20入门篇:基本操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。