搜索学习--Solr全文搜索服务器的基本使用(二)——Spring Data Solr的使用

在上一篇搜索学习–Solr全文搜索服务器的基本使用(一)——SolrJ的使用中,我是直接使用的SolrJ来调用Solr服务器的,而对于熟悉Spring Data系列的同学一定更希望用这种规范的方式去操作它。Spring Data是支持Solr的,只需要简单的配置,我们即可像操作数据库一样操作它。

依赖

<properties>
        <spring-version>4.2.3.RELEASE</spring-version>
     </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring-version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-solr</artifactId>
            <version>1.3.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

Spring配置 spring-solr.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:solr="http://www.springframework.org/schema/data/solr"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/solr http://www.springframework.org/schema/data/solr/spring-solr.xsd">


    <solr:solr-server id="solrServer" url="http://127.0.0.1:8983/solr/blog" timeout="5000" maxConnections="10"></solr:solr-server>
    <bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate" scope="singleton">
        <constructor-arg ref="solrServer"/>
    </bean>

    <solr:repositories base-package="top.yuyufeng.learn.lucene.solr"/>

</beans>

BlogCore.java

package top.yuyufeng.learn.lucene.solr;
/**
 * @author yuyufeng
 * @date 2017/12/6
 */

import org.springframework.data.annotation.Id;
import org.springframework.data.solr.core.mapping.Indexed;
import org.springframework.data.solr.core.mapping.SolrDocument;

import java.util.Date;

/**
 * @author yuyufeng
 */
@SolrDocument(solrCoreName = "blog")
public class BlogCore {
    @Id
    @Indexed
    public Long blogId;
    @Indexed
    public String blogTitle;
    @Indexed
    public String blogContent;
    @Indexed
    public Date createTime;
    @Indexed
    public String keywords;

    public Long getBlogId() {
        return blogId;
    }

    public void setBlogId(Long blogId) {
        this.blogId = blogId;
    }

    public String getBlogTitle() {
        return blogTitle;
    }

    public void setBlogTitle(String blogTitle) {
        this.blogTitle = blogTitle;
    }

    public String getBlogContent() {
        return blogContent;
    }

    public void setBlogContent(String blogContent) {
        this.blogContent = blogContent;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public String getKeywords() {
        return keywords;
    }

    public void setKeywords(String keywords) {
        this.keywords = keywords;
    }

    @Override
    public String toString() {
        return "BlogCore{" +
                "blogId=" + blogId +
                ", blogTitle='" + blogTitle + '\'' +
                ", blogContent='" + blogContent + '\'' +
                ", createTime=" + createTime +
                ", keywords='" + keywords + '\'' +
                '}';
    }
}

BlogCoreRepository.java

package top.yuyufeng.learn.lucene.solr;

/**
 * @author yuyufeng
 * @date 2017/12/6
 */
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.solr.core.query.result.HighlightPage;
import org.springframework.data.solr.repository.Highlight;
import org.springframework.data.solr.repository.Query;
import org.springframework.data.solr.repository.SolrCrudRepository;

/**
 * @author yuyufeng
 */
public interface BlogCoreRepository extends SolrCrudRepository<BlogCore, Long> {
    /**
     * 搜索所有   //(value = "*:*", filters = {"title北京市"})
     * @param page
     * @return
     */
    @Override
    @Query(value = "*:*")
    Page<BlogCore> findAll(Pageable page);

    /**
     * 通过关键词搜索
     * @param keywords
     * @param page
     * @return
     */
    @Highlight(prefix = "<B>", postfix = "</B>")
    HighlightPage<BlogCore> findByKeywords(String keywords, Pageable page);
}

测试使用

package top.yuyufeng.learn.lucene.solr;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.solr.core.query.result.HighlightEntry;
import org.springframework.data.solr.core.query.result.HighlightPage;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * @author yuyufeng
 * @date 2017/12/6
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring-solr.xml"})
public class BlogCoreTest {
    @Autowired
    private BlogCoreRepository blogCoreRepository;

    @Test
    public void testFindByKeywords() {
        Pageable pageable = new PageRequest(0, 10);
        HighlightPage<BlogCore> highlightPage = blogCoreRepository.findByKeywords("达摩院", pageable);
        for (int i = 0; i < highlightPage.getHighlighted().size(); i++) {
            BlogCore blogCore = highlightPage.getHighlighted().get(i).getEntity();
            for (HighlightEntry.Highlight highlight : highlightPage.getHighlighted().get(i).getHighlights()) {
                if ("blogTitle".equals(highlight.getField().getName())) {
                    blogCore.setBlogTitle(highlight.getSnipplets().get(0));
                } else if ("blogContent".equals(highlight.getField().getName())) {
                    blogCore.setBlogContent(highlight.getSnipplets().get(0));
                }
            }
            System.out.println(blogCore);
        }
    }

}

运行结果:

BlogCore{blogId=2, blogTitle=’达摩超越业界龙头’, blogContent=’达摩一定也必须要超越英特尔,必须超越微软,必须超越IBM,因为我们生于二十一世纪,我们是有机会后发优势的。’, createTime=Wed Dec 06 13:38:12 CST 2017, keywords=’null’}
BlogCore{blogId=1, blogTitle=’马云表达愿景’, blogContent=’10月11日杭州云栖大会上,马云表达了对新建成的阿里巴巴全球研究—阿里巴巴达摩的愿景,希望达摩二十年内成为世界第一大经济体,服务世界二十亿人,创造一亿个工作岗位。’, createTime=Wed Dec 06 12:03:56 CST 2017, keywords=’null’}

备注

之前已经建立索引,这里就直接搜索keywords了

相关文章
相关标签/搜索