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. ES索引模板

    2024-07-11 05:24:06       21 阅读
  2. ES清理索引镜像

    2024-07-11 05:24:06       33 阅读
  3. es 倒排索引

    2024-07-11 05:24:06       41 阅读
  4. es索引同步

    2024-07-11 05:24:06       28 阅读
  5. 修改ES索引名称

    2024-07-11 05:24:06       21 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 05:24:06       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 05:24:06       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 05:24:06       58 阅读
  4. Python语言-面向对象

    2024-07-11 05:24:06       69 阅读

热门阅读

  1. ”极大似然估计“和”贝叶斯估计“思想对比

    2024-07-11 05:24:06       24 阅读
  2. 理解Gunicorn:Python WSGI服务器的基石

    2024-07-11 05:24:06       24 阅读
  3. C++函数模板学习

    2024-07-11 05:24:06       19 阅读
  4. 探索Perl的自动清洁工:垃圾收集机制全解析

    2024-07-11 05:24:06       21 阅读
  5. Kruskal

    2024-07-11 05:24:06       23 阅读
  6. C++入门

    C++入门

    2024-07-11 05:24:06      21 阅读
  7. Spring框架配置进阶_自动装配(XML和注解)

    2024-07-11 05:24:06       21 阅读
  8. xml CDATA

    2024-07-11 05:24:06       23 阅读
  9. XML Schema 杂项数据类型

    2024-07-11 05:24:06       24 阅读
  10. 我的前端实习之旅

    2024-07-11 05:24:06       22 阅读