1. 话不多说,先晒出pom文件中的依赖
<dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
</dependencies>
lombok是一个不用自己去给POJO定义getter和setter的jar包,idea或者eclipse都需要安装插件才能使用,安装方法此处就不赘述了。
2. 配置文件 aplication.yml
spring:
data:
elasticsearch:
cluster-name: elasticsearch # 默认为elasticsearch
cluster-nodes: 127.0.0.1:9300
repositories:
enabled: true
这里有个要注意的地方就是,在elasticsearch/conf/elasticsearch.yml中添加如下配置
http.cors.enabled: true
http.cors.allow-origin: "*"
network.host: 0.0.0.0
transport.tcp.port: 9300
transport.tcp.compress: true
network.host 是用于客户端连接的IP地址, 对应配置文件中的spring.data.cluster-nodes后的IP地址,一般配置为0.0.0.0即可。
tansport.tcp.port 是用于客户端连接的端口, 对应配置文件中的spring.data.cluster-nodes后的端口。(我之前一直以为是http.port,结果项目启动的时候总是报错)
3. 新建实体,这里由于使用了lombok框架,所以没有getter/setter已经tostring和构造函数等一系列的方法,代码比较简洁。
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
@Document(indexName = "company", type = "employee", shards = 1, replicas = 0, refreshInterval = "-1")
@Data
public class Employee {
@Id
private String id;
@Field(type = FieldType.Text)
private String firstName;
@Field(type = FieldType.Text)
private String lastName;
@Field(type = FieldType.Integer)
private Integer age = 0;
@Field(type = FieldType.Text)
private String about;
}
4. dao层
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Component;
@Component
public interface EmployeeRepository extends ElasticsearchRepository<Employee, String> {
Employee queryEmployeeById(String id);
}
5. 由于是入门级的,就不写service层了,直接controller好了
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/es")
public class ElasticSearchController {
@Autowired
private EmployeeRepository er;
@RequestMapping(value = "/add")
public String add() {
Employee employee = new Employee();
employee.setId("1");
employee.setFirstName("海波");
employee.setLastName("夏");
employee.setAge(24);
employee.setAbout("I am in coding");
er.save(employee);
System.out.println("add a obj");
return "success";
}
@RequestMapping(value = "/delete")
public String delete() {
er.deleteById("1");
return "success";
}
@RequestMapping(value = "/update")
public String update() {
Employee employee = er.queryEmployeeById("1");
employee.setFirstName("哈哈");
er.save(employee);
return "success";
}
//查询
@RequestMapping("/query")
public Employee query(){
Employee accountInfo=er.queryEmployeeById("1");
System.err.println(new Gson().toJson(accountInfo));
return accountInfo;
}
}
5. 到这里基本就成功了,可以启动,并测试了。