当前位置:   article > 正文

SpringBoot集成ElasticSearch,实现增删改查_"@document(indexname = \"company\",type = \"employ

"@document(indexname = \"company\",type = \"employee\", shards = 1,replicas = 0,"

背景:阿里云服务器,搭建Docker,部署ElasticSearch镜像,本地SpringBoot链接ES,实现增删改查

安装Docker:https://blog.csdn.net/BinziD/article/details/111224764

Docker安装ElasticSearch:https://blog.csdn.net/BinziD/article/details/111314796

1. 配置文件(配置项不能错)

  1. spring.data.elasticsearch.cluster-name=es-log
  2. spring.data.elasticsearch.cluster-nodes=39.99.48.28:9300
  3. spring.data.elasticsearch.repositories.enabled=true

2. pom.xml文件

  1. <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
  2. <dependency>
  3. <groupId>com.alibaba</groupId>
  4. <artifactId>fastjson</artifactId>
  5. <version>1.2.62</version>
  6. </dependency>
  7. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-elasticsearch -->
  8. <dependency>
  9. <groupId>org.springframework.boot</groupId>
  10. <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
  11. <version>2.0.2.RELEASE</version>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.projectlombok</groupId>
  15. <artifactId>lombok</artifactId>
  16. <version>1.18.8</version>
  17. </dependency>

3. Entity

  1. package org.big.entity;
  2. import org.springframework.data.annotation.Id;
  3. import org.springframework.data.elasticsearch.annotations.Document;
  4. import org.springframework.data.elasticsearch.annotations.Field;
  5. import lombok.Getter;
  6. import lombok.Setter;
  7. import lombok.ToString;
  8. @Getter
  9. @Setter
  10. @ToString
  11. @Document(indexName = "company",type = "employee", shards = 1,replicas = 0, refreshInterval = "-1")
  12. public class Employee {
  13. @Id
  14. private String id;
  15. @Field
  16. private String firstName;
  17. @Field
  18. private String lastName;
  19. @Field
  20. private Integer age = 0;
  21. @Field
  22. private String about;
  23. }

4. Controller层

  1. package org.big.controller;
  2. import org.big.entity.Employee;
  3. import org.big.repository.EmployeeRepository;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import com.alibaba.fastjson.JSON;
  8. @RestController
  9. //@RequestMapping("es")
  10. public class EmployeeController {
  11. @Autowired
  12. private EmployeeRepository employeeRepository;
  13. /**
  14. * 添加
  15. * @return
  16. */
  17. @RequestMapping("add")
  18. public String add() {
  19. Employee employee = new Employee();
  20. employee.setId("1");
  21. employee.setFirstName("xuxu");
  22. employee.setLastName("zh");
  23. employee.setAge(26);
  24. employee.setAbout("i am in peking");
  25. employeeRepository.save(employee);
  26. System.err.println("add a obj");
  27. return "success";
  28. }
  29. /**
  30. * 删除
  31. * @return
  32. */
  33. @RequestMapping("delete")
  34. public String delete() {
  35. Employee employee = employeeRepository.queryEmployeeById("1");
  36. employeeRepository.delete(employee);
  37. return "success";
  38. }
  39. /**
  40. * 局部更新
  41. * @return
  42. */
  43. @RequestMapping("update")
  44. public String update() {
  45. Employee employee = employeeRepository.queryEmployeeById("1");
  46. employee.setFirstName("哈哈");
  47. employeeRepository.save(employee);
  48. System.err.println("update a obj");
  49. return "success";
  50. }
  51. /**
  52. * 查询
  53. * @return
  54. */
  55. @RequestMapping("query")
  56. public Employee query() {
  57. Employee accountInfo = employeeRepository.queryEmployeeById("1");
  58. System.err.println(JSON.toJSONString(accountInfo));
  59. return accountInfo;
  60. }
  61. }

5. Repository层

  1. package org.big.repository;
  2. import org.big.entity.Employee;
  3. import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
  4. import org.springframework.stereotype.Component;
  5. @Component
  6. public interface EmployeeRepository extends ElasticsearchRepository<Employee, String> {
  7. /**
  8. * 通过Id查询Employee对象
  9. * @param id
  10. * @return
  11. */
  12. Employee queryEmployeeById(String id);
  13. }

6.访问:http://localhost:8080/add

7. 问题:org.elasticsearch.client.transport.NoNodeAvailableException

解决:端口须指定为9300

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

闽ICP备14008679号