ZBLOG

给我一个springboot 操作es的demo。并且用的是 Elasticsearch Java High Level REST Client

下面是一个简单的 Spring Boot 示例,演示如何使用 Elasticsearch Java High Level REST Client 操作 Elasticsearch:

  1. 添加依赖

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

  1. <dependency>
  2. <groupId>org.elasticsearch.client</groupId>
  3. <artifactId>elasticsearch-rest-high-level-client</artifactId>
  4. <version>7.12.0</version>
  5. </dependency>
  1. 配置连接

在 application.properties 文件中配置 Elasticsearch 连接信息:

  1. spring.elasticsearch.rest.uris=http://localhost:9200
  1. 创建索引

创建一个索引,并向其中添加一些文档:

  1. import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
  2. import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
  3. import org.elasticsearch.client.RequestOptions;
  4. import org.elasticsearch.client.RestHighLevelClient;
  5. import org.elasticsearch.common.xcontent.XContentType;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Component;
  8. @Component
  9. public class IndexService {
  10. @Autowired
  11. private RestHighLevelClient client;
  12. public void createIndex() throws Exception {
  13. // 创建索引请求对象
  14. CreateIndexRequest request = new CreateIndexRequest("my_index");
  15. // 设置索引的映射关系
  16. request.mapping("{\n" +
  17. " \"properties\": {\n" +
  18. " \"title\": {\n" +
  19. " \"type\": \"text\"\n" +
  20. " },\n" +
  21. " \"content\": {\n" +
  22. " \"type\": \"text\"\n" +
  23. " }\n" +
  24. " }\n" +
  25. "}", XContentType.JSON);
  26. // 发送请求并获取响应结果
  27. CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
  28. // 处理响应结果
  29. boolean acknowledged = response.isAcknowledged();
  30. if (acknowledged) {
  31. System.out.println("Index created successfully!");
  32. } else {
  33. System.out.println("Failed to create index.");
  34. }
  35. }
  36. }
  1. 添加文档

向索引中添加一些文档:

  1. import org.elasticsearch.action.index.IndexRequest;
  2. import org.elasticsearch.action.index.IndexResponse;
  3. import org.elasticsearch.client.RequestOptions;
  4. import org.elasticsearch.client.RestHighLevelClient;
  5. import org.elasticsearch.common.xcontent.XContentBuilder;
  6. import org.elasticsearch.common.xcontent.XContentFactory;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Component;
  9. @Component
  10. public class DocumentService {
  11. @Autowired
  12. private RestHighLevelClient client;
  13. public void addDocument() throws Exception {
  14. // 创建 IndexRequest 对象,指定要添加的索引和文档 ID
  15. IndexRequest request = new IndexRequest("my_index", "1");
  16. // 创建文档数据
  17. XContentBuilder builder = XContentFactory.jsonBuilder()
  18. .startObject()
  19. .field("title", "Title 1")
  20. .field("content", "This is the content of document 1.")
  21. .endObject();
  22. // 将文档数据添加到请求对象中
  23. request.source(builder);
  24. // 发送请求并获取响应结果
  25. IndexResponse response = client.index(request, RequestOptions.DEFAULT);
  26. // 处理响应结果
  27. String id = response.getId();
  28. System.out.println("Document added successfully: " + id);
  29. }
  30. }
  1. 查询文档

查询索引中的文档:

  1. import org.elasticsearch.action.search.SearchRequest;
  2. import org.elasticsearch.action.search.SearchResponse;
  3. import org.elasticsearch.client.RequestOptions;
  4. import org.elasticsearch.client.RestHighLevelClient;
  5. import org.elasticsearch.index.query.QueryBuilders;
  6. import org.elasticsearch.search.SearchHit;
  7. import org.elasticsearch.search.builder.SearchSourceBuilder;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.stereotype.Component;
  10. @Component
  11. public class SearchService {
  12. @Autowired
  13. private RestHighLevelClient client;
  14. public void searchDocuments() throws Exception {
  15. // 创建 SearchRequest 对象,指定要搜索的索引
  16. SearchRequest request = new SearchRequest("my_index");
  17. // 构建查询条件
  18. SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
  19. sourceBuilder.query(QueryBuilders.matchQuery("title", "Title 1"));
  20. // 将查询条件添加到请求对象中
  21. request.source(sourceBuilder);
  22. // 发送请求并获取响应结果
  23. SearchResponse response = client.search(request, RequestOptions.DEFAULT);
  24. // 处理响应结果
  25. for (SearchHit hit : response.getHits().getHits()) {
  26. System.out.println(hit.getSourceAsString());
  27. }
  28. }
  29. }
  1. 在控制器中使用服务

将服务注入到控制器中,并在其中调用服务方法:

  1. @RestController
  2. @RequestMapping("/demo")
  3. public class DemoController {
  4. @Autowired
  5. private IndexService indexService;
  6. @Autowired
  7. private DocumentService documentService;
  8. @Autowired
  9. private SearchService searchService;
  10. @GetMapping("/createIndex")
  11. public String createIndex() throws Exception {
  12. indexService.createIndex();
  13. return "Index created successfully!";
  14. }
  15. @GetMapping("/addDocument")
  16. public String addDocument() throws Exception {
  17. documentService.addDocument();
  18. return "Document added successfully!";
  19. }
  20. @GetMapping("/searchDocuments")
  21. public List<String> searchDocuments() throws Exception {
  22. searchService.searchDocuments();
  23. return "Search finished successfully!";
  24. }
  25. }

在这个示例中,我们创建了一个 Spring Boot 应用程序,并使用 Elasticsearch Java High Level REST Client 执行一些基本操作。你可以将这个示例作为起点,进一步扩展和定制它以适应你的实际需求。

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=3790

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?