Elasticsearch:为满足特定条件的模板创建别名

Elasticsearch: Create alias for template satisfying particular condition

是否可以在定义索引模板时定义不同的别名。不同的别名将引用满足特定条件的索引名称。

示例:

{
   "index_patterns":[
      "*employee_records*"
   ],
   "settings":{
      "number_of_shards":3
   },
   "mappings":{
      "_doc":{
         "dynamic_templates":[
            // Dynamic Mapping
         ]
      }
   },
   "aliases":{
      "employee_records":{}
   }
}

假设我们创建了多个索引,例如

在定义索引模板时,none 个索引存在。映射将保持不变。我想为上述场景定义两个别名 science_dept_employee_records & maths_dept_employee_records.

我能想到的一种方法是复制索引模板,这样

就有一个别名
 "index_patterns":[
      "maths_dept_employee_records*"
 ]

的另一个别名
 "index_patterns":[
      "science_dept_employee_records*"
 ]

然而,这会在多个模板中引入大量重复项。有没有更简洁的方法来实现这个?

实现此目的的一种方法是使用 multiple templates matching 并按如下方式排序。

一个模板定义了所有映射和设置并具有最低顺序(最先应用):

{
   "index_patterns":[
      "*employee_records*"
   ],
   "order": 1,
   "settings":{
      "number_of_shards":3
   },
   "mappings":{
      "_doc":{
         "dynamic_templates":[
            // Dynamic Mapping
         ]
      }
   }
}

另一个模板为更高阶的 maths_dept_employee 索引定义了别名,并在接下来应用:

{
   "index_patterns":[
      "maths_dept_employee_records*"
   ],
   "order": 2,
   "aliases":{
      "maths_dept_employee_records":{}
   }
}

science_dept_employee 索引也是如此:

{
   "index_patterns":[
      "science_dept_employee_records*"
   ],
   "order": 2,
   "aliases":{
      "science_dept_employee_records":{}
   }
}