在上一篇搜索学习--Solr全文搜索服务器的基本使用(一)——SolrJ的使用中,我是直接使用的SolrJ来调用Solr服务器的,而对于熟悉Spring Data系列的同学一定更希望用这种规范的方式去操作它。Spring Data是支持Solr的,只需要简单的配置,我们即可像操作数据库一样操作它。
依赖
4.2.3.RELEASE
org.springframework
spring-core
${spring-version}
org.springframework
spring-beans
${spring-version}
org.springframework
spring-context
${spring-version}
org.springframework.data
spring-data-solr
1.3.1.RELEASE
org.springframework
spring-test
${spring-version}
junit
junit
4.12
test
Spring配置 spring-solr.xml
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 {
/**
* 搜索所有 //(value = "*:*", filters = {"title北京市"})
* @param page
* @return
*/
@Override
@Query(value = "*:*")
Page findAll(Pageable page);
/**
* 通过关键词搜索
* @param keywords
* @param page
* @return
*/
@Highlight(prefix = "", postfix = "")
HighlightPage 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 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了