全文检索Lucene (1)
Lucene是一个基于Java的开源全文检索框架,支持高效的大规模文本检索。本文通过一个简单的HelloWorld级别示例展示了如何使用Lucene进行站内搜索。具体步骤包括:创建索引库、添加文章数据、编写查询语句,并从索引中获取搜索结果。
依赖项包括lucene-analyzers-common-6.1.0.jar、lucene-core-6.1.0.jar等核心组件。实现过程中创建了一个包含id、标题和内容字段的文章类,并通过模拟数据展示了如何将文章添加到索引中,并通过标准分析器编写查询语句进行搜索。
最终的查询结果展示了两条匹配记录,并输出了相关文章信息。整个过程简单明了地体现了Lucene在站内搜索中的应用及其高效性。
Lucene属于Apache开源的一个基于全文检索的框架系统,并且广为人知其高效性与实用性。今天将深入探讨如何在实际项目中灵活运用这一核心组件中的关键功能模块
工作流程

依赖
如果想利用Lucene这个强大的搜索引擎工具,在项目初期必须获取并引用第三方提供的jar文件包。以下列出了一些我在项目中实际应用的jar包工具。
lucene-analyzers-common-6.1.0.jar:提供分析器功能
lucene-core-6.1.0.jar:包含全文检索核心功能
lucene-highlighter-6.1.0.jar:能够对匹配到的目标词进行高亮显示
lucene-memory-6.1.0.jar:涵盖索引存储相关功能
lucene-queries-6.1.0.jar:提供查询处理能力
lucene-queryparser-6.1.0.jar:包含查询处理模块
Lucene HelloWorld
目前启动一个名为HelloWorld的小模块。开发一个基于文章内容的查询功能。
Article.java
/** * @Date 2016年8月1日
* * @author Administrator
*/
package domain;
/** * @author 郭瑞彪
* */
public class Article {
private Integer id;
private String title;
private String content;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
@Override
public String toString() {
return "Article [id=" + id + ", title=" + title + ", content=" + content + "]";
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
创建索引库
@Test
public void createIndex() throws Exception {
// 模拟一条文章数据
Article a = new Article();
a.setId(1);
a.setTitle("全文检索");
a.setContent("我们主要是做站内搜索(或叫系统内搜索),即对系统内的资源进行搜索");
// 建立索引
Directory dir = FSDirectory.open(Paths.get("./indexDir/"));
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(new StandardAnalyzer());
IndexWriter indexWriter = new IndexWriter(dir, indexWriterConfig);
Document doc = new Document();
doc.add(new StringField("id", a.getId().toString(), Field.Store.YES));
doc.add(new TextField("title", a.getTitle(), Field.Store.YES));
doc.add(new TextField("content", a.getContent(), Field.Store.YES));
indexWriter.addDocument(doc);
indexWriter.close();
}
从索引库中获取查询结果
@Test
public void search() throws Exception {
String queryString = "资源";
Analyzer analyzer = new StandardAnalyzer();
analyzer.setVersion(Version.LUCENE_6_1_0);
QueryParser queryParser = new QueryParser("content", analyzer);
Query query = queryParser.parse(queryString);
// IndexReader indexReader =
// DirectoryReader.open(FSDirectory.open(Paths.get("./indexDir/")));
DirectoryReader directoryReader = DirectoryReader.open(FSDirectory.open(Paths.get("./indexDir/")));
IndexReader indexReader = directoryReader;
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
TopDocs topDocs = indexSearcher.search(query, 10);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
List<Article> articles = new ArrayList<Article>();
for (int i = 0; i < scoreDocs.length; i++) {
ScoreDoc scoreDoc = scoreDocs[i];
Document doc = indexSearcher.doc(scoreDoc.doc);
Article a = new Article();
a.setId(Integer.parseInt(doc.get("id")));
a.setTitle(doc.get("title"));
a.setContent(doc.get("content"));
System.out.println(a.toString());
articles.add(a);
}
// 显示结果
System.out.println("总的记录数为: " + topDocs.totalHits);
System.out.println(articles.toString());
for (Article a : articles) {
System.out.println("-----------搜索结果如下-----------------");
System.out.println(">>>id: " + a.getId());
System.out.println(">>>title:" + a.getTitle());
System.out.println(">>>content:" + a.getContent());
}
indexReader.close();
analyzer.close();
}
查询结果
总的记录数为: 4
-----------搜索结果如下-----------------
>>>id: 1
>>>title:全文检索
>>>content:我们主要是做站内搜索(或叫系统内搜索),即对系统内的资源进行搜索
-----------搜索结果如下-----------------
>>>id: 2
>>>title:全文检索2
>>>content:我们主要是做站内搜索(或叫系统内搜索),即对系统内的资源进行搜索,hahahahahhaha
总结
Lucene全文检索的功能得以轻易地实现,并且其中还有其他更多潜在的应用值得我们深入探索和利用。
