ES创建索引的过程

本文详细剖析了ES创建索引的过程,从总览到准备文档的索引计划,再到索引写入Lucene的步骤。重点介绍了InternalEngine.java中的planIndexingAsPrimary方法,解释了四种不同的IndexingStrategy及其应用场景,特别是对版本冲突的判断和处理策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                                                                                    ES中创建索

### 使用 Spring Boot 和 Elasticsearch 创建索引 在 Spring Boot 项目中集成并使用 Elasticsearch创建索引过程涉及多个方面,包括配置依赖项、设置连接属性以及编写具体的操作逻辑。 #### 添加 Maven 依赖 为了使应用程序能够与 Elasticsearch 进行交互,在 `pom.xml` 文件中加入必要的库: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> ``` 这一步骤确保了开发环境具备访问 RESTful API 所需的功能[^1]。 #### 配置 application.properties 或 application.yml 接着定义数据库连接参数和其他必要选项。对于 YAML 格式的文件来说,可以这样写入相应的字段来指定主机地址和服务端口等信息: ```yaml spring: elasticsearch: rest: uris: http://localhost:9200 ``` 上述配置指定了本地运行的一个单节点集群实例作为目标服务器[^2]。 #### 编写 Java 类完成索引管理任务 最后就是实际编码部分了。这里给出一段简单的例子用来演示怎样利用 `ElasticsearchOperations` 接口下的方法新建自定义结构的数据存储空间——即所谓的“索引”。 ```java import org.elasticsearch.index.query.QueryBuilders; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.elasticsearch.core.ElasticsearchOperations; import org.springframework.stereotype.Service; @Service public class IndexService { @Autowired private ElasticsearchOperations operations; public void createIndex(String indexName){ if (!operations.indexExists(indexName)){ operations.createIndex(indexName); System.out.println("Created new index named "+indexName+" successfully."); }else{ System.out.println("The specified index already exists!"); } } } ``` 此段程序首先验证所给定名称对应的索引是否存在;如果不存在,则调用相应的方法建立新的索引,并打印成功消息到控制台;反之则提示该索引已存在[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值