ES索引模板

在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);

        // 关闭RestClient
        restClient.close();

        return statusCode;
    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值