此篇文章為springboot整合elasticsearch的demo,以此做一下記錄
文章目錄
- 環境準備
- 運作elasticsearch
- demo代碼展示
-
- 代碼結構圖
- pom.xml
- application.yml
- model
- service
- controller
- 啟動類
- postman請求連接配接
-
- 插入資料
- 查詢資料
- github連接配接
- 參考資料
環境準備
環境 | 版本 |
---|---|
springboot | 2.0.5.RELEASE |
elasticsearch | 6.2.4 |
注意:建議不要用elasticsearch的最新版,參考了spring data elasticsearch官網因為整合springboot根elasticsearch有版本要求,是以建議按照官網的版本來實作demo,以免有其他坑出現。![]()
springboot整合elasticsearch環境準備運作elasticsearchdemo代碼展示postman請求連接配接github連接配接參考資料
運作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