1. 程式人生 > >spring boot整合elasticsearch並實現簡單的增刪改查

spring boot整合elasticsearch並實現簡單的增刪改查

java操作elasticsearch是作為一個無資料節點與其他節點之間通訊,因此使用的是tcp埠,elasticsearch預設的節點間通訊的tcp埠是9300。elasticsearch和jdk版本一定要適配,因為elasticsearch是用java編寫的,隨著版本的升級,用的也是最新版的jdk,所以低版本的jdk就和最新elasticsearch版本不匹配。但是,高版本的jdk可以向下相容低版本的elasticsearch,因為jdk在升級的過程中,自身也要向下相容。這一點很重要,否則專案是起不來的。我用的是jdk1.8,elasticsearch-2.4.2。現在,讓我們開始整合。

一、注入elasticsearch依賴

  1. <!-- elasticsearch -->

  2. <dependency>

  3. <groupId>org.elasticsearch</groupId>

  4. <artifactId>elasticsearch</artifactId>

  5. <!-- <version>6.0.0</version> -->

  6. </dependency>

  7. <dependency>

  8. <groupId>org.springframework.boot</groupId>

  9. <artifactId>spring-boot-starter-data-elasticsearch</artifactId>

  10. </dependency>

  11. <dependency>

  12. <groupId>org.springframework.data</groupId>

  13. <artifactId>spring-data-elasticsearch</artifactId>

  14. </dependency>

  15. <dependency>

  16. <groupId>com.fasterxml.jackson.core</groupId>

  17. <artifactId>jackson-databind</artifactId>

  18. <!-- <version>2.1.3</version> -->

  19. </dependency>

  20. <dependency>

  21. <groupId>net.java.dev.jna</groupId>

  22. <artifactId>jna</artifactId>

  23. <!-- <version>3.0.9</version> -->

  24. </dependency>

二、在application.properties中新增elasticsearch配置

  1. # elasticsearch

  2. #節點名字,預設elasticsearch

  3. spring.data.elasticsearch.cluster-name=elasticsearch

  4. #節點地址,多個節點用逗號隔開

  5. spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300

  6. #spring.data.elasticsearch.local=false

  7. spring.data.elasticsearch.repositories.enable=true

三、實體類

  1. import org.springframework.data.annotation.Id;

  2. import org.springframework.data.elasticsearch.annotations.Document;

  3. import org.springframework.data.elasticsearch.annotations.Field;

  4. // indexName :索引名字(對應mysql的資料庫名字)

  5. //type:型別(對應mysql的表名)

  6. @Document(indexName = "megacorp",type = "employee", shards = 1,replicas = 0, refreshInterval = "-1")

  7. public class Employee {

  8. @Id

  9. private String id;

  10. @Field

  11. private String firstName;

  12. @Field

  13. private String lastName;

  14. @Field

  15. private Integer age=0;

  16. @Field

  17. private String about;

  18. //get、set...

  19. }

四、編寫dao

  1. import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

  2. import org.springframework.stereotype.Component;

  3. import com.example.demo.elasticsearch.entity.Employee;

  4. @Component

  5. public interface EmployeeRepository extends ElasticsearchRepository<Employee,String>{

  6. Employee queryEmployeeById(String id);

  7. }

五、由於咱們就是入門測試,service我就省略了,直接書寫介面了

  1. import org.springframework.beans.factory.annotation.Autowired;

  2. import org.springframework.web.bind.annotation.RequestMapping;

  3. import org.springframework.web.bind.annotation.RestController;

  4. import com.example.demo.elasticsearch.dao.EmployeeRepository;

  5. import com.example.demo.elasticsearch.entity.Employee;

  6. import com.google.gson.Gson;

  7. @RestController

  8. @RequestMapping("/es")

  9. public class ElasticSearchController {

  10. @Autowired

  11. private EmployeeRepository er;

  12. //增加

  13. @RequestMapping("/add")

  14. public String add(){

  15. Employee employee=new Employee();

  16. employee.setId("1");

  17. employee.setFirstName("xuxu");

  18. employee.setLastName("zh");

  19. employee.setAge(26);

  20. employee.setAbout("i am in peking");

  21. er.save(employee);

  22. System.err.println("add a obj");

  23. return "success";

  24. }

  25. //刪除

  26. @RequestMapping("/delete")

  27. public String delete(){

  28. er.delete("1");

  29. return "success";

  30. }

  31. //區域性更新

  32. @RequestMapping("/update")

  33. public String update(){

  34. Employee employee=er.queryEmployeeById("1");

  35. employee.setFirstName("哈哈");

  36. er.save(employee);

  37. System.err.println("update a obj");

  38. return "success";

  39. }

  40. //查詢

  41. @RequestMapping("/query")

  42. public Employee query(){

  43. Employee accountInfo=er.queryEmployeeById("1");

  44. System.err.println(new Gson().toJson(accountInfo));

  45. return accountInfo;

  46. }

  47. }

至此,elasticsearch就算是入門了。