用ChatGPT编写的一个调用ElasticSearch的maven的spring elasticsearch demo案例
创始人
2025-05-29 02:23:18
0

以下是一个简单的Maven工程和Spring demo案例,演示如何使用Java调用Elasticsearch。

配置Maven依赖

在pom.xml文件中添加以下依赖:

org.elasticsearch elasticsearch 6.5.4 org.elasticsearch.client transport 6.5.4 org.elasticsearch.client rest 6.5.4

创建Elasticsearch连接

创建一个Elasticsearch连接配置类,用于连接Elasticsearch集群。示例代码如下:

@Configuration
public class ElasticsearchConfig {

private static final String HOST = "localhost";
private static final int PORT = 9300;@Bean
public RestHighLevelClient restHighLevelClient() {RestClientBuilder builder = RestClient.builder(new HttpHost(HOST, PORT));RestHighLevelClient client = new RestHighLevelClient(builder);return client;
}

}

创建Elasticsearch操作类

创建一个Elasticsearch操作类,用于执行Elasticsearch的操作。示例代码如下:


@Componentpublic class ElasticsearchService {private RestHighLevelClient client;@Autowired
public ElasticsearchService(RestHighLevelClient client) {this.client = client;
}public void createIndex(String indexName) throws IOException {CreateIndexRequest request = new CreateIndexRequest(indexName);client.indices().create(request, RequestOptions.DEFAULT);
}public void deleteIndex(String indexName) throws IOException {DeleteIndexRequest request = new DeleteIndexRequest(indexName);client.indices().delete(request, RequestOptions.DEFAULT);
}public void insert(String indexName, String id, String jsonString) throws IOException {IndexRequest request = new IndexRequest(indexName);request.id(id);request.source(jsonString, XContentType.JSON);IndexResponse response = client.index(request, RequestOptions.DEFAULT);
}public void update(String indexName, String id, String jsonString) throws IOException {
UpdateRequest request = new UpdateRequest(indexName, id);request.doc(jsonString, XContentType.JSON);UpdateResponse response = client.update(request, RequestOptions.DEFAULT);}public void delete(String indexName, String id) throws IOException {DeleteRequest request = new DeleteRequest(indexName, id);DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
}public SearchHits search(String indexName, QueryBuilder queryBuilder) throws IOException {SearchRequest searchRequest = new SearchRequest(indexName);SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();searchSourceBuilder.query(queryBuilder);searchRequest.source(searchSourceBuilder);SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);SearchHits hits = response.getHits();return hits;
}}4. 创建Spring Boot控制器创建一个Spring Boot控制器,在其中调用Elasticsearch操作类执行Elasticsearch操作。示例代码如下:@RestControllerpublic class ElasticsearchController {@Autowired
private ElasticsearchService elasticsearchService;@GetMapping("/createIndex")
public String createIndex() throws IOException {elasticsearchService.createIndex("my_index");return "Index created successfully";
}@GetMapping("/deleteIndex")
public String deleteIndex() throws IOException {elasticsearchService.deleteIndex("my_index");return "Index deleted successfully";
}@PostMapping("/insert")
public String insert() throws IOException {String jsonString = "{\"title\":\"Java Programming\",\"author\":\"John Doe\"}";elasticsearchService.insert("my_index", "1", jsonString);return "Document inserted successfully";
}@PostMapping("/update")
public String update() throws IOException {String jsonString = "{\"title\":\"Java Programming\",\"author\":\"Jane Doe\"}";elasticsearchService.update("my_index", "1", jsonString);return "Document updated successfully";
}@GetMapping("/delete")
public String delete() throws IOException {elasticsearchService.delete("my_index", "1");return "Document deleted successfully";
}@GetMapping("/search")
public SearchHits search() throws IOException {QueryBuilder queryBuilder = QueryBuilders.matchQuery("title", "Java");SearchHits hits = elasticsearchService.search("my_index", queryBuilder);return hits;
}}

以上代码演示了如何创建Elasticsearch连接、执行Elasticsearch

操作,以及在Spring Boot控制器中调用Elasticsearch操作类执行操作。

测试

运行Spring Boot应用程序,并访问以下URL以执行相应的操作:

/createIndex:创建名为“my_index”的索引。

/deleteIndex:删除名为“my_index”的索引。

/insert:向名为“my_index”的索引插入一条文档。

/update:更新名为“my_index”的索引中的文档。

/delete:从名为“my_index”的索引中删除文档。

/search:从名为“my_index”的索引中搜索匹配“Java”的文档。

可以使用Postman或类似的工具进行测试。

以上就是一个基本的Maven工程和Spring demo案例,演示了如何使用Java调用Elasticsearch。这只是一个简单的示例,实际应用中可能需要更复杂和细致的实现。

相关内容

热门资讯

前端-session、jwt 目录:   (1)session (2&#x...
linux入门---制作进度条 了解缓冲区 我们首先来看看下面的操作: 我们首先创建了一个文件并在这个文件里面添加了...
关于测试,我发现了哪些新大陆 关于测试 平常也只是听说过一些关于测试的术语,但并没有使用过测试工具。偶然看到编程老师...
前缀和与对数器与二分法 1. 前缀和 假设有一个数组,我们想大量频繁的去访问L到R这个区间的和,...
nodejs:本地安装nvm实... 一、背景-使用不同版本node的原因 vue3+ts、nuxt3版本,node...
JAVA集合知识整理 Java集合知识整理 HashMap相关 HashMap的底层数据结构:jdk1.8之...
无刷直流电机介绍及单片机控制实... 无刷直流电机介绍及单片机控制实例前言基本概念优势与劣势使用寿命基本结构使用单片机控制实例电子调速器&...
fwdiary(2) dp2 1.传纸条  AcWing 275. 传纸条 - AcWing 走两条路,走一条最大的...
常用的DOS命令 常用的DOS命令 DOS(Disk Operating System,磁...
<C++> 类和对象(下) 1.const成员函数将const修饰的“成员函数”称之为const成员函数,cons...