在上一篇搜索學(xué)習(xí)--Solr全文搜索服務(wù)器的基本使用(一)——SolrJ的使用中,我是直接使用的SolrJ來調(diào)用Solr服務(wù)器的访得,而對(duì)于熟悉Spring Data系列的同學(xué)一定更希望用這種規(guī)范的方式去操作它震鹉。Spring Data是支持Solr的,只需要簡(jiǎn)單的配置传趾,我們即可像操作數(shù)據(jù)庫(kù)一樣操作它浆兰。
依賴
<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);
/**
* 通過關(guān)鍵詞搜索
* @param keywords
* @param page
* @return
*/
@Highlight(prefix = "<B>", postfix = "</B>")
HighlightPage<BlogCore> findByKeywords(String keywords, Pageable page);
}
測(cè)試使用
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("達(dá)摩院", 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);
}
}
}
運(yùn)行結(jié)果:
BlogCore{blogId=2, blogTitle='<B>達(dá)摩</B><B>院</B>超越業(yè)界龍頭', blogContent='<B>達(dá)摩</B><B>院</B>一定也必須要超越英特爾劫恒,必須超越微軟轿腺,必須超越IBM,因?yàn)槲覀兩诙皇兰o(jì)族壳,我們是有機(jī)會(huì)后發(fā)優(yōu)勢(shì)的。', createTime=Wed Dec 06 13:38:12 CST 2017, keywords='null'}
BlogCore{blogId=1, blogTitle='馬云表達(dá)愿景', blogContent='10月11日杭州云棲大會(huì)上贰您,馬云表達(dá)了對(duì)新建成的阿里巴巴全球研究<B>院</B>—阿里巴巴<B>達(dá)摩</B><B>院</B>的愿景拢操,希望<B>達(dá)摩</B><B>院</B>二十年內(nèi)成為世界第一大經(jīng)濟(jì)體,服務(wù)世界二十億人孽亲,創(chuàng)造一億個(gè)工作崗位展父。', createTime=Wed Dec 06 12:03:56 CST 2017, keywords='null'}
備注
之前已經(jīng)建立索引,這里就直接搜索keywords了