springboot集成ElasticSearch
本例场景:
springboot集成ElasticSearch,
使用JestClient操作ElasticSearch文件服务器
本例子demo源码: https://github.com/zhangbeizhen/springboot-elasticsearch
1.在pom.xml中引入依赖
<dependency>
<groupId>io.searchbox</groupId>
<artifactId>jest</artifactId>
<version>5.3.3</version>
</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>
2.在application.properties配置文件服务器地址
spring.elasticsearch.jest.uris=http://172.16.4.156:9200
3.在ElasticsearchController中实现插入数据与查询数据
@RestController
public class ElasticsearchController {
/**注入操作客户端*/
@Autowired
JestClient jestClient;
/**查匹配员工信息*/
@GetMapping("/query")
public String elastic( @RequestBody String matchJson){
/**构建一个查询*/
Search search = new Search.Builder(matchJson).addIndex("info").addType("emp").build();
SearchResult result =null;
try {
result = jestClient.execute(search);
} catch (IOException e) {
e.printStackTrace();
}
return result.getJsonString();
}
/**插入信息*/
@PostMapping("/insert")
public String insertEmployee(@RequestBody Employee employee) {
/**构建一个索引*/
Index index = new Index.Builder(employee).index("info").type("emp").build();
JestResult result = null;
try {
result = jestClient.execute(index);
} catch (IOException e) {
e.printStackTrace();
}
return result.getJsonString();
}
}
4.实例对象
public class Employee implements Serializable {
private Integer id;
private String lastName;
private String email;
private Integer gender;
private String cardNumber;
private String post;
private Integer departmentId;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public String getCardNumber() {
return cardNumber;
}
public void setCardNumber(String cardNumber) {
this.cardNumber = cardNumber;
}
public String getPost() {
return post;
}
public void setPost(String post) {
this.post = post;
}
public Integer getDepartmentId() {
return departmentId;
}
public void setDepartmentId(Integer departmentId) {
this.departmentId = departmentId;
}
}
5.测试
1>.测试插入数据
请求数据:
{
"cardNumber": "20190116",
"departmentId": 1,
"email": "[email protected]",
"gender": 1,
"id": 1,
"lastName": "张三",
"post": "程序猿"
}
Postman工具截图:
2>.测试查询数据
请求数据:
{
"query": {
"match": {
"lastName": "张三"
}
}
}
Postman工具截图:
6.在Linux上使用docker安装ElasticSearch
1>.搜索镜像
docker search elasticsearch
2>.下载镜像
docker pull elasticsearch:5.5
3>.运行-堆内存限制
docker run -e ES_JAVA_OPTS='-Xms256m -Xmx563m' -d -p 9200:9200 -p 9300:9300 --name ES01 镜像ID
解析:
9200: web通信端口
9300: 分布式节点之间端口
4.web验证启动成功
http://172.16.4.156:9200
返回信息:
{
"name" : "yeth9tA",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "X9YMjDcXSVaeu4oIqaOYbQ",
"version" : {
"number" : "5.5.2",
"build_hash" : "b2f0c09",
"build_date" : "2017-08-14T12:33:14.154Z",
"build_snapshot" : false,
"lucene_version" : "6.6.0"
},
"tagline" : "You Know, for Search"
}
以上,TKS.