前面我們?cè)贓S的基本概念一文中講到了索引
,ES中索引
對(duì)應(yīng)的就是關(guān)系型數(shù)據(jù)庫(kù)中的庫(kù)
.本小節(jié)我們來(lái)講一下ES的有關(guān)索引的基本操作。以后的文章中我會(huì)用到了kibana
工具,沒(méi)有安裝的可以按我的另一篇文章,里面有介紹怎么 安裝,安裝完成后瀏覽器訪問(wèn) localhost:5601
左側(cè)側(cè)邊欄找到Dev Tools
,如下圖

一:查看所有的索引
GET /_cat/indices?v
es返回
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open .kibana_1 B1GfSkleTF6cj5R4-c41rg 1 0 2 0 9.5kb 9.5kb
yellow open megacorp zdXsIWZtRMubzHXsiGq4Cw 5 1 5 0 28.2kb 28.2kb
結(jié)果顯示里面已經(jīng)有2個(gè)索引了分別是
.kibana_1
,megacorp
這個(gè)是我之前的測(cè)試索引
二:創(chuàng)建索引
語(yǔ)法
PUT /{IndexName}?pretty
創(chuàng)建用戶(hù)索引,在kibana的Console中輸入命令
PUT /user?pretty
ES返回
#! Deprecation: the default number of shards will change from [5] to [1] in 7.0.0; if you wish to continue using the default of [5] shards, you must manage this on the create index request or with an index template
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "user"
}
翻譯:棄用:默認(rèn)的分片數(shù)將在7.0.0中從[5]更改為[1]; 如果您希望繼續(xù)使用默認(rèn)的[5]分片,則必須在創(chuàng)建索引請(qǐng)求或索引模板上進(jìn)行管理。
我這里使用的是ES6.6已經(jīng)是相對(duì)較新的版本了,這個(gè)問(wèn)題我們暫時(shí)忽略,此時(shí)你可以使用GET /_cat/indices?v
再次查看索引看看和之前有什么區(qū)別
三:刪除索引
DELETE /{IndexName}?pretty
四:重索引和別名
ES不能重命名索引名稱(chēng),但是可以給索引取別名和重索引(reindex)
取別名語(yǔ)法
PUT /{OldIndexName}/_alias/{NewIndexName}
測(cè)試
1.往user索引插入文檔
PUT /user/introduce/1?pretty
{
"name" : "jack",
"age" : 20,
"gender" : "male"
}
2.取別名
PUT /user/_alias/users
3.查看索引數(shù)據(jù)
查users索引:GET /users/_search
查user索引:GET /user/_search
2次返回的結(jié)果都是一樣的
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [
{
"_index" : "user",
"_type" : "introduce",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "jack",
"age" : 20,
"gender" : "male"
}
}
]
}
}
沖索引等于數(shù)據(jù)遷移,本節(jié)就不講解了