Lucene 软g包的发布形式是一?JAR 文gQ下面我们分析一下这?JAR 文g里面的主要的 JAVA 包,使读者对之有个初步的?jin)解?/p>
Package: org.apache.lucene.document
q个包提供了(jin)一些ؓ(f)装要烦(ch)引的文档所需要的c,比如 Document, Field。这P每一个文档最l被装成了(jin)一?Document 对象?/p>
Package: org.apache.lucene.analysis
q个包主要功能是Ҏ(gu)档进行分词,因ؓ(f)文档在徏立烦(ch)引之前必要q行分词Q所以这个包的作用可以看成是为徏立烦(ch)引做准备工作?/p>
Package: org.apache.lucene.index
q个包提供了(jin)一些类来协助创建烦(ch)引以?qing)对创徏好的索引q行更新。这里面有两个基的类QIndexWriter ?IndexReaderQ其?IndexWriter 是用来创建烦(ch)引ƈd文档到烦(ch)引中的,IndexReader 是用来删除烦(ch)引中的文档的?/p>
Package: org.apache.lucene.search
q个包提供了(jin)对在建立好的索引上进行搜索所需要的cR比?IndexSearcher ?Hits, IndexSearcher 定义?jin)在指定的?ch)引上q行搜烦(ch)的方法,Hits 用来保存搜烦(ch)得到的结果?/p>
Z(jin)Ҏ(gu)档进行烦(ch)引,Lucene 提供?jin)五个基的类Q他们分别是 Document, Field, IndexWriter, Analyzer, Directory。下面我们分别介l一下这五个cȝ用途:(x) Document Document 是用来描q文档的Q这里的文档可以指一?HTML 面Q一电(sh)子邮Ӟ或者是一个文本文件。一?Document 对象由多?Field 对象l成的。可以把一?Document 对象惌成数据库中的一个记录,而每?Field 对象是记录的一个字Dc(din)?/p> Field Field 对象是用来描qC个文档的某个属性的Q比如一电(sh)子邮件的标题和内容可以用两个 Field 对象分别描述?/p> Analyzer 在一个文档被索引之前Q首先需要对文档内容q行分词处理Q这部分工作是?Analyzer 来做的。Analyzer cL一个抽象类Q它有多个实现。针对不同的语言和应用需要选择适合?Analyzer。Analyzer 把分词后的内容交l?IndexWriter 来徏立烦(ch)引?/p> IndexWriter IndexWriter ?Lucene 用来创徏索引的一个核?j)的c,他的作用是把一个个?Document 对象加到索引中来?/p> Directory q个cM表了(jin) Lucene 的烦(ch)引的存储的位|,q是一个抽象类Q它目前有两个实玎ͼW一个是 FSDirectoryQ它表示一个存储在文gpȝ中的索引的位|。第二个?RAMDirectoryQ它表示一个存储在内存当中的烦(ch)引的位置?/p> 熟?zhn)了(jin)徏立?ch)引所需要的q些cdQ我们就开始对某个目录下面的文本文件徏立烦(ch)引了(jin)Q清?l出?jin)对某个目录下的文本文g建立索引的源代码?/p> |
package TestLucene; import java.io.File; import java.io.FileReader; import java.io.Reader; import java.util.Date; import org.apache.lucene.analysis.Analyzer; 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; /** * This class demonstrate the process of creating index with Lucene * for text files */ public class TxtFileIndexer { public static void main(String[] args) throws Exception{ //indexDir is the directory that hosts Lucene's index files File indexDir = new File("D:\\luceneIndex"); //dataDir is the directory that hosts the text files that to be indexed File dataDir = new File("D:\\luceneData"); Analyzer luceneAnalyzer = new StandardAnalyzer(); File[] dataFiles = dataDir.listFiles(); IndexWriter indexWriter = new IndexWriter(indexDir,luceneAnalyzer,true); long startTime = new Date().getTime(); for(int i = 0; i < dataFiles.length; i++){ if(dataFiles[i].isFile() && dataFiles[i].getName().endsWith(".txt")){ System.out.println("Indexing file " + dataFiles[i].getCanonicalPath()); Document document = new Document(); Reader txtReader = new FileReader(dataFiles[i]); document.add(Field.Text("path",dataFiles[i].getCanonicalPath())); document.add(Field.Text("contents",txtReader)); indexWriter.addDocument(document); } } indexWriter.optimize(); indexWriter.close(); long endTime = new Date().getTime(); System.out.println("It takes " + (endTime - startTime) + " milliseconds to create index for the files in directory " + dataDir.getPath()); } }
|
利用Luceneq行搜烦(ch)像建立索引一样也是非常方便的。在上面一部分中,我们已经Z个目录下的文本文档徏立好?jin)?ch)引,现在我们p在这个烦(ch)引上q行搜烦(ch)以找到包含某个关键词或短语的文档。Lucene提供?jin)几个基的类来完成这个过E,它们分别是呢IndexSearcher, Term, Query, TermQuery, Hits. 下面我们分别介绍q几个类的功能?/p> Query q是一个抽象类Q他有多个实玎ͼ比如TermQuery, BooleanQuery, PrefixQuery. q个cȝ目的是把用户输入的查询字W串装成Lucene能够识别的Query?/p> Term Term是搜索的基本单位Q一个Term对象有两个Stringcd的域l成。生成一个Term对象可以有如下一条语句来完成QTerm term = new Term(“fieldName”,”queryWord”); 其中W一个参C表了(jin)要在文档的哪一个Field上进行查找,W二个参C表了(jin)要查询的关键词?/p> TermQuery TermQuery是抽象类Query的一个子c,它同时也是Lucene支持的最为基本的一个查询类。生成一个TermQuery对象由如下语句完成:(x) TermQuery termQuery = new TermQuery(new Term(“fieldName”,”queryWord”)); 它的构造函数只接受一个参敎ͼ那就是一个Term对象?/p> IndexSearcher IndexSearcher是用来在建立好的索引上进行搜索的。它只能以只ȝ方式打开一个烦(ch)引,所以可以有多个IndexSearcher的实例在一个烦(ch)引上q行操作?/p> Hits Hits是用来保存搜索的l果的?/p> 介绍完这些搜索所必须的类之后Q我们就开始在之前所建立的烦(ch)引上q行搜烦(ch)?jin),清?l出?jin)完成搜索功能所需要的代码?/p>
在清?中,cIndexSearcher的构造函数接受一个类型ؓ(f)Directory的对象,Directory是一个抽象类Q它目前有两个子c:(x)FSDirctory和RAMDirectory. 我们的程序中传入?jin)一个FSDirctory对象作ؓ(f)其参敎ͼ代表?jin)一个存储在盘?sh)的索引的位|。构造函数执行完成后Q代表了(jin)q个IndexSearcher以只ȝ方式打开?jin)一个烦(ch)引。然后我们程序构造了(jin)一个Term对象Q通过q个Term对象Q我们指定了(jin)要在文档的内容中搜烦(ch)包含关键?#8221;lucene”的文档。接着利用q个Term对象构造出TermQuery对象q把q个TermQuery对象传入到IndexSearcher的searchҎ(gu)中进行查询,q回的结果保存在Hits对象中。最后我们用?jin)一个@环语句把搜烦(ch)到的文档的\径都打印?jin)出来。好?jin),我们的搜索应用程序已l开发完毕,怎么P利用Lucene开发搜索应用程序是不是很简单?/p> |