当前位置: 首页 > news >正文

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);// 关闭RestClientrestClient.close();return statusCode;}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 分布式服务基于Zookeeper的分布式锁的实现
  • 全栈智能家居系统设计方案:STM32+Linux+多协议(MQTT、Zigbee、Z-Wave)通信+云平台集成
  • LangChain —— 多模态大模型的 prompt template
  • WPF 手撸插件 一
  • vite-服务端渲染(ssr)项目线上频繁刷新(踩坑记录)
  • langchain新版本v0.2文档:tutorials(1)
  • 【Neo4j】实战 (数据库技术丛书)学习笔记
  • Ubuntu 22.04.4 LTS (linux) 安装certbot 免费ssl证书申请 letsencrypt
  • python条件
  • 【Android面试八股文】请描述一下 android 的系统架构?
  • WSL-Ubuntu20.04部署环境配置
  • Web 性能入门指南-1.1 网站速度与用户幸福感的心理学
  • 51单片机5(GPIO简介)
  • Go:基本变量与数据类型
  • Excel如何才能忽略隐藏行进行复制粘贴?
  • php的引用
  • 【每日笔记】【Go学习笔记】2019-01-10 codis proxy处理流程
  • 30秒的PHP代码片段(1)数组 - Array
  • JavaWeb(学习笔记二)
  • js对象的深浅拷贝
  • Js基础——数据类型之Null和Undefined
  • js中forEach回调同异步问题
  • Python_网络编程
  • 纯 javascript 半自动式下滑一定高度,导航栏固定
  • 第13期 DApp 榜单 :来,吃我这波安利
  • 构建二叉树进行数值数组的去重及优化
  • 精彩代码 vue.js
  • 如何优雅的使用vue+Dcloud(Hbuild)开发混合app
  • 入口文件开始,分析Vue源码实现
  • 一起来学SpringBoot | 第十篇:使用Spring Cache集成Redis
  • 【运维趟坑回忆录 开篇】初入初创, 一脸懵
  • const的用法,特别是用在函数前面与后面的区别
  • ​比特币大跌的 2 个原因
  • #14vue3生成表单并跳转到外部地址的方式
  • #Linux(make工具和makefile文件以及makefile语法)
  • $$$$GB2312-80区位编码表$$$$
  • $redis-setphp_redis Set命令,php操作Redis Set函数介绍
  • %check_box% in rails :coditions={:has_many , :through}
  • (2024,Vision-LSTM,ViL,xLSTM,ViT,ViM,双向扫描)xLSTM 作为通用视觉骨干
  • (23)Linux的软硬连接
  • (板子)A* astar算法,AcWing第k短路+八数码 带注释
  • (附源码)ssm基于jsp的在线点餐系统 毕业设计 111016
  • (附源码)计算机毕业设计SSM基于健身房管理系统
  • (九)c52学习之旅-定时器
  • (蓝桥杯每日一题)平方末尾及补充(常用的字符串函数功能)
  • (论文阅读23/100)Hierarchical Convolutional Features for Visual Tracking
  • (十二)python网络爬虫(理论+实战)——实战:使用BeautfulSoup解析baidu热搜新闻数据
  • (十一)手动添加用户和文件的特殊权限
  • (实战)静默dbca安装创建数据库 --参数说明+举例
  • (一)插入排序
  • (已解决)Bootstrap精美弹出框模态框modal,实现js向modal传递数据
  • (转)清华学霸演讲稿:永远不要说你已经尽力了
  • .helper勒索病毒的最新威胁:如何恢复您的数据?
  • .Mobi域名介绍
  • .net 7和core版 SignalR