Spring Boot集成Solr全文搜索

原創(chuàng)文章,轉(zhuǎn)載請注明出處
原文博客地址

最近有個項目想針對現(xiàn)有的Mysql數(shù)據(jù)庫做全文檢索.Mysql本身的全文檢索相對較弱,所以想尋找個替代的方案。最先考慮的肯定是Lucene,先實(shí)現(xiàn)起來相對復(fù)雜,自然而然的找到了構(gòu)建在Lucene之上的Solr和Elasticsearch。Elasticsearch無疑是目前最為流行的數(shù)據(jù)檢索的框架,但我這個項目對實(shí)時性并沒有要求,且Solr方案相對簡單一些。所以最終采用了Solr作為檢索服務(wù)器.項目后端采用的是Spring boot,所以最終就涉及了如何在Spring boot中集成Solr的問題。

在Spring Boot項目中引入Solr

如果你使用Spring Initializr創(chuàng)建Spring Boot項目,那么在過程中就可以引入Solr


引入Solr

在已有項目中引入Solr,則在pom文件中加入依賴

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

配置Solr數(shù)據(jù)源

假定你已經(jīng)有一個部署好的Solr服務(wù)器,在application.properies文件中配置

application.properies

spring.data.solr.host=http://127.0.0.1:8393/solr/
spring.data.solr.repositories.enabled=true

使用spring data jpa訪問solr

假如你的solr服務(wù)器中已經(jīng)配置了一個叫做student的core,那么我們首先需要編寫一個與之對應(yīng)的實(shí)體

創(chuàng)建Student實(shí)體

Student類

package com.felix.springbootpractice.domain.entity;

import org.apache.solr.client.solrj.beans.Field;
import org.springframework.data.solr.core.mapping.SolrDocument;
import java.lang.annotation.Documented;

@SolrDocument(solrCoreName="student")
public class Student {
    @Field("id")
    Long id;
    @Field("name")
    String name;
    @Field("age")
    Integer age;
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
}
  • @SolrDocument(solrCoreName="student"),solrCoreName就是core的名稱
  • @Field("id"),用于指定對應(yīng)core中的字段名稱

創(chuàng)建Repository

StudentRepository類

@Repository
public interface StudentRepository extends SolrCrudRepository<Student,Long> {

    List<Student> findByName(String name);

    @Query("*:*")
    Page<Student> findAllWithPageable(Pageable pageable);

    @Highlight(prefix = "</highlight>",postfix = "</highlight>")
    @Query("*:*")
    HighlightPage<Student> findWithHighlight();

}

我們在StudentRepository創(chuàng)建了三個查詢方法

  • findByName,根據(jù)方法命名實(shí)現(xiàn)默認(rèn)的查詢
  • findAllWithPageable,根據(jù)@Query中的Lucene查詢語法進(jìn)行查詢,并進(jìn)行了分頁操作
  • findWithHighlight,根據(jù)@Query查詢且返回高亮數(shù)據(jù)

創(chuàng)建Service方法

StudentService類

@Service
public class StudentService {
    @Autowired
    private StudentRepository studentRepository;

    public List<Student> searchByName(String name){
      return   studentRepository.findByName(name);
    }
    public List<Student> searchWithPageable(Integer pageNum,Integer pageSize){
        PageRequest pageRequest = PageRequest.of(pageNum,pageSize);
        return studentRepository.findAllWithPageable(pageRequest).getContent();
    }
    public List<Student> searchWithHighlight(Integer pageNum,Integer pageSize){
        List<Student> result = new ArrayList<>();
        PageRequest pageRequest = PageRequest.of(pageNum,pageSize);
        HighlightPage<Student> highlightPage = studentRepository.findWithHighlight(pageRequest);
        // highlightPage.getContent(); 這個是返回不帶高亮數(shù)據(jù)
        /*
          這里處理邏輯是遍歷Highlighted數(shù)據(jù),然后把指定的字段的高亮數(shù)據(jù)替換到Entry的數(shù)據(jù)中,然后再放到返回結(jié)果列表中
         */
        for(HighlightEntry<Student>  highlightEntry : highlightPage.getHighlighted()){
            for(HighlightEntry.Highlight highlight : highlightEntry.getHighlights() ){
                if(highlight.getField().equals("name")){
                    highlightEntry.getEntity().setName("");
                    String temp = "";
                    for (String sl : highlight.getSnipplets()){
                        temp+=sl;
                    }
                    highlightEntry.getEntity().setName(temp);
                }
            }
            result.add(highlightEntry.getEntity());
        }
        return result;
    }
}

項目目錄結(jié)構(gòu)

引入Solr

至此,Spring Boot集成Solr差不多就完成了,最后Contrller調(diào)用Service就可以了.

項目代碼地址: https://github.com/felix1982/spring-boot-practice/tree/master/spring-boot-solr

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容