创建模板(模板名和索引名一样都不能有大写)
PUT http://222.108.x.x:9200/_template/templateds
{ "template": "dsideal*", "order": 0, "settings": { "number_of_shards": 5 }, "aliases": { "dsideal-query": {} #起个别名 }, "mappings": { "doc": { "properties": { "person_name": { "type": "keyword" }, "gender_id": { "type": "long" }, "bureau_id": { "type": "long" } } } }}
写一些数据
POST http://222.108.x.x:9200/dsideal10/doc/1
{ "person_name": "张三", "gender_id": 1, "bureau_id": 2}
POST http://222.108.x.x:9200/dsideal11/doc/2
{ "person_name": "李四", "gender_id": 2, "bureau_id": 2}
会按照模板自动生成两个索引“dsideal10”和“dsideal11”
可以利用在创建模板时起的别名进行查询
POST http://222.108.x.x:9200/dsideal-query/_search
{ "size": 10, "query": { "bool": { "must": [ { "term": { "bureau_id": "2" } } ] } }}
创建多个索引别名【备忘】
POST http://222.108.x.x:9200/_aliases
{ "actions" : [ { "add" : { "index" : "dsideal1","alias" : "alias1" } }, { "add" : { "index" : "dsideal2","alias" : "alias1" } } ]}
在创建一个索引时可为这个索引根据条件创建多个别名
POST http://222.108.x.x:9200/test100
{ "aliases" : { "2014" : { "filter" : { "term" : {"year": 2014 } } }, "2015" : { "filter" : { "term" : {"year": 2015 } } }, "2016" : { "filter" : { "term" : {"year": 2016 } } } }, "mappings": { "doc": { "properties": { "person_name": { "type": "keyword" }, "year": { "type": "long" }, "bureau_id": { "type": "long" } } } }}
说明:当插入year=2015的数据时可用2015这个别名去查询
测试插入一些数据
{"person_name":"张三","year":2014,"bureau_id":2},{ "person_name":"李四","year":2015,"bureau_id":3},{ "person_name":"王五","year":2015,"bureau_id":3},{ "person_name":"赵六","year":2016,"bureau_id":4}
post http://222.108.x.x:9200/2015/_search
{ "size": 10, "query": { "bool": { "must": [ { "match_all": { } } ] } }}
就只返回year=2015的数据