springboot2.4.5对应的es版本号7.9.3
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.4.5</version>
</parent>
<elasticsearch.version>7.9.3</elasticsearch.version>
1.引入jar包
<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.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
2.添加配置
server.port=8081
# es
spring.elasticsearch.rest.uris = 127.0.0.1:9200
spring.elasticsearch.rest.username=''
spring.elasticsearch.rest.password=''
# 日期格式化
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
3.构建entity
package com.example.springboot.es.entity.es;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.DateFormat;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import java.util.Date;
/**
* created by <a href="mailto:18045142590@163.com" > mm</a>
*/
@Data
@Document(indexName = "blog", useServerConfiguration = true, createIndex = false)
public class ESBlog {
@Id
private Integer id;
@Field(type = FieldType.Text, analyzer = "id_max_word")
private String title;
@Field(type = FieldType.Text, analyzer = "id_max_word")
private String author;
@Field(type = FieldType.Text, analyzer = "id_max_word")
private String content;
@Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis")
private Date createTime;
@Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis")
private Date updateTime;
}
4.构建repository
package com.example.springboot.es.repository.es;
import com.example.springboot.es.entity.es.ESBlog;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
/**
* created by <a href="mailto:18045142590@163.com" > mm</a>
*/
public interface ESBlogRepository extends ElasticsearchRepository<ESBlog, Integer> {
}
5.构建controller
package com.example.springboot.es.controller;
import com.example.springboot.es.entity.es.ESBlog;
import com.example.springboot.es.repository.es.ESBlogRepository;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* created by <a href="mailto:18045142590@163.com" > mm</a>
*/
@RestController
@AllArgsConstructor
@RequestMapping("/es")
public class ESController {
private final ESBlogRepository esBlogRepository;
@GetMapping("/test")
public ESBlog test() {
return esBlogRepository.findAll().iterator().next();
}
}
6.接口测试http://127.0.0.1:8081/es/test