1. 創建索引
# 語法
PUT /索引名/[型別名]/檔案id
{
請求體
}
可以通過 postman 發送請求,也可以通過 kibana 發送請求,由于 kibana 有提示,所以我們選擇kibana
索引名不能有大寫字母
PUT Book
{
"error" : {
"root_cause" : [
{
"type" : "invalid_index_name_exception",
"reason" : "Invalid index name [Book], must be lowercase",
"index_uuid" : "_na_",
"index" : "Book"
}
],
"type" : "invalid_index_name_exception",
"reason" : "Invalid index name [Book], must be lowercase",
"index_uuid" : "_na_",
"index" : "Book"
},
"status" : 400
}
索引名是唯一的,不能重復,重復創建會出錯
{
"error" : {
"root_cause" : [
{
"type" : "resource_already_exists_exception",
"reason" : "index [book/AlxnoOUPSPK_HBVPe1Qkwg] already exists",
"index_uuid" : "AlxnoOUPSPK_HBVPe1Qkwg",
"index" : "book"
}
],
"type" : "resource_already_exists_exception",
"reason" : "index [book/AlxnoOUPSPK_HBVPe1Qkwg] already exists",
"index_uuid" : "AlxnoOUPSPK_HBVPe1Qkwg",
"index" : "book"
},
"status" : 400
}
創建索引請求,什么都不指定
PUT book
創建索引請求,指定欄位的型別,創建規則
PUT book
{
"mappings": {
"properties":{
"name": {
"type": "text"
},
"author": {
"type": "text"
},
"age": {
"type": "long"
},
"birthday" :{
"type": "date"
}
}
}
}
索引創建成功后,可以向索引中寫入檔案
# 如果自己的檔案欄位沒有指定,那么es就會給我們默認配置欄位型別
PUT book/_doc/1
{
"name": "雪中悍刀行",
"author": "烽火戲諸侯",
"age": 30,
"birthday": "2012-10-10"
}
PUT book/_doc/2
{
"name": "徐曉",
"author": "烽火戲諸侯",
"age": 50,
"birthday": "1970-01-11"
}
默認情況下,索引是具備讀寫權限的,當然這個讀寫權限可以關閉,
例如,關閉索引的寫權限:
PUT book/_settings
{
"blocks.write": true
}
關閉之后,就無法添加檔案了,關閉了寫權限之后,如果想要再次打開,方式如下:
PUT book/_settings
{
"blocks.write": false
}
其他類似的權限有:
- blocks.write
- blocks.read
- blocks.read_only
2. 查看索引
請求查看方式,可以通過 GET 請求獲取具體的資訊
GET book
GET book/_settings
也可以同時查看多個索引資訊
GET book,test/_settings
也可以查看所有索引資訊
GET _all/_settings
獲取索引的健康值
GET _cat/health
獲取版本資訊
GET _cat/indices?v
3. 修改
索引創建好之后,可以修改其屬性
例如修改索引的副本數
PUT book/_settings
{
"number_of_replicas": 2
}
使用put覆寫修改(版本號會增加)基本已不再采用
PUT /test3/_doc/1
{
"name": "大將軍",
"age": 45,
"birthday": "1970-01-11"
}
使用post修改,doc是固定格式,本例只修改name欄位
POST /test3/_doc/1/_update
{
"doc":{
"name": "北涼王"
}
}
4. 洗掉索引
通過DELETE命令實作洗掉,根據你的請求判斷是洗掉索引還是洗掉檔案記錄
DELETE book
洗掉一個不存在的索引會報錯,
5. 索引打開/關閉
關閉索引:
一個關閉的索引幾乎不占用系統資源,如果一個索引暫時不用,可以關閉它
POST book/_close
打開索引
POST book/_open
當然,可以同時關閉/打開多個索引,多個索參考 , 隔開,或者直接使用 _all 代表所有索引,
6. 復制索引
索引復制,只會復制資料,不會復制索引配置(新復制的索引book_new是1個分片1個副本,而book索引是1個分片2個副本,并沒有把索引配置復制過去)
POST _reindex
{
"source": {"index":"book"},
"dest": {"index":"book_new"}
}
復制的時候,可以添加查詢條件,
7. 索引別名
可以為索引創建別名,如果這個別名是唯一的,該別名可以代替索引名稱,
POST /_aliases
{
"actions": [
{
"add": {
"index": "book",
"alias": "book_alias"
}
}
]
}
將 add 改為 remove 就表示移除別名:
POST /_aliases
{
"actions": [
{
"remove": {
"index": "book",
"alias": "book_alias"
}
}
]
}
可以添加多個別名,也可以洗掉多個別名
查看某一個索引的別名:
GET /book/_alias
查看某一個別名對應的索引(book_alias 表示一個別名):
GET /book_alias/_alias
可以查看集群上所有可用別名:
GET /_alias
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/339068.html
標籤:其他
上一篇:一名合格的Java后端工程師或架構師必須要掌握 Spring Framework、Spring Boot、Spring Cloud
