这几天搭建一个博客系统,利用的springboot、mybatis框架搭建了一下并整合了luncene全数据库检索,定时器功能。其他不说了,看下如何整合luncene吧
1.在pom.xml中添加全文检索的jar包
<!-- lucnene核心包 -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>5.5.0</version>
</dependency>
<!-- lucnene解析包 -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queryparser</artifactId>
<version>5.5.0</version>
</dependency>
<!-- lucene 公共包分析器 -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers-common</artifactId>
<version>5.5.0</version>
</dependency>
<!-- 中文分词 -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers-smartcn</artifactId>
<version>5.5.0</version>
</dependency>
<!-- 关键词高亮显示 -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-highlighter</artifactId>
<version>5.5.0</version>
</dependency>
2.创建实体类
package com.wchstrife.entity.common;
import org.apache.lucene.store.Directory;
/**
* 创建中文索引类
* Created by T430 on 2017/8/18.
*/
public class Indexer {
private Integer ids[];//主键ID
private String titles[];//标题
private String contents[];//内容
private Directory dir;//字典
public Integer[] getIds() {
return ids;
}
public void setIds(Integer[] ids) {
this.ids = ids;
}
public String[] getTitles() {
return titles;
}
public void setTitles(String[] titles) {
this.titles = titles;
}
public String[] getContents() {
return contents;
}
public void setContents(String[] contents) {
this.contents = contents;
}
public Directory getDir() {
return dir;
}
public void setDir(Directory dir) {
this.dir = dir;
}
}
3,创建service层
package com.wchstrife.service;
import com.wchstrife.entity.article;
import com.wchstrife.entity.common.Indexer;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import java.io.IOException;
import java.util.List;
/**
* 索引Service
* Created by T430 on 2017/8/18.
*/
public interface IndexService {
Indexer getIndexer();
Ind