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.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.ParseException;
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.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;
publicclass TestLucene {
/**
*@paramargs
*@throwsIOException
*@throwsLockObtainFailedException
*@throwsCorruptIndexException
*/
@SuppressWarnings("deprecation")
publicstaticvoid main(String[] args) throws CorruptIndexException,
LockObtainFailedException, IOException {
/**
*创建索引
*/
// indexDir is the directory that hosts Lucene's index files
File indexDir = new File("D:\\luceneIndex");
// dataDir is the directory that hosts the text files that to be indexed
Analyzer luceneAnalyzer = new StandardAnalyzer(Version.LUCENE_36);
IndexWriter indexWriter = new IndexWriter(FSDirectory.open(indexDir),
new IndexWriterConfig(Version.LUCENE_35, luceneAnalyzer));
indexWriter.deleteAll();
File dataDir = new File("D:\\luceneData");
File[] dataFiles = dataDir.listFiles();
long startTime = new Date().getTime();
for (int i = 0; i < dataFiles.length; i++) {
if (dataFiles[i].isFile()
&& dataFiles[i].getName().endsWith(".txt")) {
System.out.println("Indexing file "
+ dataFiles[i].getCanonicalPath());
Field fieldPath = new Field("path", dataFiles[i].getAbsolutePath(),Field.Store.YES,Field.Index.ANALYZED);
Field fieldBody = new Field("body", new FileReader(dataFiles[i]));
Document document = new Document();
document.add(fieldPath);
document.add(fieldBody);
indexWriter.addDocument(document);
}
}
indexWriter.close();
long endTime = new Date().getTime();
System.out.println("It takes " + (endTime - startTime)
+ " milliseconds to create index for the files in directory "
+ dataDir.getPath());
/**
*搜索
*/
FSDirectory directory = FSDirectory.open(indexDir);
IndexReader reader = null;
if (reader == null) {
reader = IndexReader.open(directory);
} else {
IndexReader ir = IndexReader.openIfChanged(reader);
if (ir != null) {
reader.close();
reader = ir;
}
}
/* IndexSearcher searcher = new IndexSearcher(reader);
//判断Index目录是否存在
if (!indexDir.exists()) {
System.out.println("The Lucene index is not exist");
return;
}
String queryStr = "132";
Term term = new Term("body", queryStr);
TermQuery luceneQuery = new TermQuery(term);
TopDocs topDocs = searcher.search(luceneQuery, 10);
System.out.println("总共查询了:"+topDocs.totalHits);
for(ScoreDoc sd : topDocs.scoreDocs){
Document doc=searcher.doc(sd.doc);
System.out.println("("+doc.get("id")+")"+"["+doc.get("name")+"]");
}
searcher.close();*/
search();
}
/**
*搜索
*/
publicstaticvoid search(){
Directory directory=null;
IndexReader reader=null;
IndexSearcher searcher=null;
QueryParser parser=null;
Query query=null;
TopDocs tds=null;
try {
//1.创建Director
directory=FSDirectory.open(new File("D:\\luceneIndex"));
//2.创建IndexReader
reader=IndexReader.open(directory);
//3.通过IndexReader创建IndexSearcher
searcher=new IndexSearcher(reader);
//4.创建Query
//创建parser来确定文搜索的内容,第二个参数代表搜索的域
parser=new QueryParser(Version.LUCENE_35, "body",new StandardAnalyzer(Version.LUCENE_35));
//创建query,表示搜索域为content包含mysqld的文档
query=parser.parse("157");
//5.根据search搜索并返回TopDocs
tds=searcher.search(query, 1000);
//6.根据TopDocs获取ScoreDoc
ScoreDoc[] sds=tds.scoreDocs;
for(ScoreDoc sd:sds){
//7.根据search和ScoreDoc获取具体的Document对象
Document doc=searcher.doc(sd.doc);
//8.更具Document对象获取具体的值
System.out.println(doc.get("filename")+"["+doc.get("content")+"]");
}
//9.关闭reader
reader.close();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}