index(IndexWriter writer, File file, FileFilter filter)调用私有方法indexDirectory(IndexWriter writer, File file, FileFilter filter)完成文件的索引。
index(IndexWriter writer, File file, FileFilter filter)调用私有方法indexDirectory(IndexWriter writer, File file, FileFilter filter)完成文件的索引。
下面是IndexException异常类。
IndexException.java
package powerwind;
publicclass IndexException extends Exception {
public IndexException(String message) {
super("Throw IndexException while indexing files: " + message);
}
}
下面是FileFilterFactory类,返回一个特定的文件过滤器(FileFilter)。
FileFilterFactory.java
package powerwind;
import java.io.*;
publicclass FileFilterFactory {
/**
*静态匿名内部类
*/
privatestatic FileFilter filter = new FileFilter() {
publicboolean accept(File file) {
long len;
return file.isDirectory()||
(file.getName().endsWith(".java") &&
((len = file.length()) > 0) && len < 1024 * 1024);
}
};
publicstatic FileFilter getFilter() {
returnfilter;
}
}
main方法
/**
* main方法
*/
publicstaticvoid main(String[] args) throws Exception {
IndexJavaFiles ijf = new IndexJavaFiles();
Date start = new Date();
try {
IndexWriter writer = IndexWriterFactory.newInstance().createWriter("./index", true);
System.out.println("Indexing ...");
ijf.index(writer, new File("."), FileFilterFactory.getFilter());
System.out.println("Optimizing...");
writer.optimize();
writer.close();
Date end = new Date();
System.out.println(end.getTime() - start.getTime() + " total milliseconds");
} catch (IOException e) {
System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage());
}
}
SearchJavaFiles.java
package powerwind;
import java.io.*;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.*;
import org.apache.lucene.search.*;
publicclass SearchJavaFiles {
private IndexSearcher searcher;
private QueryParser parser;
/**
*
*@paramsearcher
*/
public SearchJavaFiles(IndexSearcher searcher) {
this.searcher = searcher;
}
/**
*
*@paramfield
*@paramanalyzer
*/
publicvoid setParser(String field, Analyzer analyzer) {
setParser(new QueryParser(field, analyzer));
}
/**
*@paramparser
*/
publicvoid setParser(QueryParser parser) {
this.parser = parser;
}
/**
*
*@paramquery
*@returnHits
*@throwsSearchException
*/
public Hits serach(Query query) throws SearchException {
try {
returnsearcher.search(query);
} catch (IOException ioe) {
thrownew SearchException(ioe.getMessage());
}
}
/**
*
*@paramqueryString
*@returnHits
*@throwsSearchException
*/
public Hits serach(String queryString) throws SearchException {
if (parser == null)
thrownew SearchException("parser is null!");
try {
returnsearcher.search(parser.parse(queryString));
} catch (IOException ioe) {
thrownew SearchException(ioe.getMessage());
} catch (ParseException pe) {
thrownew SearchException(pe.getMessage());
}
}
/**
*
*输出hits的结果,从start开始到end,不包括end
*
*@paramhits
*@paramstart
*@paramend
*@throwsSearchException
*/
publicstatic Hits display(Hits hits, int start, int end) throws SearchException {
try {
while (start < end) {
Document doc = hits.doc(start);
String path = doc.get("path");
if (path != null) {
System.out.println((start + 1) + "- " + path);
} else {
System.out.println((start + 1) + "- " + "No such path");
}
start++;
}
} catch (IOException ioe) {
thrownew SearchException(ioe.getMessage());
}
return hits;
}
查看本文来源