当前位置:   article > 正文

spring-boot集成elasticsearch并实现简单的增删改查_elasticsearch 局部更新 springboot

elasticsearch 局部更新 springboot

java操作elasticsearch是作为一个无数据节点与其他节点之间通信,端口是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. spring.data.elasticsearch.cluster-name=elasticsearch #节点名字,默认elasticsearch
  3. spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300 #节点地址,多个节点用逗号隔开
  4. #spring.data.elasticsearch.local=false
  5. 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. public String getId() {return id;}
  19. public void setId(String id){this.id = id;}
  20. public String getFirstName() {return firstName;}
  21. public void setFirstName(String firstName) {this.firstName = firstName;}
  22. public String getLastName() {return lastName;}
  23. public void setLastName(String lastName) {this.lastName = lastName;}
  24. public Integer getAge() {return age;}
  25. public void setAge(Integer age) {this.age = age;}
  26. public String getAbout() {return about;}
  27. public void setAbout(String about) {this.about = about;}
  28. }

 

 

 

 

四、编写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就算是入门了。

 

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

闽ICP备14008679号