当前位置:   article > 正文

springboot集成ES,ElasticsearchClient数据增删改查_elasticsearchclient.update

elasticsearchclient.update

pom依赖引入

  1. <dependency>
  2. <groupId>org.elasticsearch</groupId>
  3. <artifactId>elasticsearch</artifactId>
  4. <version>8.3.2</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>co.elastic.clients</groupId>
  8. <artifactId>elasticsearch-java</artifactId>
  9. <version>8.3.2</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.elasticsearch.client</groupId>
  13. <artifactId>elasticsearch-rest-client</artifactId>
  14. <version>8.3.2</version>
  15. </dependency>
  16. <dependency>
  17. <groupId>jakarta.json</groupId>
  18. <artifactId>jakarta.json-api</artifactId>
  19. <version>2.1.1</version>
  20. </dependency>

 application.yaml配置文件ip地址写法:

  1. spring:
  2. main:
  3. allow-bean-definition-overriding: true
  4. servlet:
  5. multipart:
  6. max-request-size: 100MB
  7. max-file-size: 100MB
  8. # 搜索引擎
  9. elasticsearch:
  10. rest:
  11. # username: elastic
  12. # password: elastic
  13. uris: 127.0.0.1:9200
ElasticsearchConfig类:
  1. @Component
  2. public class ElasticsearchConfig {
  3. @Bean
  4. public ElasticsearchClient elasticsearchClient(RestClient restClient) {
  5. ElasticsearchTransport transport = new RestClientTransport(restClient,new JacksonJsonpMapper());
  6. ElasticsearchClient elasticsearchClient = new ElasticsearchClient(transport);
  7. return elasticsearchClient;
  8. }
  9. }

将数据写入es,比如我们要将Info实体的数据写入es:

  1. IndexRequest<Object> indexRequest = new IndexRequest.Builder<>()
  2. .index("info") //索引
  3. .id(info.getTid()+"") //主键
  4. .document(info) //需要写入es的实体
  5. .build();
  6. try {
  7. elasticsearchClient.index(indexRequest);
  8. } catch (ElasticsearchException | IOException e) {
  9. log.error("",e);
  10. }

es的查询、修改、删除:

  1. @Autowired
  2. private ElasticsearchClient elasticsearchClient;
  3. /**
  4. * 通过id查询
  5. */
  6. public static void get() {
  7. try {
  8. long tid = 1889L;
  9. Query query = QueryBuilders.ids().values(tid + "").build()._toQuery();
  10. SearchResponse<Info> response = elasticsearchClient
  11. .search(s -> s.index("info").query(query), Info.class);
  12. for (Hit<Info> hit : response.hits().hits()) {
  13. Info source = hit.source();
  14. System.err.println("Found product " + source);
  15. }
  16. } catch (Exception e) {
  17. // TODO: handle exception
  18. }
  19. }
  20. /**
  21. * 根据id修改数据
  22. */
  23. public static void edit() {
  24. try {
  25. long tid = 1889L;
  26. Map<String, Object> map = new HashMap<>();
  27. map.put("status", 1);
  28. // 构建修改的请求,因为修改数据es会默认1秒后生效,需要加上refresh(Refresh.True)立即生效
  29. UpdateResponse<Info> response = elasticsearchClient.update(
  30. e -> e.index("info").id(tid + "").refresh(Refresh.True).doc(map),
  31. Info.class);
  32. } catch (Exception e) {
  33. // TODO: handle exception
  34. }
  35. }
  36. /**
  37. * 删除
  38. */
  39. public static void delete() {
  40. try {
  41. long tid = 1889L;
  42. elasticsearchClient.delete(e -> e.index("info").id(tid + ""));
  43. } catch (Exception e) {
  44. // TODO: handle exception
  45. }
  46. }
  47. /**
  48. * 删除索引
  49. */
  50. public static void deleteAll() {
  51. try {
  52. DeleteIndexResponse deleteIndexResponse = elasticsearchClient.indices().delete(s -> s.index("info"));
  53. } catch (ElasticsearchException e) {
  54. // TODO Auto-generated catch block
  55. e.printStackTrace();
  56. } catch (IOException e) {
  57. // TODO Auto-generated catch block
  58. e.printStackTrace();
  59. }
  60. }

下期分享ElasticsearchClient查询分页、高级查询的方法~

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/68801
推荐阅读
相关标签
  

闽ICP备14008679号