环境
jdk 1.8
es 6.2.2
springboot 2.1.4
IDE idea
下载es
springboot 依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
创建实体类
@Data 此注解需要安装lombok插件,还要导入lombok依赖包
@Document(indexName = "book",type = "_doc")
@Data
public class Book {
@Id
private String id;
private String name;
private Long maxPage;
}
创建Repository接口
public interface BookRepository extends ElasticsearchRepository<Book,String> {
Page<Book> findByName(String name, Pageable pageable);
}
添加测试数据
@Autowired
private BookRepository bookRepository;
@Test
public void contextLoads() {
for (Long i = 0L; i <100000; i++) {
Book book=new Book();
book.setMaxPage(i);
book.setName("book----"+i);
Book save = bookRepository.save(book);
System.out.println(save);
}
}
controller 测试
@Autowired
private BookRepository bookRepository;
@RequestMapping(value = "finaAll", method = RequestMethod.GET)
@ResponseBody
public List<Book> findAll() {
String name = "book";
Page<Book> byName = bookRepository.findByName(name, PageRequest.of(1, 10));
return byName.getContent();
}
url测试
到此springboot 整合es 成功,有什么不足大佬们请多多指点!_