此篇文章为springboot整合elasticsearch的demo,以此做一下记录
文章目录
环境准备
环境 | 版本 |
---|---|
springboot | 2.0.5.RELEASE |
elasticsearch | 6.2.4 |
注意:建议不要用elasticsearch的最新版,参考了spring data elasticsearch官网
因为整合springboot根elasticsearch有版本要求,所以建议按照官网的版本来实现demo,以免有其他坑出现。
运行elasticsearch
运行环境:win10
去到对应elasticsearch目录下的bin目录运行elasticsearch.bat
cmd运行elasticsearch.bat
运行起来
访问
http://localhost:9200/
demo代码展示
代码结构图
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.elasticsearch.demo</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml
application.yml
server:
port: 8080
spring:
data:
elasticsearch:
cluster-nodes: localhost:9300
# cluster-name: my-application
model
com.example.elasticsearch.demo.demo.elasticsearch.model.BlogModel
package com.example.elasticsearch.demo.demo.elasticsearch.model;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.format.annotation.DateTimeFormat;
import java.io.Serializable;
import java.util.Date;
@Data
@Accessors(chain = true)
@Document(indexName = "blog", type = "java")
public class BlogModel implements Serializable {
private static final long serialVersionUID = 6320548148250372657L;
@Id
private String id;
private String title;
//@Field(type = FieldType.Date, format = DateFormat.basic_date)
@DateTimeFormat(pattern = "yyyy-MM-dd")
@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
private Date time;
}
service
com.example.elasticsearch.demo.demo.elasticsearch.service.BlogRepository
package com.example.elasticsearch.demo.demo.elasticsearch.service;
import com.example.elasticsearch.demo.demo.elasticsearch.model.BlogModel;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;
public interface BlogRepository extends ElasticsearchRepository<BlogModel, String> {
List<BlogModel> findByTitleLike(String keyword);
}
controller
package com.example.elasticsearch.demo.demo.elasticsearch.controller;
import com.example.elasticsearch.demo.demo.elasticsearch.service.BlogRepository;
import com.example.elasticsearch.demo.demo.elasticsearch.model.BlogModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping("/blog")
public class BlogController {
@Autowired
private BlogRepository blogRepository;
@PostMapping("/add")
public String add(@RequestBody BlogModel blogModel) {
blogRepository.save(blogModel);
return "SAVE IN ELASTICSEARCH OK";
}
@GetMapping("/get/{id}")
public BlogModel getById(@PathVariable String id) {
Optional<BlogModel> blogModelOptional = blogRepository.findById(id);
if (blogModelOptional.isPresent()) {
BlogModel blogModel = blogModelOptional.get();
return blogModel;
}
return null;
}
@GetMapping("/get")
public List getAll() {
Iterable<BlogModel> iterable = blogRepository.findAll();
List<BlogModel> list = new ArrayList<>();
iterable.forEach(list::add);
return list;
}
@GetMapping("/title")
public List repSearchTitle(@RequestParam String keyword) {
if (StringUtils.isEmpty(keyword))
return null;
return blogRepository.findByTitleLike(keyword);
}
}
启动类
com.example.elasticsearch.demo.demo.DemoApplication
package com.example.elasticsearch.demo.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
postman请求连接
插入数据
{
"title":":美国环境保护署",
"time":"2019-05-06"
}
{
"title":":美国多个品牌快餐店停售汉堡",
"time":"2019-05-06"
}
{
"title":":美国新冠感染病例超过125万例",
"time":"2019-05-06"
}
插入后返回json
SAVE IN ELASTICSEARCH OK
查询数据
get请求可在浏览器访问
http://localhost:8080/blog/get
返回json
[
{
"id": "EfEE83EBpdk2CU62IpTH",
"title": ":美国新冠感染病例超过125万例",
"time": "2019-05-06"
},
{
"id": "EvEW83EBpdk2CU62XpQN",
"title": ":美国多个品牌快餐店停售汉堡",
"time": "2019-05-06"
},
{
"id": "E_EX83EBpdk2CU62MJRL",
"title": ":美国环境保护署",
"time": "2019-05-06"
},
{
"id": "EPH98nEBpdk2CU62f5TW",
"title": "Elasticsearch实战篇:Spring Boot整合ElasticSearch",
"time": "2019-05-06"
}
]
请求
http://localhost:8080/blog/title?keyword=美国
结果
[
{
"id": "E_EX83EBpdk2CU62MJRL",
"title": ":美国环境保护署",
"time": "2019-05-06"
},
{
"id": "EfEE83EBpdk2CU62IpTH",
"title": ":美国新冠感染病例超过125万例",
"time": "2019-05-06"
},
{
"id": "EvEW83EBpdk2CU62XpQN",
"title": ":美国多个品牌快餐店停售汉堡",
"time": "2019-05-06"
}
]
请求url
http://localhost:8080/blog/get/E_EX83EBpdk2CU62MJRL
结果
{
"id": "E_EX83EBpdk2CU62MJRL",
"title": ":美国环境保护署",
"time": "2019-05-06"
}
请求url
github连接
https://github.com/sky5cai/Elasticsearchdemo
参考资料
Spring Data Elasticsearch - Reference Documentation
https://docs.spring.io/spring-data/elasticsearch/docs/3.2.7.RELEASE/reference/html/#repositories.query-methods
Elasticsearch实战篇——Spring Boot整合ElasticSearch
https://segmentfault.com/a/1190000018625101