1.maven配置lucene
<properties>
<lucene.version>7.6.0</lucene.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- lucene start -->
<dependency>
<groupId> org.apache.lucene</groupId>
<artifactId>lucene-analyzers-common</artifactId>
<version> ${lucene.version}</version>
</dependency>
<dependency>
<groupId> org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version> ${lucene.version}</version>
</dependency>
<dependency>
<groupId> org.apache.lucene</groupId>
<artifactId>lucene-highlighter</artifactId>
<version> ${lucene.version}</version>
</dependency>
<dependency>
<groupId> org.apache.lucene</groupId>
<artifactId>lucene-queryparser</artifactId>
<version> ${lucene.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.magese/ik-analyzer -->
<dependency>
<groupId>com.github.magese</groupId>
<artifactId>ik-analyzer</artifactId>
<version>${lucene.version}</version>
</dependency>
<!-- lucene end -->
</dependencies>
2. ext.dic 扩展词库
stopword.dic 停用词
IKAnalyzer.cfg.xml 中文分词器
将以上三个文件放置在 src/main/resources目录下
3.测试中文分词器
import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.junit.Test;
import org.wltea.analyzer.lucene.IKAnalyzer;
public class MySearch {
@Test
public void indexCreate() throws IOException {
// TODO Auto-generated method stub
//1 将需要添加的实体构造成实体对象
Article article=new Article(1, "今天天气不错,一起出去玩玩吧","好天气");
//2 保存到数据库(此步骤暂时省略)
//3 建立索引(lucene)
Directory directory = null;
try {
directory = FSDirectory.open(Paths.get("D:/indexdocument"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //索引库目录
Analyzer analyzer=new IKAnalyzer(); //分词器
IndexWriterConfig iwc= new IndexWriterConfig(analyzer);
// >>3.1 将Article转为Document
/** Store参数说明
No 本字段的原始值不存储
YES 本字段的原始值会存在出在数据库区中
如果不存在,则搜索出来的结果中这个字段的值为null */
/**
* 自Lucene4开始 创建field对象使用不同的类型 只需要指定是否需要保存源数据 不需指定分词类别
* 之前版本的写法如下
* doc.Add(new Field("id", article.id.ToString(), Field.Store.YES, Field.Index.ANALYZED));
*/
Document doc=new Document();
doc.add(new TextField("id", article.getId().toString(), Store.YES));
doc.add(new TextField("title", article.getTitle(), Store.YES));
doc.add(new TextField("content", article.getContent(), Store.YES));
// >>3.2 保存到索引库中
IndexWriter indexWriter=new IndexWriter(directory,iwc);
indexWriter.addDocument(doc);
indexWriter.close(); //释放资源
}
@Test
public void Search() throws Exception {
//1 搜索条件
String queryCondition="天气";
//2 执行搜索(lucene)
List<Article> articles=new ArrayList<Article>();
//--------------------------搜索代码-----------------------------
Directory directory=FSDirectory.open(Paths.get("D:/indexdocument")); //索引库目录
Analyzer analyzer=new IKAnalyzer(); //分词器
//2.1 把查询字符串转为Query对象(只在title中查询)
QueryParser queryParser=new QueryParser( "title",analyzer);
Query query=queryParser.parse(queryCondition);
//2.2 执行搜索得到结果
IndexReader indexReader=DirectoryReader.open(directory);
IndexSearcher indexSearcher=new IndexSearcher(indexReader);
TopDocs topDocs= indexSearcher.search(query, 100); //返回查询出来的前n条结果
long count= topDocs.totalHits; //总结果数量
ScoreDoc[] scoreDocs=topDocs.scoreDocs; //返回前N条结果信息
//2.3 处理结果
for (int i = 0; i < scoreDocs.length; i++) {
ScoreDoc scoreDoc=scoreDocs[i];
int docId=scoreDoc.doc;
System.out.println("得分是:"+scoreDoc.score+",内部编号是:"+docId);
//根据内部编号取出真正的Document数据
Document doc=indexSearcher.doc(docId);
//将document转化为Article
Article article=new Article(Integer.parseInt(doc.get("id")), doc.get("title"), doc.get("content"));
articles.add(article);
}
//------------------------------------------------------------
//3 控制台显示结果
System.err.println("总结果数:"+count);
for (Article article : articles) {
System.out.println("查询结果为:"+article);
}
indexSearcher.getIndexReader().close();
}
}
![](/qrcode.jpg)
//此处为文章bean
public class Article {
/**
* 文章Id
*/
private Integer id;
/**
* 文章标题
*/
private String title;
/**
* 文章内容
*/
private String content;
/**
* 构造方法
* @param id
* @param title
* @param content
*/
public Article(Integer id, String title, String content) {
this.id = id;
this.title = title;
this.content = content;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "Article [id=" + id + ", title=" + title + ", content="
+ content + "]";
}
}