該系列上一篇文章《Elasticsearch必知必會的乾貨知識一:ES索引文檔的CRUD》 講瞭如何進行index的增刪改查,本篇則側重講解說明如何對index進行創建、更改、遷移、查詢配置信息等。 僅創建索引:PUT index PUT /index 添加欄位設置(mappings):PUT in ...
該系列上一篇文章《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