創建模板(模板名和索引名一樣都不能有大寫)
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的數據