本教程主要讲解spring boot如何整合 spring data elasticsearch 实现elasticsearch检索引擎的整合使用。需要注意的是spring boot 1.x就算当前最新的1.5.11支持的elasticsearch版本仅为2.x。如果需要spring boot支持5.x版本的elasticsearch或者6.x版本的elasticsearch则需要将spring boot版本升级到最新的2.0.1即可。本教程所使用的elasticsearch版本为6.2。所以使用的spring boot版本将会是2.0.1
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>demo-springboot-springdata-elasticsearch</artifactId>
<packaging>jar</packaging>
<url>https://www.leftso.com</url>
<version>1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<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-web</artifactId>
</dependency>
<!--HTTP方式接入-->
<!-- <dependency>
<groupId>io.searchbox</groupId>
<artifactId>jest</artifactId>
</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Package as an executable jar/war -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
核心依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
package com.leftso.project.demo.book.model;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
@Document(indexName = "lee", type = "books")
public class Book {
@Id
private String id;
private String title;
private String author;
private String releaseDate;
public Book() {
}
public Book(String id, String title, String author, String releaseDate) {
this.id = id;
this.title = title;
this.author = author;
this.releaseDate = releaseDate;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getReleaseDate() {
return releaseDate;
}
public void setReleaseDate(String releaseDate) {
this.releaseDate = releaseDate;
}
@Override
public String toString() {
return "Book{" +
"id='" + id + '\'' +
", title='" + title + '\'' +
", author='" + author + '\'' +
", releaseDate='" + releaseDate + '\'' +
'}';
}
}
package com.leftso.project.demo.book.repository;
import com.leftso.project.demo.book.model.Book;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface BookRepository extends ElasticsearchRepository<Book, String> {
Page<Book> findByAuthor(String author, Pageable pageable);
Page<Book> findByTitle(String title, Pageable pageable);
}
package com.leftso.project.demo.book.service;
import com.leftso.project.demo.book.model.Book;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
public interface BookService {
Book save(Book book);
void delete(Book book);
Book findOne(String id);
Iterable<Book> findAll();
Page<Book> findByAuthor(String author, PageRequest pageRequest);
Page<Book> findByTitle(String title, PageRequest pageRequest);
}
package com.leftso.project.demo;
import com.leftso.project.demo.book.model.Book;
import com.leftso.project.demo.book.service.BookService;
import org.elasticsearch.client.Client;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import java.util.Map;
@SpringBootApplication
public class Application implements CommandLineRunner {
@Autowired
private ElasticsearchOperations es;
@Autowired
private BookService bookService;
public static void main(String args[]) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... args) throws Exception {
printElasticSearchInfo();
Book b1=bookService.save(new Book("1001", "这是一个Spring Boot1.0的书籍", "Rambabu Posa", "23-FEB-2017"));
Book b2=bookService.save(new Book("1002", "Apache的一本书籍", "Rambabu Posa", "13-MAR-2017"));
bookService.save(new Book("1003", "Solr是一个搜索引擎", "Rambabu Posa", "21-MAR-2017"));
//fuzzey search
// Page<Book> books = bookService.findByAuthor("Rambabu", new PageRequest(0, 10));
Page<Book> books = bookService.findByTitle("书", new PageRequest(0, 10));
books.forEach(x -> System.out.println(x));
}
//useful for debug
private void printElasticSearchInfo() {
System.out.println("--ElasticSearch-->");
Client client = es.getClient();
Map<String, String> asMap = client.settings().getAsMap();
asMap.forEach((k, v) -> {
System.out.println(k + " = " + v);
});
System.out.println("<--ElasticSearch--");
}
}
启动类实现了一个CommandLineRunner接口。该接口主要有个run方法。启动spring boot的时候会执行run方法的内容。就当测试啦。https://www.leftso.com/article/412.html