一个简单应用,在jdk1.5,Lucene2.0版本下通过,正常运行。
一共3个文件
Constants.java用于存放常量
LuceneIndex.java用于建立索引
LuceneSearch.java用于搜索
package testlucene;
public class Constants {
//要索引的文件的存放路径
public final static String INDEX_FILE_PATH = "c:\\test";
//索引的存放位置
public final static String INDEX_STORE_PATH = "c:\\index";
}
package testlucene;
import java.io.*;
import java.util.*;
import org.apache.lucene.analysis.standard.*;
import org.apache.lucene.document.*;
import org.apache.lucene.index.*;
public class LuceneIndex {
private IndexWriter writer = null;
public LuceneIndex(){
try {
writer = new IndexWriter(Constants.INDEX_STORE_PATH,
new StandardAnalyzer(),true);
}catch(Exception e){
e.printStackTrace();
}
}
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(new Field("contents",reader));
doc.add(new Field("path",f.getAbsolutePath(),Field.Store.YES,Field.Index.TOKENIZED));
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);
Document doc = getDocument(file);
System.out.println("正在建立索引: " + file + " ");
writer.addDocument(doc);
}
}
}
public void close()throws Exception{
writer.close();
}
public static void main(String[] args)throws Exception{
LuceneIndex indexer = new LuceneIndex();
Date start = new Date();
indexer.writeToIndex();
Date end = new Date();
System.out.println("建立索引用时 " + (end.getTime() - start.getTime()) + "毫秒");
indexer.close();
}
}
package testlucene;
import java.util.*;
import org.apache.lucene.analysis.standard.*;
import org.apache.lucene.index.*;
import org.apache.lucene.document.*;
import org.apache.lucene.queryParser.*;
import org.apache.lucene.search.*;
public class LuceneSearch {
private IndexSearcher searcher = null;
private Query query = null;
public LuceneSearch(){
try{
searcher = new IndexSearcher(IndexReader.open(Constants.INDEX_STORE_PATH));
}catch(Exception e){
e.printStackTrace();
}
}
public final Hits Search(String keyword){
System.out.println("正在检索关键字 " + keyword);
try{
query = new QueryParser("contents", new StandardAnalyzer()).parse(keyword);
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("---------------------------");
}
public static void main(String[] args) throws Exception{
LuceneSearch test = new LuceneSearch();
Hits h = null;
h = test.Search("测试");
test.printResult(h);
h = test.Search("搜索");
test.printResult(h);
h = test.Search("引擎");
test.printResult(h);
}
}