第一个类Searcher,主要负责遍历指定的文件系统并且索引文件中包含.txt文件
package com.biaoqi.ibs.util;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
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;
/**
* 遍历文件系统并且索引.txt文件
*
* @author XIEWEI
*
*/
public class Indexer {
public static void main(String[] args) throws Exception {
File indexDir = new File("D:\\luceneIndex");
File dataDir = new File("D:\\luceneData");
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");
}
// 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,// ① 创建Lucene索引
new StandardAnalyzer(), true);
writer.setUseCompoundFile(false);
//writer.setMergeFactor(50*10);//提高写入索引的速度
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 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();
Reader txtReader = new FileReader(f);
doc.add(new Field("path", f.getCanonicalPath(), Field.Store.YES,
Field.Index.TOKENIZED));
doc.add(new Field("contents", txtReader, Field.TermVector.YES));
writer.addDocument(doc);// ⑤ 添加片段到Lucene索引
}
}
第二个类Searcher负责为查询搜索Lucene索引
package com.biaoqi.ibs.util;
import java.io.File;
import java.util.Date;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
/**
* 查询搜索Lucene索引
*
* @author XIEWEI
*
*/
public class Searcher {
public static void main(String[] args) throws Exception {
File indexDir = new File("D:\\luceneIndex");
String q = "lucene";
if (!indexDir.exists() || !indexDir.isDirectory()) {
throw new Exception(indexDir
+ "does not exist or is not a directory.");
}
search(indexDir, q);
}
public static void search(File indexDir, String q) throws Exception {
FSDirectory directory = FSDirectory.getDirectory(indexDir);
Directory fsDir = directory;
IndexSearcher is = new IndexSearcher(fsDir); // ① 打开索引
Term term = new Term("contents", q.toLowerCase());
TermQuery luceneQuery = new TermQuery(term);
long start = new Date().getTime();
Hits hits = is.search(luceneQuery);// ③ 搜索索引
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("path"));
}
}
}
package com.biaoqi.ibs.util;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
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;
/**
* 遍历文件系统并且索引.txt文件
*
* @author XIEWEI
*
*/
public class Indexer {
public static void main(String[] args) throws Exception {
File indexDir = new File("D:\\luceneIndex");
File dataDir = new File("D:\\luceneData");
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");
}
// 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,// ① 创建Lucene索引
new StandardAnalyzer(), true);
writer.setUseCompoundFile(false);
//writer.setMergeFactor(50*10);//提高写入索引的速度
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 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();
Reader txtReader = new FileReader(f);
doc.add(new Field("path", f.getCanonicalPath(), Field.Store.YES,
Field.Index.TOKENIZED));
doc.add(new Field("contents", txtReader, Field.TermVector.YES));
writer.addDocument(doc);// ⑤ 添加片段到Lucene索引
}
}
第二个类Searcher负责为查询搜索Lucene索引
package com.biaoqi.ibs.util;
import java.io.File;
import java.util.Date;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
/**
* 查询搜索Lucene索引
*
* @author XIEWEI
*
*/
public class Searcher {
public static void main(String[] args) throws Exception {
File indexDir = new File("D:\\luceneIndex");
String q = "lucene";
if (!indexDir.exists() || !indexDir.isDirectory()) {
throw new Exception(indexDir
+ "does not exist or is not a directory.");
}
search(indexDir, q);
}
public static void search(File indexDir, String q) throws Exception {
FSDirectory directory = FSDirectory.getDirectory(indexDir);
Directory fsDir = directory;
IndexSearcher is = new IndexSearcher(fsDir); // ① 打开索引
Term term = new Term("contents", q.toLowerCase());
TermQuery luceneQuery = new TermQuery(term);
long start = new Date().getTime();
Hits hits = is.search(luceneQuery);// ③ 搜索索引
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("path"));
}
}
}