Elasticsearch 2.20入门篇:基本操作
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
????前面我們已經(jīng)安裝了Elasticsearch ,下一步我們要對(duì)Elasticsearch進(jìn)行一些基本的操作。基本的操作主要有,建索引庫(kù),插入數(shù)據(jù),查詢數(shù)據(jù),修改數(shù)據(jù),刪除數(shù)據(jù),刪除索引庫(kù)。
????備注:如果沒有特殊說明,本文章及后面所有的文章都在2.20版本中進(jìn)行驗(yàn)證,其他版本不能確定是否可用。
????由于官方文檔都是使用curl來進(jìn)行實(shí)例操作,不太直觀,我更喜歡用圖形化界面來進(jìn)行驗(yàn)證。在本文及以后的例子中,我都是已RESTClient3.5來作為操作的工具。下載地址為http://code.fosshub.com/WizToolsorg-RESTClient/downloads,下載的文件是restclient-ui-3.5-jar-with-dependencies.jar。
程序運(yùn)行: java -jar restclient-ui-3.5-jar-with-dependencies.jar
建索引庫(kù)
執(zhí)行PUT localhost:9200/customer?pretty
返回表示建庫(kù)成功:
{"acknowledged"?:?true }說明:http方法PUT,url為localhost:9200/customer?pretty
查詢庫(kù)
執(zhí)行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?
表示已經(jīng)建成了一個(gè)索引customer,主分片是5個(gè),健康度是黃色,狀態(tài)是活動(dòng),文檔數(shù)為0。
插入數(shù)據(jù)
執(zhí)行?PUT localhost:9200/customer/external/1?pretty
參數(shù):{ ?"name": "John Doe" }
注意:Method選擇PUT,Body要設(shè)置成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 }界面如下:
我們?cè)俅螆?zhí)行庫(kù)查詢,發(fā)現(xiàn)文檔數(shù)是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?
查詢數(shù)據(jù)
執(zhí)行:GET?http://localhost:9200/customer/external/1?pretty
返回:
{"_index"?:?"customer","_type"?:?"external","_id"?:?"1","_version"?:?1,"found"?:?true,"_source"?:?{"name"?:?"John?Doe"} }可以看到_source的內(nèi)容就是我們剛才插入的數(shù)據(jù)。
本文由賽克藍(lán)德(secisland)原創(chuàng),轉(zhuǎn)載請(qǐng)標(biāo)明作者和出處。
修改數(shù)據(jù)
執(zhí)行:POST localhost:9200/customer/external/1/_update?pretty
參數(shù):
返回結(jié)果:
{"_index"?:?"customer","_type"?:?"external","_id"?:?"1","_version"?:?2,"_shards"?:?{"total"?:?2,"successful"?:?1,"failed"?:?0} }表示執(zhí)行成功。
然后我們?cè)诓樵円幌聰?shù)據(jù)
GET http://localhost:9200/customer/external/1?pretty
{"_index"?:?"customer","_type"?:?"external","_id"?:?"1","_version"?:?2,"found"?:?true,"_source"?:?{"name"?:?"secisland?Doe"} }可以看出文檔的內(nèi)容由John Doe修改成了secisland?Doe。
刪除文檔
執(zhí)行:DELETE localhost:9200/customer/external/1?pretty
返回:
{"found"?:?true,"_index"?:?"customer","_type"?:?"external","_id"?:?"1","_version"?:?3,"_shards"?:?{"total"?:?2,"successful"?:?1,"failed"?:?0} }然后我們查詢庫(kù)的狀態(tài):
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?
從中可以看出,數(shù)據(jù)庫(kù)中已經(jīng)沒有記錄了。
刪除索引庫(kù)
執(zhí)行:DELETE localhost:9200/customer?pretty
返回:
{"acknowledged"?:?true }表示刪除成功
然后我們查詢庫(kù)的狀態(tài):
GET http://localhost:9200/_cat/indices?v
返回:
health status index pri rep docs.count docs.deleted store.size pri.store.size?
從中可以看出已經(jīng)沒有任何的庫(kù)了。
賽克藍(lán)德(secisland)后續(xù)會(huì)逐步對(duì)Elasticsearch的最新版本的各項(xiàng)功能進(jìn)行分析,近請(qǐng)期待。也歡迎加入secisland公眾號(hào)進(jìn)行關(guān)注。
轉(zhuǎn)載于:https://my.oschina.net/secisland/blog/613927
總結(jié)
以上是生活随笔為你收集整理的Elasticsearch 2.20入门篇:基本操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 作为前端开发兼任产品专员是一种咋样的体验
- 下一篇: R:在Ubuntu14.04 安装R