lucene原始碼分析---1
阿新 • • 發佈:2019-01-09
lucene原始碼分析—例項
本章開始分析lucene的原始碼,版本為目前最新的6.1.0,下面先看一段常見的lucene建立索引和進行搜尋的例項,
建立索引例項:
String filePath = ...//檔案路徑
String indexPath = ...//索引路徑
File fileDir = new File(filePath);
Directory dir = FSDirectory.open(Paths.get(indexPath));
Analyzer luceneAnalyzer = new StandardAnalyzer();
IndexWriterConfig iwc = new IndexWriterConfig(luceneAnalyzer);
iwc.setOpenMode(OpenMode.CREATE);
IndexWriter indexWriter = new IndexWriter(dir,iwc);
File[] textFiles = fileDir.listFiles();
for (int i = 0; i < textFiles.length; i++) {
if (textFiles[i].isFile()) {
String temp = FileReaderAll(textFiles[i].getCanonicalPath(),
"GBK");
Document document = new Document();
Field FieldPath = new StringField("path" , textFiles[i].getPath(), Field.Store.YES);
Field FieldBody = new TextField("body", temp, Field.Store.YES);
document.add(FieldPath);
document.add(FieldBody);
indexWriter.addDocument(document);
}
}
indexWriter.close();
其中,FileReaderAll函式用來從檔案中讀取字串。
搜尋例項:
String indexPath=...//索引路徑
IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get(indexPath)));
IndexSearcher searcher=new IndexSearcher(reader);
ScoreDoc[] hits=null;
String queryString=...//關鍵字串
Query query=null;
Analyzer analyzer= new StandardAnalyzer();
try {
QueryParser qp=new QueryParser("body",analyzer);
query=qp.parse(queryString);
} catch (ParseException e) {
}
if (searcher!=null) {
TopDocs results=searcher.search(query, 10);
hits=results.scoreDocs;
Document document=null;
for (int i = 0; i < hits.length; i++) {
document=searcher.doc(hits[i].doc);
String body=document.get("body");
String path=document.get("path");
String modifiedtime=document.get("modifiField");
}
reader.close();
}
後面的章節就會開始分析這兩個例項究竟做了哪些工作,以及探究lucene背後的原理。