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

NO IMAGE

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

一、注入elasticsearch依賴

 <!-- elasticsearch -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<!-- <version>6.0.0</version> -->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
</dependency>
<dependency>  
<groupId>com.fasterxml.jackson.core</groupId>  
<artifactId>jackson-databind</artifactId>  
<!-- <version>2.1.3</version>   -->
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<!-- <version>3.0.9</version> -->
</dependency>

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

# elasticsearch
#節點名字,預設elasticsearch
spring.data.elasticsearch.cluster-name=elasticsearch
#節點地址,多個節點用逗號隔開
spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300
#spring.data.elasticsearch.local=false
spring.data.elasticsearch.repositories.enable=true

三、實體類

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
// indexName :索引名字(對應mysql的資料庫名字)
//type:型別(對應mysql的表名)
@Document(indexName = "megacorp",type = "employee", shards = 1,replicas = 0, refreshInterval = "-1")
public class Employee {
@Id
private String id;
@Field
private String firstName;
@Field
private String lastName;
@Field
private Integer age=0;
@Field
private String about;
//get、set...
}

四、編寫dao

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Component;
import com.example.demo.elasticsearch.entity.Employee;
@Component
public interface EmployeeRepository extends ElasticsearchRepository<Employee,String>{
Employee queryEmployeeById(String id);
}

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.elasticsearch.dao.EmployeeRepository;
import com.example.demo.elasticsearch.entity.Employee;
import com.google.gson.Gson;
@RestController
@RequestMapping("/es")
public class ElasticSearchController {
@Autowired
private EmployeeRepository er;
//增加
@RequestMapping("/add")
public String add(){
Employee employee=new Employee();
employee.setId("1");
employee.setFirstName("xuxu");
employee.setLastName("zh");
employee.setAge(26);
employee.setAbout("i am in peking");
er.save(employee);
System.err.println("add a obj");
return "success";
}
//刪除
@RequestMapping("/delete")
public String delete(){
er.delete("1");
return "success";
}
//區域性更新
@RequestMapping("/update")
public String update(){
Employee employee=er.queryEmployeeById("1");
employee.setFirstName("哈哈");
er.save(employee);
System.err.println("update a obj");
return "success";
}
//查詢
@RequestMapping("/query")
public Employee query(){
Employee accountInfo=er.queryEmployeeById("1");
System.err.println(new Gson().toJson(accountInfo));
return accountInfo;
}
}

至此,elasticsearch就算是入門了。

參考文獻:https://blog.csdn.net/zhaoyahui_666/article/details/78688688