Spring轻松整合ElasticSearch

Posted 清泉白石

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring轻松整合ElasticSearch相关的知识,希望对你有一定的参考价值。

完整项目代码地址(https://github.com/fonxian/spring-elasticsearch-example/tree/master/spring-elasticsearch-example)

一、整合过程

引入依赖

	<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>


    <dependencies>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-elasticsearch</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

添加配置


server.port=8085

spring.data.elasticsearch.cluster-nodes = bei1:9300
elasticsearch.cluster.name=coffe-elasticsearch

创建实体类和数据访问类

实体类


@Document(indexName = "book",type = "book")
public class Book {
    @Id
    private String id;
    private String name;
    private Long price;
    @Version
    private Long version;

    public Map<Integer, Collection<String>> getBuckets() {
        return buckets;
    }

    public void setBuckets(Map<Integer, Collection<String>> buckets) {
        this.buckets = buckets;
    }

    @Field(type = FieldType.Nested)
    private Map<Integer, Collection<String>> buckets = new HashMap();

    public Book(){}

    public Book(String id, String name,Long version) {
        this.id = id;
        this.name = name;
        this.version = version;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Long getPrice() {
        return price;
    }

    public void setPrice(Long price) {
        this.price = price;
    }

    public long getVersion() {
        return version;
    }

    public void setVersion(long version) {
        this.version = version;
    }
}

数据访问类


@Component
public interface BookRepository extends ElasticsearchRepository<Book,String> {

    Page<Book> findByNameAndPrice(String name, Long price, Pageable pageable);
    Page<Book> findByNameOrPrice(String name, Long price, Pageable pageable);
    Page<Book> findByName(String name, Pageable pageable);

}

二、单元测试、测试整合结果

使用单元测试测试使用结果

创建测试类


@SpringBootTest
@RunWith(SpringRunner.class)
public class BookRepositoryTest {

    @Autowired
    private BookRepository repository;
    @Autowired
    private ElasticsearchTemplate esTemplate;
    
}

(1)添加文档



    /**
     * 插入文档
     */
    @Test
    public void indexBook() {

        Book book = new Book();
        book.setId("123456");
        book.setName("瓦尔登湖");
        book.setPrice(20L);
        book.setVersion(1L);
        repository.save(book);

        Book book2 = new Book();
        book2.setId("234567");
        book2.setName("Java编程思想");
        book2.setPrice(88L);
        book2.setVersion(1L);
        repository.save(book2);

        Book book3 = new Book();
        book3.setId("8910");
        book3.setName("程序员的自我修养");
        book3.setPrice(56L);
        book3.setVersion(1L);
        repository.save(book3);

    }



(2)查询所有文档

    /**
     * 获取所有文档
     */
    @Test
    public void getAll() {
        repository.findAll().forEach(book -> {
            System.out.println(book.getName());
            System.out.println(book.getPrice());
        });
    }

运行结果

(3)使用查询条件


	/**
     * 使用查询条件
     */
    @Test
    public void queryByNameOrPrice() {
        Page<Book> books = repository.findByNameOrPrice("瓦尔登湖", 56L, Pageable.unpaged());
        books.forEach(book -> {
            System.out.println(book.getName());
            System.out.println(book.getPrice());
        });
    }


运行结果

(4)使用原生方式查询


    /**
     * 原生方式查询字段
     */
    @Test
    public void queryByName() {

        QueryBuilder queryBuilder = new QueryStringQueryBuilder("修养").field("name");
        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(queryBuilder)
                .build();
        Page<Book> bookPage = esTemplate.queryForPage(searchQuery, Book.class);
        bookPage.getContent().forEach(book -> {
            System.out.println(book.getName());
        });

    }


运行结果

参考文档

spring-data-elasticsearch

以上是关于Spring轻松整合ElasticSearch的主要内容,如果未能解决你的问题,请参考以下文章

Spring整合ElasticSearch

Spring Boot 整合 Elasticsearch,实现 function score query 权重分查询

Spring Boot 整合 Elasticsearch,实现 function score query 权重分查询

springboot检索之整合elasticsearch并使用spring-data操作

spring boot 整合Elasticsearch

elasticSearch+spring 整合 maven依赖详解