虽然Lucene的源代码很庞大,很复杂,但对于使用者来说还是非常简单的,没有几行代码就可以实现完整的创建索引和检索的全过程。我们来看两个例子:
//检索数据
package org.apache.lucene.demo2;
import java.io.File;
import java.util.Date;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

public class Searcher ...{

/** *//**
* @param args
*/
public static void main(String[] args) ...{
try ...{
File indexDir = new File("test2\index");
String q = "mscomctl.ocx";
if (!indexDir.exists() || !indexDir.isDirectory()) ...{
throw new Exception(indexDir + " does not exist or is not a directory.");
}
search(indexDir, q);
} catch (Exception e) ...{
e.printStackTrace();
}
}

public static void search(File indexDir, String q) throws Exception ...{
Directory fsDir = FSDirectory.getDirectory(indexDir, false);
IndexSearcher is = new IndexSearcher(fsDir);
QueryParser parser = new QueryParser("contents", new StandardAnalyzer());
Query query = parser.parse(q);
long start = new Date().getTime();
Hits hits = is.search(query);
long end = new Date().getTime();
System.err.println("Found " + hits.length() + " document(s) (in " + (end - start) + " milliseconds) that matched query '" + q + "':");
for (int i = 0; i < hits.length(); i++) ...{
Document doc = hits.doc(i);
System.out.println(doc.get("filename"));
}
}
}
//创建索引
package org.apache.lucene.demo2;
import java.io.File;
import java.io.FileReader;
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.document.Field;
import org.apache.lucene.index.IndexWriter;

public class Inderer ...{

public static void main(String[] args) ...{
try ...{
File dataDir = new File("test2\data");
File indexDir = new File("test2\index");
long start = new Date().getTime();
int numIndexed = index(indexDir, dataDir);
long end = new Date().getTime();
System.out.println("Indexing " + numIndexed + " files took " + (end - start) + " milliseconds");
} catch (IOException e) ...{
e.printStackTrace();
}
}
// open an index and start file directory traversal
public static int index(File indexDir, File dataDir) throws IOException ...{
if (!dataDir.exists() || !dataDir.isDirectory()) ...{
throw new IOException(dataDir + " does not exist or is not a directory");
}
IndexWriter writer = new IndexWriter(indexDir, new StandardAnalyzer(), true);
writer.setUseCompoundFile(false);
indexDirectory(writer, dataDir);
int numIndexed = writer.docCount();
writer.optimize();
writer.close();
return numIndexed;
}
// recursive method that calls itself when it finds a directory
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 if (f.getName().endsWith(".txt")) ...{
indexFile(writer, f);
}
}
}
// method to actually index a file using Lucene
private static void indexFile(IndexWriter writer, File f) throws IOException ...{
if (f.isHidden() || !f.exists() || !f.canRead()) ...{
return;
}
System.out.println("Indexing " + 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.NO));
// System.out.println(doc);
writer.addDocument(doc);
}
}
发表于 @ 2007年08月15日 20:34:00 | 评论( loading... ) | 举报| 收藏