Lucene是一个完全用Java编写的高性能、全功能的文本搜索引擎库.

常用的查询方式介绍:

TermQuery:按指定词条(字段)查询

BooleanQuery:按结果的组合的Boolean运算查询(关系:BooleanClause.Occur.SHOULD/MUST/MUST_NOT)

RangeQuery:范围查询,RangeQuery(startTerm,endTerm,true);

lucene入门实例

importjava.io.File;

importjava.io.FileReader;

importjava.io.Reader;

importjava.util.Date;

importorg.apache.lucene.analysis.Analyzer;

importorg.apache.lucene.analysis.standard.StandardAnalyzer;

importorg.apache.lucene.document.Document;

importorg.apache.lucene.document.Field;

importorg.apache.lucene.index.IndexWriter;

importorg.apache.lucene.index.Term;

importorg.apache.lucene.search.Hits;

importorg.apache.lucene.search.IndexSearcher;

importorg.apache.lucene.search.TermQuery;

publicclassTestLucene{

publicstaticvoidmain(Stringargs[])throwsException{

//创建索引

//indexDiristhedirectorythathostsLucene'sindexfiles

FileindexDir=newFile("D:\\txtFileIndexer\\luceneIndex");

//dataDiristhedirectorythathoststhetextfilesthattobeindexed

FiledataDir=newFile("D:\\txtFileIndexer\\luceneData");

AnalyzerluceneAnalyzer=newStandardAnalyzer();

File[]dataFiles=dataDir.listFiles();

IndexWriterindexWriter=newIndexWriter(indexDir,luceneAnalyzer,

true);

longstartTime=newDate().getTime();

for(inti=0;i<dataFiles.length;i++){

if(dataFiles[i].isFile()

&&dataFiles[i].getName().endsWith(".txt")){

System.out.println("Indexingfile"

+dataFiles[i].getCanonicalPath());

Documentdocument=newDocument();

ReadertxtReader=newFileReader(dataFiles[i]);

Fieldfield1=newField("path",dataFiles[i]

.getCanonicalPath(),Field.Store.YES,

Field.Index.TOKENIZED);

document.add(field1);

Fieldfield2=newField("contents",txtReader);

document.add(field2);

//document.add(Field.Text("path",dataFiles[i].getCanonicalPath()));

//document.add(Field.Text("contents",txtReader));

indexWriter.addDocument(document);

}

}

indexWriter.optimize();

indexWriter.close();

longendTime=newDate().getTime();

System.out.println("Ittakes"+(endTime-startTime)

+"millisecondstocreateindexforthefilesindirectory"

+dataDir.getPath());

//查询操作

StringqueryStr="china";

//ThisisthedirectorythathoststheLuceneindex

//FileindexDir=newFile("D:\\txtFileIndexer");

//FSDirectorydirectory=FSDirectory.getDirectory(indexDir,false);

//System.out.println("********************"+directory.toString());

IndexSearchersearcher=newIndexSearcher(

"D:\\txtFileIndexer\\luceneIndex");//索引路径

//if(!indexDir.exists()){

//System.out.println("TheLuceneindexisnotexist");

//return;

//}

Termterm=newTerm("contents",queryStr.toLowerCase());

TermQueryluceneQuery=newTermQuery(term);

Hitshits=searcher.search(luceneQuery);

for(inti=0;i<hits.length();i++){

Documentdocument=hits.doc(i);

System.out.println("File:"+document.get("path"));//根据字段名获得

}

}

}

lucene核心类:

(1)Lucene软件包分析

org.apache.lucene.document

这个包提供了一些为封装要索引的文档所需要的类,比如Document,Field。这样,每一个文档最终被封装成了一个Document对象。

org.apache.lucene.analysis

这个包主要功能是对文档进行分词,因为文档在建立索引之前必须要进行分词,所以这个包的作用可以看成是为建立索引做准备工作。

org.apache.lucene.index

这个包提供了一些类来协助创建索引以及对创建好的索引进行更新。这里面有两个基础的类:IndexWriter和IndexReader,其中IndexWriter是用来创建索引并添加文档到索引中的,IndexReader是用来删除索引中的文档的。

org.apache.lucene.search

这个包提供了对在建立好的索引上进行搜索所需要的类。比如IndexSearcher和Hits,IndexSearcher定义了在指定的索引上进行搜索的方法,Hits用来保存搜索得到的结果。

(2)创建索引核心类分析

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给出了对某个目录下的文本文件建立索引的源代码。

(3)搜索核心类分析

Query

这是一个抽象类,他有多个实现,比如TermQuery,BooleanQuery,PrefixQuery.这个类的目的是把用户输入的查询字符串封装成Lucene能够识别的Query。

Term

Term是搜索的基本单位,一个Term对象有两个String类型的域组成。生成一个Term对象可以有如下一条语句来完成:Termterm=newTerm(“fieldName”,”queryWord”);其中第一个参数代表了要在文档的哪一个Field上进行查找,第二个参数代表了要查询的关键词。

TermQuery

TermQuery是抽象类Query的一个子类,它同时也是Lucene支持的最为基本的一个查询类。生成一个TermQuery对象由如下语句完成:TermQuerytermQuery=newTermQuery(newTerm(“fieldName”,”queryWord”));它的构造函数只接受一个参数,那就是一个Term对象。

IndexSearcher

IndexSearcher是用来在建立好的索引上进行搜索的。它只能以只读的方式打开一个索引,所以可以有多个IndexSearcher的实例在一个索引上进行操作。

Hits

Hits是用来保存搜索的结果的