該系列上一篇文章《Elasticsearch必知必會的乾貨知識一:ES索引文件的CRUD》 講了如何進行index的增刪改查,本篇則側重講解說明如何對index進行建立、更改、遷移、查詢配置資訊等。
-
僅建立索引:PUT index
PUT /index
-
新增欄位設定(mappings):PUT index/_mapping/type,如:(properties下均為索引欄位)
PUT /index/_mapping/_doc { "properties": { "email": { "type": "keyword" } } }
-
新增索引設定(settings):PUT /index/_settings,如:
PUT /index/_settings { "number_of_shards":"8", "max_inner_result_window":"130000", "max_result_window":"130000", "analysis":{ "analyzer":{ "ngram_analyzer":{ "tokenizer":"ngram_tokenizer" } }, "tokenizer":{ "ngram_tokenizer":{ "token_chars":[ "letter", "digit", "punctuation" ], "type":"ngram", "max_gram":"1" } } } }
-
新增別名或刪除別名:POST /_aliases 或 PUT /index/_alias/name ,如:(remove表示刪除別名,add表示新增別名)
POST /_aliases
{
"actions" : [
{ "remove" : { "index" : "test1", "alias" : "alias1" } },
{ "add" : { "index" : "test2", "alias" : "alias1" } }
]
}
PUT /index/_alias/name exp:/index/_alias/index100
-
一次性建立包含完整的別名(alias)、欄位(mappings)、設定(settings)的索引,如:
PUT /index_name { "index_name":{ "aliases":{ "index_alias_name":{ } }, "mappings":{ "index_type":{ "dynamic":"false", "properties":{ "doubleField1":{ "type":"double", "null_value":0 }, "keywordField2":{ "type":"keyword", "null_value":"" }, "longField3":{ "type":"long", "null_value":0 }, "textField4":{ "type":"text", "fields":{ "raw":{ "type":"keyword", "null_value":"" } }, "analyzer":"ngram_analyzer" }, "dateField5":{ "type":"date", "null_value":"-62167420800000", "format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" } } } }, "settings":{ "number_of_shards":"3", "number_of_replicas":"2", "max_inner_result_window":"130000", "max_result_window":"130000", "analysis":{ "analyzer":{ "ngram_analyzer":{ "tokenizer":"ngram_tokenizer" } }, "tokenizer":{ "ngram_tokenizer":{ "token_chars":[ "letter", "digit", "punctuation" ], "type":"ngram", "max_gram":"1" } } } } } }
-
index自動滾動【Rollover】,即:當索引達到預設的滾動條件時,會自動建立新的索引(index),並將別名(alias)指向最新的索引,原索引將被自動刪除對映,如:(先建立索引並指名索引別名,然後執行_rollover API 且設定滾動的條件值,最後正常的插入文件數,當達到滾動條件後,則會自動觸發index _rollover)
POST index_alias_name/_rollover/ { "conditions": { "max_age": "7d", //設定:最大時間7天 "max_docs": 10000,//設定:最大文件記錄數 "max_size": "5gb" //設定:索引最大容量 } } //Response: { "old_index": "舊索引名", "new_index": "新索引名", "rolled_over": true, "dry_run": false, "acknowledged": true, "shards_acknowledged": true, "conditions": { "[max_docs: 10000]": true, "[max_age: 7d]": false, "[max_size: 5gb]": false } } //POST index_alias_name/_doc -d {index文件JSON} 插入10000以上的文件記錄
-
重建索引(_reindex),即:一旦索引被建立,則無法直接修改索引欄位的mapping屬性,必需要重建索引然後將舊的索引資料遷移到新的索引中才行(遷移過程底層使用了scroll API ),如:
POST _reindex { "conflicts": "proceed",//發生衝突繼續執行 "source": { "index": "old_index", "type": "_doc", "size": 5000, //設定每批遷移的文件記錄數 "_source": ["user", "_doc"], //可設定要遷移的索引欄位,不設定則預設所有欄位 "query": { //可設定要遷移的文件記錄過濾條件 "match_all": { } } }, "dest": { "index": "new_index", "type": "_doc", "version_type": "internal" //"internal"或者不設定,則Elasticsearch強制性的將文件轉儲到目標中,覆蓋具有相同型別和ID的任何內容 } }
其它說明可參見:ES資料庫重建索引——Reindex(資料遷移)
-
查詢index的完整定義資訊:GET /index ,如:
GET /index
-
只查詢mapping欄位資訊:GET /index/_mapping (帶s也可以)
GET /index/_mapping OR /index/_mappings OR /index/_mapping/_doc【如有多個type可以加上指定type】
-
只查詢settings資訊:GET /index/_settings
GET /index/_settings