ElasticSearch是一个基于Lucene的搜索服务器,它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。ElasticSearch是用Java开发的,因此Java API是与ElasticSearch交互的主要方式。
Spring Boot是一个基于Spring框架的快速开发框架,它可以帮助我们快速搭建一个基于Spring的应用程序。
本文将介绍如何使用Spring Boot连接ElasticSearch7.17.3。
在开始之前,需要确保已经安装了ElasticSearch7.17.3,并且已经启动了ElasticSearch服务。
在项目的pom.xml文件中添加以下依赖:
org.springframework.boot spring-boot-starter-data-elasticsearch 2.6.0
在application.properties文件中添加以下配置:
spring.data.elasticsearch.cluster-nodes=localhost:9300
其中,localhost是ElasticSearch所在的主机名,9300是ElasticSearch的TCP端口号。
连接成功后,就可以使用Spring Data Elasticsearch操作ElasticSearch了。以下是一些常用的操作:
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;IndexOperations indexOperations = elasticsearchRestTemplate.indexOps(IndexCoordinates.of("index_name"));
indexOperations.create();
其中,index_name是索引的名称。
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;IndexOperations indexOperations = elasticsearchRestTemplate.indexOps(IndexCoordinates.of("index_name"));
indexOperations.delete();
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;Object object = new Object();elasticsearchRestTemplate.save(object, IndexCoordinates.of("index_name"));
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;UpdateQuery updateQuery = UpdateQuery.builder("document_id").withDocument("{ \"field1\": \"new_value1\", \"field2\": \"new_value2\" }").build();
elasticsearchRestTemplate.update(updateQuery, IndexCoordinates.of("index_name"));
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;elasticsearchRestTemplate.delete("document_id", IndexCoordinates.of("index_name"));
本文主要是讲解springboot操作elasticsearch简易方法,后续文章讲解springboot如何深度集成elasticsearch
上一篇: 英文句子唯美简短
下一篇: 冬至节适合发朋友圈句子