Lucene的API接口设计的比较通用,输入输出结构都很像数据库的表==>记录==>字段,所以很多传统的应用的文件、数据库等都可以比较方便的映射到Lucene的存储结构/接口中。总体上看:可以先把Lucene当成一个支持全文索引的数据库系统
。
建立索引
为了对文档进行索引,Lucene 提供了五个基础的类,他们分别是 Document, Field, IndexWriter,
Analyzer, Directory。下面我们分别介绍一下这五个类的用途:
Document
Document 是用来描述文档的,这里的文档可以指一个 HTML 页面,一封电子邮件,或者是一个文本文件。一个
Document 对象由多个 Field 对象组成的。可以把一个 Document 对象想象成数据库中的一个记录,而每个 Field
对象就是记录的一个字段。
Field
Field 对象是用来描述一个文档的某个属性的,比如一封电子邮件的标题和内容可以用两个 Field 对象分别描述。
Analyzer
在一个文档被索引之前,首先需要对文档内容进行分词处理,这部分工作就是由 Analyzer 来做的。Analyzer
类是一个抽象类,它有多个实现。针对不同的语言和应用需要选择适合的 Analyzer。Analyzer 把分词后的内容交给
IndexWriter 来建立索引。
IndexWriter
IndexWriter 是 Lucene 用来创建索引的一个核心的类,他的作用是把一个个的 Document
对象加到索引中来。
Directory
这个类代表了 Lucene 的索引的存储的位置,这是一个抽象类,它目前有两个实现,第一个是
FSDirectory,它表示一个存储在文件系统中的索引的位置。第二个是
RAMDirectory,它表示一个存储在内存当中的索引的位置。
熟悉了建立索引所需要的这些类后,我们就开始对某个目录下面的文本文件建立索引了,清单1给出了对某个目录下的文本文件建立索引的源代码:
搜索文档
利用Lucene进行搜索就像建立索引一样也是非常方便的。在上面一部分中,我们已经为一个目录下的文本文档建立好了索引,现在我们就要在这个索引
上进行搜索以找到包含某个关键词或短语的文档。Lucene提供了几个基础的类来完成这个过程,它们分别是呢IndexSearcher,
Term, Query, TermQuery, Hits. 下面我们分别介绍这几个类的功能。
Query
这是一个抽象类,他有多个实现,比如TermQuery, BooleanQuery, PrefixQuery.
这个类的目的是把用户输入的查询字符串封装成Lucene能够识别的Query。
Term
Term是搜索的基本单位,一个Term对象有两个String类型的域组成。生成一个Term对象可以有如下一条语句来完成:Term
term = new Term(“fieldName”,”queryWord”);
其中第一个参数代表了要在文档的哪一个Field上进行查找,第二个参数代表了要查询的关键词。
TermQuery
TermQuery是抽象类Query的一个子类,它同时也是Lucene支持的最为基本的一个查询类。生成一个TermQuery对象由如下语句完成:
TermQuery termQuery = new TermQuery(new
Term(“fieldName”,”queryWord”)); 它的构造函数只接受一个参数,那就是一个Term对象。
IndexSearcher
IndexSearcher是用来在建立好的索引上进行搜索的。它只能以只读的方式打开一个索引,所以可以有多个IndexSearcher的实例在一个索引上进行操作。
Hits
Hits是用来保存搜索的结果的。
在系统的D盘的User文件夹里建两个文件夹index,file,file里新建几个文本文件1.txt,2.txt...名字可以随意起,在每个文本里写上一句话保存为utf-8格式
这个特别重要,不然搜索的时候会查找不到结果下面是建立索引的类
package test3;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Date;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
public class LuceneIndexTest {
public static void main(String[] args) throws Exception {
// 声明一个对象
LuceneIndexTest indexer = new LuceneIndexTest();
// 建立索引
Date start = new Date();
indexer.writeToIndex();
Date end = new Date();
System.out.println("建立索引用时" + (end.getTime() - start.getTime()) + "毫秒");
indexer.close();
}
public LuceneIndexTest() {
try {
writer = new IndexWriter(Constants.INDEX_STORE_PATH,
new StandardAnalyzer(), true);
} catch (Exception e) {
e.printStackTrace();
}
}
// 索引器
private IndexWriter writer = null;
// 将要建立索引的文件构造成一个Document对象,并添加一个域"content"
private Document getDocument(File f) throws Exception {
Document doc = new Document();
FileInputStream is = new FileInputStream(f);
Reader reader = new BufferedReader(new InputStreamReader(is));
doc.add(Field.Text("contents", reader));
doc.add(Field.Keyword("path", f.getAbsolutePath()));
return doc;
}
public void writeToIndex() throws Exception {
File folder = new File(Constants.INDEX_FILE_PATH);
if (folder.isDirectory()) {
String[] files = folder.list();
for (int i = 0; i < files.length; i++) {
File file = new File(folder, files[i]);
Document doc = getDocument(file);
System.out.println("正在建立索引 : " + file + "");
writer.addDocument(doc);
}
}
}
public void close() throws Exception {
writer.close();
}
}
package test3;
import java.util.Date;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
public class LuceneSearchTest {
public static void main(String[] args) throws Exception {
LuceneSearchTest test = new LuceneSearchTest();
Hits h = null;
h = test.search("法院");
test.printResult(h);
h = test.search("学院");
test.printResult(h);
h = test.search("boy");
test.printResult(h);
}
public LuceneSearchTest() {
try {
searcher = new IndexSearcher(IndexReader
.open(Constants.INDEX_STORE_PATH));
} catch (Exception e) {
e.printStackTrace();
}
}
// 声明一个IndexSearcher对象
private IndexSearcher searcher = null;
// 声明一个Query对象
private Query query = null;
public final Hits search(String keyword) {
System.out.println("正在检索关键字 : " + keyword);
try {
// 将关键字包装成Query对象
query = QueryParser.parse(keyword, "contents",
new StandardAnalyzer());
Date start = new Date();
Hits hits = searcher.search(query);
Date end = new Date();
System.out.println("检索完成,用时" + (end.getTime() - start.getTime()) + "毫秒");
return hits;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public void printResult(Hits h) {
if (h.length() == 0) {
System.out.println("对不起,没有找到您要的结果。");
}
else
{
for (int i = 0; i < h.length(); i++) {
try {
Document doc = h.doc(i);
System.out.print("这是第" + i + "个检索到的结果,文件名为:");
System.out.println(doc.get("path"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
System.out.println("--------------------------");
}
}
package test1;
public class Constants {
public final static String INDEX_FILE_PATH = "d:\\Users\\file";
public final static String INDEX_STORE_PATH = "d:\\Users\\index";
}
lucene与数据库的对比
相关推荐
Lucene入门与使用,非常简单,适合入门
lucene入门小例子
这里包含lucene的 jar包 ,API帮助文档CHM格式 ,还有一小段入门代码
lucene入门到项目开发.docx lucene入门到项目开发.docx
Lucene入门demo,lucene简单的应用
有关lucene入门学习的PPT以及算法和倒排原理
Lucene使用lucene入门[归类].pdf
全文检索lucene入门,结合全文检索原理分析lucene.帮助你更快掌握lucene
lucene入门指南 汇集网络一些精华和自己的总结
Lucene入门[参照].pdf
Lucene入门级教程,简单通俗,让你轻松上手
说明: 例子是根据lucene3.6写的,也可以说是直接copy别人的。 包括参考文章的代码,以及修改部分之后的代码
lucene 入门整理
lucene入门相关知识,包括基本介绍、简单示例、核心API介绍。
lucene入门体会
Lucene 简单入门程序实例
NULL 博文链接:https://kylinsoong.iteye.com/blog/719415
这是一本介绍lucene的书,适合初学者,也可以当做帮主文档使用