ES 學習之路-indices APIs(1)
ES 學習之indices APIs
1.關閉萬用字元,_all等操作
在index api 中可以使用_all或者逗號操作符,或者是萬用字元*,但是這些操作可能會導致誤操作,所有我們可以通過在配置檔案中配置如下屬性,這樣api中將不能使用像_all這樣的操作
action.destructive_requires_name=true
2.判斷index是已經存在
可以通過api的方式判斷一個index是否存在,如果index存在將會api呼叫將會返回200,如果不存在將會返回404,但是如果有別名和你查詢的名稱相同的時候,也會返回200,所以說這個api判斷是index或者是別名是否存在。遺憾的是我在elasticsearch-head中呼叫這個api的時候,一直提示正在請求…,沒有任何結果
HEAD /index_name
3.關閉/開啟index
索引可以被關閉/開啟, 一個關閉的索引的在叢集中基本不需要什麼開銷,除了維護其元資料需要的開銷,關閉的index後不能進行讀寫操作;這個API可以同時開啟或者是關閉多個index,但是預設情況下, 如果遇到index不存在,將會丟擲異常,我們可以通過在配置檔案中配置ignore_unavailable=true方式忽略這樣的問題,下邊是開啟、關閉api樣例:
POST /my_index/_close
POST /my_index/_open
ignore_unavailable=true
關閉index可能會消耗大量的磁碟空間,同時引發一下別的問題,elasticsearch官方描述是如下:
Closed indices consume a significant amount of disk-space which can cause problems in managed environments. Closing indices can be disabled via the cluster settings API by setting
cluster.indices.close.enable
tofalse
. The default istrue
.
可以通過通過配置cluster.indices.close.enable=false的方式是index不可被關閉
cluster.indices.close.enable=false
4.等待分片操作
Because opening an index allocates its shards, the
wait_for_active_shards
setting on index creation applies to the index opening action as well. The default value for thewait_for_active_shards
setting on the open index API is 0, which means that the command won’t wait for the shards to be allocated.
5.壓縮index(shrink index)
我們知道一個index的shards是不可以調整的, 但是我們可以通過shrink index api將一個index壓縮到一個新的index中, 但是這個api有很多要求:
- the target index must not exist
- The index must have more primary shards than the target index.
- The number of primary shards in the target index must be a factor of the number of primary shards in the source index. The source index must have more primary shards than the target index.
- The index must not contain more than
2,147,483,519
documents in total across all shards that will be shrunk into a single shard on the target index as this is the maximum number of docs that can fit into a single shard.- The node handling the shrink process must have sufficient free disk space to accommodate a second copy of the existing index.
然後我們看看壓縮操作過程
- First, it creates a new target index with the same definition as the source index, but with a smaller number of primary shards.
- Then it hard-links segments from the source index into the target index. (If the file system doesn’t support hard-linking, then all segments are copied into the new index, which is a much more time consuming process.)
- Finally, it recovers the target index as though it were a closed index which had just been re-opened.
要執行要是, 源index必須是隻讀的,而且健康值是green
In order to shrink an index, the index must be marked as read-only, and a (primary or replica) copy of every shard in the index must be relocated to the same node and have health
green
.
執行樣例:
POST my_source_index/_shrink/my_target_index
{
"settings": {
"index.number_of_replicas": 1,
"index.number_of_shards": 1,
"index.codec": "best_compression"
},
"aliases": {
"my_search_indices": {}
}
}
6.rollover index(翻新index)
這個API可以從一個別名對應的index中翻新出一個新的index, 當你認為原來的index太舊或者是資料量太大時。這個api需要接收一個別名,還有一些判斷條件,而且這個別名必須只對應一個index,當滿足這些判斷條件的時候, 新的index在執行這個API會被建立
PUT /logs-000001
{
"aliases": {
"logs_write": {}
}
}
# Add > 1000 documents to logs-000001
如果沒有自己定義新index的名稱, elasticsearch會給你定義一個
POST /logs_write/_rollover
{
"conditions": {
"max_age": "7d",
"max_docs": 1000,
"max_size": "5gb"
}
}
這個api呼叫時自己給出新index的名稱
POST /my_alias/_rollover/my_new_index_name
{
"conditions": {
"max_age": "7d",
"max_docs": 1000,
"max_size": "5gb"
}
}
引數中不只是可以接收判斷條件,同時還可以接收新index的一些設定,mappings,alias等等, 就像在建立create index的api一樣
POST /logs_write/_rollover
{
"conditions" : {
"max_age": "7d",
"max_docs": 1000,
"max_size": "5gb"
},
"settings": {
"index.number_of_shards": 2
}
}
實際樣例:
POST http://192.168.199.103:9200/log_write/_rollover/log-1/
{
"conditions": {
"max_docs": 100
},
"settings": {
"number_of_shards": 1,
"number_of_replicas": 2
},
"mappings": {
"log": {
"properties": {
"name": {
"type": "text",
"index": true
},
"message": {
"type": "text",
"index": true
}
}
}
},
"aliases": {
"log-1-write": {}
}
}
{
"old_index": "logs",
"new_index": "log-1",
"rolled_over": true,
"dry_run": false,
"acknowledged": true,
"shards_acknowledged": true,
"conditions": {
"[max_docs: 100]": true
}
}
需要注意的是,如果在引數中不指定這些的話,那麼創建出來的新的index將什麼資料都不包含,mapping也是空的
7.PUT Mapping
put mapping API 允許我們給一個已經存在的index新增新的fields或者是改變現有欄位的搜尋模式。樣例:
PUT twitter
{}
建立一個什麼都沒有的index,命名為twitter
PUT twitter/_mapping/_doc
{
"properties": {
"email": {
"type": "keyword"
}
}
}
給twitter index 新增一個型別為_doc,同時給_doc新增一個屬性email,型別為keyword
put mapping 可以支援多index(multi-index)引數, 也就是同時調整多個index的mapping,樣例:
# Create the two indices
PUT twitter-1
PUT twitter-2
# Update both mappings
PUT /twitter-1,twitter-2/_mapping/_doc
{
"properties": {
"user_name": {
"type": "text"
}
}
}
實際上在elasticsearch中通過put mapping只能給現有的屬性進行補充, 不能對現有的屬性進行更改,否則會丟擲異常,例如:
{
"error": {
"root_cause": [
{
"type": "illegal_argument_exception",
"reason": "mapper [name] of different type, current_type [text], merged_type [long]"
}
],
"type": "illegal_argument_exception",
"reason": "mapper [name] of different type, current_type [text], merged_type [long]"
},
"status": 400
}
這是更新一個field 為name的型別, 原來為text,改為long,結果執行api的時候丟擲以上異常。
8.GET Mapping
get mapping api可以查詢一個或多個index的mapping資訊, 語法如下:
host:port/{index}/_mapping/{type}
樣例:查詢所有index中type為log的mapping,實際上沒有index都只有一個type
GET /_all/_mapping/log
結果:
{
"log-1": {
"mappings": {
"log": {
"properties": {
"email": {
"type": "text"
},
"name": {
"type": "text"
}
}
}
}
}
}
9.GET Field Mapping
get field mapping API可以用來查詢index中指定的一個或多個field資訊, 而不是一次性查詢一個index或多個index的所有mapping資訊
建立一個publications index
PUT publications
{
"mappings": {
"_doc": {
"properties": {
"id": { "type": "text" },
"title": { "type": "text"},
"abstract": { "type": "text"},
"author": {
"properties": {
"id": { "type": "text" },
"name": { "type": "text" }
}
}
}
}
}
}
獲取publications index 中_doc type 的title field 資訊, 同時顯示返回結果
GET publications/_mapping/_doc/field/title
{
"publications": {
"mappings": {
"_doc": {
"title": {
"full_name": "title",
"mapping": {
"title": {
"type": "text"
}
}
}
}
}
}
}
get field mapping API 支援一次查詢多個field或者是查詢多個index的多個field
,它支援逗號運算子,_all, *號匹配等
如:
GET /twitter,kimchy/_mapping/field/message
GET /_all/_mapping/_doc,tweet,book/field/message,user.id
GET /_all/_mapping/_do*/field/*.id
GET publications/_mapping/_doc/field/a*