为了账号安全,请及时绑定邮箱和手机立即绑定

SpringBoot+SpringDataJPA+ElasticSearch实现Full-Text Search

标签:
SpringBoot

Version

  • SpringBoot 2.0.6
  • SpringDataJPA 2.0.6
  • ElasticSearch 6.4.2

Tips

dependencies:

implementation('org.springframework.boot:spring-boot-starter-data-jpa:2.0.6.RELEASE')
implementation('org.springframework.boot:spring-boot-starter-data-elasticsearch:2.0.6.RELEASE')
implementation('net.java.dev.jna:jna:4.5.2')

文档类domain/es/EsBlog.java:

package com.ncucoder.hsoblog.domain.es;

import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import javax.persistence.Id;
import java.io.Serializable;

/**
 * @author hsowan
 * @classname EsBlog
 * @description TODO
 * @date 2018/11/1 23:36
 **/
@Document(indexName = "blog", type = "blog")
public class EsBlog implements Serializable {
    private static final long serialVersionUID = -2210631390635355780L;

    @Id
    private String id;
    // 建立索引时不使用分词
    @Field(type = FieldType.Keyword)
    private String title;
    // 摘要
    @Field(type = FieldType.Keyword)
    private String summary;
    @Field(type = FieldType.Keyword)
    private String content;


    /**
     * JPA规范要求,防止直接使用
      */
    protected EsBlog(){}

    public EsBlog(String title, String summary, String content) {
        this.title = title;
        this.summary = summary;
        this.content = content;
    }

    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 getSummary() {
        return summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Override
    public String toString() {
        return "EsBlog{" +
            "id='" + id + '\'' +
            ", title='" + title + '\'' +
            ", summary='" + summary + '\'' +
            ", content='" + content + '\'' +
            '}';
    }
}

仓库类repository/es/EsBlogRepository.java:

package com.ncucoder.hsoblog.repository.es;

import com.ncucoder.hsoblog.domain.es.EsBlog;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

/**
 * EsBlog是文档类,String是EsBlog的主键类型
 * @since 2018/11/2 0:19
 * @author <a href="hsowan.github.io">hsowan</a>
 */
public interface EsBlogRepository extends ElasticsearchRepository<EsBlog, String> {


    /**
     * 分页查询博客,去重
     * @since 2018/11/2 0:23
     * @author <a href="hsowan.github.io">hsowan</a>
     */
    Page<EsBlog> findDistinctEsBlogByTitleContainingOrSummaryContainingOrContentContaining(String title, String summary, String content, Pageable pageable);
}

测试类repository/es/EsBlogRepositoryTest.java:

package com.ncucoder.hsoblog.repository.es;

import com.ncucoder.hsoblog.domain.es.EsBlog;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.test.context.junit4.SpringRunner;

import static org.assertj.core.api.Java6Assertions.assertThat;

/**
 *
 * @author <a href="https://hsowan.github.io">hsowan</a>
 * @sine 2018/11/2 0:27
 **/
@RunWith(SpringRunner.class)
@SpringBootTest
public class EsBlogRepositoryTest {

    @Autowired
    private EsBlogRepository esBlogRepository;

    /**
     * 初始化数据
     * @since 2018/11/2 0:32
     * @author <a href="hsowan.github.io">hsowan</a>
     */
    @Before
    public void initRepositoryData(){
        esBlogRepository.deleteAll();
        esBlogRepository.save(new EsBlog("登鹤雀楼", "王之涣的登鹤雀楼", "白日依山尽,黄河入海流。欲穷千里目,更上一层楼。"));
        esBlogRepository.save(new EsBlog("相思", "王维的相思", "红豆生南国,春来发几枝。愿君多采撷,此物最相思。"));
        esBlogRepository.save(new EsBlog("静夜思", "李白的静夜思", "床前明月光,疑是地上霜。举头望明月,低头思故乡。"));
    }

    @Test
    public void testEsBlogRepository(){
        // PageRequest已经不用了,改成PageRequest.of()
        Pageable pageable = PageRequest.of(0, 20);
        String title = "思";
        String summary = "相思";
        String content = "相思";
        Page<EsBlog> page = esBlogRepository.findDistinctEsBlogByTitleContainingOrSummaryContainingOrContentContaining(title, summary, content, pageable);
        // 使用JUnit断言
        assertThat(page.getTotalElements()).isEqualTo(2);
        System.out.println("Start--------------------1");
        for (EsBlog esBlog : page.getContent()){
            System.out.println(esBlog.toString());
        }
        System.out.println("End--------------------1");

        title = "相思";
        page = esBlogRepository.findDistinctEsBlogByTitleContainingOrSummaryContainingOrContentContaining(title, summary, content, pageable);

        // 使用JUnit断言
        assertThat(page.getTotalElements()).isEqualTo(1);
        System.out.println("Start--------------------2");
        for (EsBlog esBlog : page.getContent()){
            System.out.println(esBlog.toString());
        }
        System.out.println("End--------------------2");
    }
}

注意:文档类中除了主键需要@Id的注解,其他属性需要给@Field(type = FieldType.Keyword)的注解,否则查询词语或者句子的时候可能将查不到,因为默认在建立索引的时候会进行分词,使用keyword将不进行分词。

点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消