1,检索文件目录,创建index
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
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;
/**
* @author Clay Zhong - Email: zjclay@gmail.com
* @date Sep 7, 2008
*/
public class Indexer {
public static int index(File indexDir, File dataDir) throws IOException {
if (!dataDir.exists() || !dataDir.isDirectory()) {
throw new IOException(dataDir + " does not exists or is not a directory");
}
IndexWriter writer = new IndexWriter(indexDir, new StandardAnalyzer(), true);
writer.setUseCompoundFile(false);
indexDirectory(writer, dataDir);
int indexNbr = writer.docCount();
writer.optimize();
writer.close();
return indexNbr;
}
private static void indexDirectory(IndexWriter writer, File dir) throws IOException {
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
File f = files[i];
if (f.isDirectory()) {
indexDirectory(writer, f);
}
else {
indexFile(writer, f);
}
}
}
private static void indexFile(IndexWriter writer, File file) throws IOException {
if (!file.exists() || file.isHidden() || !file.canRead()) {
return;
}
System.out.println("Index: " + file.getCanonicalPath());
Document doc = new Document();
doc.add(new Field("path", file.getCanonicalPath(), Field.Store.COMPRESS,
Field.Index.UN_TOKENIZED));
doc.add(new Field("content", new FileReader(file)));
writer.addDocument(doc);
}
}
2,检索index目录,查找文档对象
import java.io.File;
import java.io.IOException;
import java.util.Date;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
/**
* @author Clay Zhong - Email: zjclay@gmail.com
* @date Sep 7, 2008
*/
public class Searcher {
public static void search(File indexDir, String queryString) throws IOException, ParseException {
IndexReader reader = IndexReader.open(indexDir);
org.apache.lucene.search.Searcher searcher = new IndexSearcher(reader);
QueryParser parser = new QueryParser("content", new StandardAnalyzer());
Query query = parser.parse(queryString);
long start = new Date().getTime();
Hits hits = searcher.search(query);
long end = new Date().getTime();
System.out.println("Found " + hits.length() + " document(s) (in " + (end - start)
+ " milliseconds) that matched query: '" + queryString + "'");
for (int i = 0; i < hits.length(); i++) {
Document doc = hits.doc(i);
System.out.println(hits.id(i) + " - " + hits.score(i) + " - " + doc.get("path"));
}
}
}
3,运行Demo,查看索引结果
import java.io.File;
import java.util.Date;
/**
* @author Clay Zhong - Email: zjclay@gmail.com
* @date Sep 6, 2008
*/
public class Demo {
public static void main(String[] args) {
try {
if (true) {
long start = new Date().getTime();
int indexingNbr = Indexer.index(new File("/home/clay/work/lucene/index"), new File(
"/home/clay/work/lucene/data"));
long end = new Date().getTime();
System.out.println("Indexing:" + indexingNbr + " files, took " + (end - start)
+ " milliseconds");
}
else {
Searcher.search(new File("/home/clay/work/lucene/index"), "junit");
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}