首先说明,我是这方面新的感兴趣者,现在可以称之为:菜鸟
下面是一个针对2.4版本的简单例子(官网找不到2.3版本的bin下载,郁闷中...)。代码算是看着demo一行行研读过的。
1.建立索引
package tutorial;
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;
/**
* 索引创建者
*/
public class Indexer {
public static void main(String[] args) throws IOException {
//盛放索引的目录
File indexDir = new File("I:/index");
//盛放数据文件的目录
File dataDir = new File("I:/data");
int numAdded = index(indexDir, dataDir);
System.out.println("共有:" + numAdded + " 个文件加入到索引当中");
}
/**
* 创建索引
* @param indexDir 盛放索引的目录
* @param dataDir 盛放数据文件的目录
* @return int 加入文件的个数
* @throws IOException
*/
public static int index(File indexDir, File dataDir) throws IOException {
if (!indexDir.exists() || !dataDir.isDirectory()) {
throw new IOException();
}
//第三个参数为true的意思是,如果没有该索引就创建,否则覆盖原有索引
IndexWriter writer = new IndexWriter(indexDir, new StandardAnalyzer(),
true, IndexWriter.MaxFieldLength.LIMITED);
//该属性设为true的意思是,所以文件建立的索引都融合到统一的文件中,可以把这个设为false
//看看结果会生成多少索引文件
writer.setUseCompoundFile(true);
//建立索引
indexDirectory(writer, dataDir);
int numAdded = writer.maxDoc();
writer.optimize();
writer.close();
return numAdded;
}
/**
* 给目录建立索引
* @param writer
* @param dir
* @throws IOException
*/
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().toLowerCase().endsWith(".txt")) {
indexFile(writer, f);
}
}
}
/**
* 给文件建立索引
* @param writer
* @param f
* @throws IOException
*/
public static void indexFile(IndexWriter writer, File f) throws IOException {
if (f.isHidden() || !f.exists() || !f.canRead()) {
return;
}
System.out.println("加入: " + f.getCanonicalPath());
//针对参数文件建立索引文档
Document doc = new Document();
//Field.Index.NOT_ANALYZED 文件名称 建立索引,但不分词
doc.add(new Field("filename", f.getCanonicalPath(), Field.Store.YES,
Field.Index.NOT_ANALYZED));
doc.add(new Field("contents", new FileReader(f)));
//在writer中加入此文档
writer.addDocument(doc);
}
}
2.进行查询
package tutorial;
import java.io.File;
import java.io.IOException;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
/**
* 查询者
*/
public class Searcher {
public static void main(String[] args) throws Exception {
File indexDir = new File("I:/index");
String q = "中文";
if (!indexDir.exists() || !indexDir.isDirectory()) {
throw new IOException();
}
search(indexDir, q);
}
/**
* 查询
* @param indexDir 索引目录
* @param q 查询字符串
* @throws Exception
*/
public static void search(File indexDir, String q) throws Exception {
Directory fsDir = FSDirectory.getDirectory(indexDir);
IndexSearcher searcher = new IndexSearcher(fsDir);
//建立查询分析器
QueryParser parser = new QueryParser("contents", new StandardAnalyzer());
Query query = parser.parse(q);
//100是显示队列的Size
TopDocs topDocs = searcher.search(query, 100);
ScoreDoc[] hits = topDocs.scoreDocs;
System.out.println("共有" + searcher.maxDoc() + "条索引,命中" + hits.length
+ "条");
for (int i = 0; i < hits.length; i++) {
int docId = hits[i].doc;
Document document = searcher.doc(docId);
System.out.println(docId + ":" + document.get("filename"));
}
}
}