该系列上一篇文章《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
No comments:
Post a Comment