在Elasticsearch中,索引模板(Index Templates)是用來預定義新創建索引的設置和映射的一種機制。當你創建了一個索引模板,它會包含一系列的默認設置和映射規則,這些規則會在滿足一定條件的新索引被創建時自動應用。
索引模板通過index_patterns
字段來指定模板適用的索引名稱模式。當一個新的索引被創建,Elasticsearch會查找是否有任何模板的index_patterns
與該索引名稱匹配。如果有匹配的模板,那么該模板的設置和映射將被應用到新創建的索引上。
因此,如果你創建了一個名為content_erp_nlp_help_online
的索引模板,并且在其中定義了index_patterns
為["content_erp_nlp_help_online"]
,那么當你嘗試創建一個確切名稱為content_erp_nlp_help_online
的索引時,該模板將會被應用,從而自動配置索引的設置和映射。
但是,需要注意的是,如果在創建索引時顯式指定了某些設置或映射,那么這些顯式指定的值將優先于模板中的值。此外,一旦索引已經被創建,索引模板的更改將不會影響到已經存在的索引。
索引模板還可以通過通配符模式來匹配多個索引。例如,如果模板的index_patterns
為["content_*"]
,那么所有以content_
開頭的索引都會應用該模板。
總結來說,索引模板是一種策略,它允許你預設一組設置和映射,以便在創建符合特定命名模式的新索引時自動應用這些預設。這極大地簡化了管理大量索引的過程,尤其是當這些索引具有相似的特性時。
ES 8.14 新的創建模板的方法:
PUT /_index_template/content_erp_nlp_help
{"index_patterns": ["content_erp*"],"priority": 100,"template": {"settings": {"analysis": {"analyzer": {"my_ik_analyzer": {"type": "ik_smart"}}},"number_of_shards": 1},"mappings": {"properties": {"id": {"type": "long"},"content": {"type": "text","analyzer": "ik_max_word","search_analyzer": "ik_smart"},"content_vector": {"type": "dense_vector","similarity": "cosine","index": true,"dims": 768,"element_type": "float","index_options": {"type": "hnsw","m": 16,"ef_construction": 128}},"content_answer": {"type": "text","analyzer": "ik_max_word","search_analyzer": "ik_smart"},"title": {"type": "text","analyzer": "ik_max_word","search_analyzer": "ik_smart"},"param": {"type": "text","analyzer": "ik_max_word","search_analyzer": "ik_smart"},"type": {"type": "text","analyzer": "ik_max_word","search_analyzer": "ik_smart"},"questionId": {"type": "text","analyzer": "ik_max_word","search_analyzer": "ik_smart"},"createTime": {"type": "text","analyzer": "ik_max_word","search_analyzer": "ik_smart"},"updateTime": {"type": "text","analyzer": "ik_max_word","search_analyzer": "ik_smart"},"hitCount": {"type": "text","analyzer": "ik_max_word","search_analyzer": "ik_smart"},"answerPattern": {"type": "text","analyzer": "ik_max_word","search_analyzer": "ik_smart"},"nearQuestionVOList": {"type": "text","analyzer": "ik_max_word","search_analyzer": "ik_smart"},"questionEnclosureVOList": {"type": "text","analyzer": "ik_max_word","search_analyzer": "ik_smart"},"questionRelationVOList": {"type": "text","analyzer": "ik_max_word","search_analyzer": "ik_smart"},"rmsRoutingAnswerVos": {"type": "text","analyzer": "ik_max_word","search_analyzer": "ik_smart"}}}}
}
查詢模板:
GET /_index_template/*
GET /_index_template/content_erp_nlp_help
Java實現的代碼:
public int createIndexTemp(String indexTempName) throws Exception {// 創建RestClient實例RestClientBuilder builder = RestClient.builder(new HttpHost("127.0.0.1", 9200, "http"));RestClient restClient = builder.build();// 定義請求體String requestBody = "{\n" +" \"index_patterns\": [\"content_erp*\"],\n" +" \"priority\": 100,\n" +" \"template\": {\n" +" \"settings\": {\n" +" \"analysis\": {\n" +" \"analyzer\": {\n" +" \"my_ik_analyzer\": {\n" +" \"type\": \"ik_smart\"\n" +" }\n" +" }\n" +" },\n" +" \"number_of_shards\": 1\n" +" },\n" +" \"mappings\": {\n" +" \"properties\": {\n" +" \"id\": {\"type\": \"long\"},\n" +" \"content\": {\"type\": \"text\",\"analyzer\": \"ik_max_word\",\"search_analyzer\": \"ik_smart\"},\n" +" \"content_vector\": {\"type\": \"dense_vector\",\"similarity\": \"cosine\",\"index\": true,\"dims\": 768,\"element_type\": \"float\",\"index_options\": {\"type\": \"hnsw\",\"m\": 16,\"ef_construction\": 128}},\n" +" \"content_answer\": {\"type\": \"text\",\"analyzer\": \"ik_max_word\",\"search_analyzer\": \"ik_smart\"},\n" +" \"title\": {\"type\": \"text\",\"analyzer\": \"ik_max_word\",\"search_analyzer\": \"ik_smart\"},\n" +" \"param\": {\"type\": \"text\",\"analyzer\": \"ik_max_word\",\"search_analyzer\": \"ik_smart\"},\n" +" \"type\": {\"type\": \"text\",\"analyzer\": \"ik_max_word\",\"search_analyzer\": \"ik_smart\"},\n" +" \"questionId\": {\"type\": \"text\",\"analyzer\": \"ik_max_word\",\"search_analyzer\": \"ik_smart\"},\n" +" \"createTime\": {\"type\": \"text\",\"analyzer\": \"ik_max_word\",\"search_analyzer\": \"ik_smart\"},\n" +" \"updateTime\": {\"type\": \"text\",\"analyzer\": \"ik_max_word\",\"search_analyzer\": \"ik_smart\"},\n" +" \"hitCount\": {\"type\": \"text\",\"analyzer\": \"ik_max_word\",\"search_analyzer\": \"ik_smart\"},\n" +" \"answerPattern\": {\"type\": \"text\",\"analyzer\": \"ik_max_word\",\"search_analyzer\": \"ik_smart\"},\n" +" \"nearQuestionVOList\": {\"type\": \"text\",\"analyzer\": \"ik_max_word\",\"search_analyzer\": \"ik_smart\"},\n" +" \"questionEnclosureVOList\": {\"type\": \"text\",\"analyzer\": \"ik_max_word\",\"search_analyzer\": \"ik_smart\"},\n" +" \"questionRelationVOList\": {\"type\": \"text\",\"analyzer\": \"ik_max_word\",\"search_analyzer\": \"ik_smart\"},\n" +" \"rmsRoutingAnswerVos\": {\"type\": \"text\",\"analyzer\": \"ik_max_word\",\"search_analyzer\": \"ik_smart\"}\n" +" }\n" +" }\n" +" }\n" +"}";// 構建請求Request request = new Request("PUT", "/_index_template/" + indexTempName);request.setJsonEntity(requestBody);// 發送請求并獲取響應Response response = restClient.performRequest(request);// 處理響應int statusCode = response.getStatusLine().getStatusCode();System.out.println("Response status: " + statusCode);// 關閉RestClientrestClient.close();return statusCode;}