1、先到网上下载lucene的jar包
2、设置classpath或者将其放入JDK/jre/lib/ext目录自动发现
3、索引程序功能是将“C:\\j2sdk1.4.2_14\\docs”目录中的所有文本文件建立索引,并将索引存放在“C:\\temp”,这些目录皆可以自己定义:
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import java.io.File;
import java.io.IOException;
import java.io.FileReader;
import java.util.Date;
/**
* This code was originally written for
* Erik's Lucene intro java.net article
*/
public class Indexer
{
public static void main(String[] args) throws Exception
{
File indexDir = new File("C:\\temp"); //Create Lucene index in this directory
File dataDir = new File("C:\\j2sdk1.4.2_14\\docs"); //Index files in this directory
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");
}
public static int index(File indexDir, File dataDir) throws IOException
{
IndexWriter writer = new IndexWriter(indexDir, new StandardAnalyzer(),
true);
//indexDir表示索引的存放地点
//new StandardAnalyzer()表示分析器
//true表示如已有索引则覆盖之
writer.setUseCompoundFile(false);
//表示生成多文件索引文件(默认为复合索引)
indexDirectory(writer, dataDir);
int numIndexed = writer.docCount();
writer.optimize();
writer.close();
return numIndexed;
}
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;
if (f.isDirectory())
{
indexDirectory(writer, f); // recurse
}
else if (f.getName().endsWith(".txt"))
{
indexFile(writer, f);
}
}
}
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(Field.Text("contents", new FileReader(f)));
doc.add(Field.Keyword("filename", f.getCanonicalPath()));
writer.addDocument(doc);
}
}
4、搜索程序为:
import org.apache.lucene.document.Document;
import org.apache.lucene.search.*;
import org.apache.lucene.index.*;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.Directory;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import java.io.File;
import java.util.Date;
public class SearchExec
{
public static void main(String[] args) throws Exception
{
File indexDir = new File("c:\\temp");
Directory fsDir = FSDirectory.getDirectory(indexDir, false);//if true, create, or erase any existing contents
//打开索引
IndexSearcher is = new IndexSearcher(fsDir);
//解析查询词语
//Query query = new TermQuery(new Term("contents", "java"));
//Query query = new TermQuery(new Term("contents", "application"));
Query query = QueryParser.parse("java AND NOT application", "contents", new StandardAnalyzer());
//搜索索引
Hits hits = is.search(query);
for (int i = 0; i < hits.length(); i++)
{
Document doc = hits.doc(i);
System.out.println(doc.get("filename"));
}
}
}
[此贴子已经被作者于2010-12-14 09:27:29编辑过]