Lucene是一个高性能、可伸缩的信息搜索(IR)库。它使你可以为你的应用程序添加索引和搜索能力。Lucene是用java实现的成熟的、免费的开源项目,是著名的Apache Jakarta大家庭的一员,并且基于在Apache软件许可 [ASF, License]。同样,Lucene是当前与近几年内非常流行的免费的Java信息搜索(IR)库。[Lucene in Action]
简单例子:
使用Lucene,首先要建立一个索引,有了索引我们就可以在大量的文件信息库中快速的定位到你需要的内容.使用Lucene创建索引很简单.如下所示:
package com.jacshan;
import java.io.IOException;
import java.io.File;
import java.io.FileReader;
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;
public class Index{
public static void main(String[] args)throws Exception{
if(args.length!=2){
System.out.println("使用:java "+Index.class.getName()+" 存放索引的目录 索引目标目录");
System.exit(-1);
}
File indexDir=new File(args[0]);
File dataDir=new File(args[1]);
long startDate=(new Date()).getTime();
int indexNum=index(indexDir,dateDir);
long endDate=new Date().getTime();
System.out.println("建立"+indexNum+"索引文件用时: "+(endDate-startDate));
}
public static int index(File indexDir,File dataDir)throws IOException{
if(!dataDir.exists()||!dataDir.isDirectory()){
System.out.println("目录不存在或者不是一个目录");
System.exit(-1);
}
IndexWriter writer=new IndexWriter(indexDir,new StandardAnalyzer(),true);
writer.setUseCompoundFile(false);
indexDirectory(writer,dataDir);
int numIndexed=writer.docCount();
writer.optimize();
writer.close();
return numIndexed;
}
public static void indexDirectory(IndexWriter writer,File dataDir)throws IOException{
File[] files =dataDir.listFiles();
for (int i=0;i<files.length;i++){
File f=files[i];
if(f.isDirectory()){
indexDirectory(writer,f);//递归
}
else if (f.getName().endsWith(".sql")){
indexFile(writer,f);
}
}
}
public static void indexFile(IndexWriter writer,File f)throws IOException{
if (f.isHidden()||!f.exists()||!f.canRead()){
return;
}
System.out.println("创建索引 "+f.getCanonicalPath());
Document doc=new Document();
doc.add(new Field("contents",new FileReader(f)));
doc.add(new Field("filename",f.getCanonicalPath(),Field.Store.YES,Field.Index.TOKENIZED));
writer.addDocument(doc);
}
}
创建了索引,我们就可以像用数据库一样对文章内容进行搜索:
package com.jacshan;
import java.io.File;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.search.*;
import org.apache.lucene.queryParser.*;
public class Searcher{
public static void main(String[] args)throws Exception{
if(args.length!=2){
System.out.println("使用: java "+Searcher.class.getName()+" <index dir> <auery>");
System.exit(-1);
}
String indexDir =args[0];
File f=new File(indexDir);
String q=args[1];
if(!f.exists()||!f.isDirectory()){
throw new Exception(indexDir+"does not exist or is not a directory.");
}
search(indexDir,q);
}
public static void search(String indexDir,String q)throws Exception{
IndexSearcher is=new IndexSearcher(indexDir);
QueryParser queryParser = new QueryParser("contents", new StandardAnalyzer());//这是一个分词器
Query query = queryParser.parse(q);
Hits hits =is.search(query);
for(int i=0;i<hits.length();i++){
Document doc=hits.doc(i);
System.out.println("内容是:"+doc.get("contents"));
System.out.println("文件的路径是:" + doc.get("filename"));
}
}
}
使用Lucene就这么简单,刚接触Lucene,以后有时间再研究一下它的API和原理