Package: org.apache.lucene.document
這個包提供了一些爲封裝要索引的文檔所須要的類,好比Document,Field。這樣,每個文檔最終被封裝成了一個Document對象。
Package: org.apache.lucene.analysis
這個包主要功能是對文檔進行分詞,由於文檔在創建索引以前必需要進行分詞,因此這個包的做用能夠當作是爲創建索引作準備工做。
Package: org.apache.lucene.index
這個包提供了一些類來協助建立索引以及對建立好的索引進行更新。這裏面有兩個基礎的類:IndexWriter和IndexReader,其中IndexWriter是用來建立索引並添加文檔到索引中的,IndexReader是用來刪除索引中的文檔的。
Package: org.apache.lucene.search
這個包提供了對在創建好的索引上進行搜索所須要的類。好比IndexSearcher和Hits,IndexSearcher定義了在指定的索引上進行搜索的方法,Hits用來保存搜索獲得的結果。
五種基本類簡介:
package test; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.Reader; 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.Store; import org.apache.lucene.document.TextField; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.Version; /** * 文本文件創建索引 * * @author jing * */ public class TxtFileIndexer { private static final String DATA_DIR = "E:\\luceneData";// 數據存儲位置 private static final String INDEX_DIR = "E:\\luceneIndex";// 索引文件位置 private static Directory diskDir;// 索引存儲位置 // 加載索引Directory static { try { diskDir = FSDirectory.open(new File(INDEX_DIR)); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) throws IOException { // 數據存儲位置 File dataDir = new File(DATA_DIR); // 分詞器 Analyzer luceneAnalyzer = new StandardAnalyzer(); File[] dataFiles = dataDir.listFiles(); // 索引 IndexWriter indexWriter = new IndexWriter(diskDir, new IndexWriterConfig(Version.LATEST, luceneAnalyzer)); long startTime = System.currentTimeMillis(); for (File dataFile : dataFiles) { if (dataFile.isFile() && dataFile.getName().endsWith(".txt")) { System.out.println("分詞器分詞,文件: " + dataFile.getName()); Document document = new Document(); Reader txtReader = new FileReader(dataFile); document.add(new TextField("path", dataFile.getCanonicalPath(), Store.YES)); //讀取文件內容 char[] ch = new char[10240]; while( txtReader.read(ch) != -1){ continue; } document.add(new TextField("contents", String.valueOf(ch), Store.YES)); indexWriter.addDocument(document); txtReader.close(); } } indexWriter.close(); long endTime = System.currentTimeMillis(); System.out.println("It takes " + (endTime - startTime) + " milliseconds to create index for the files in directory " + dataDir.getPath()); } }
分詞器分詞,文件:1-副本(2).txt 分詞器分詞,文件:1-副本(3).txt 分詞器分詞,文件:1-副本(4).txt 分詞器分詞,文件:1-副本(5).txt 分詞器分詞,文件:1-副本(6).txt 分詞器分詞,文件:1-副本.txt 分詞器分詞,文件:1.txt It takes 375 milliseconds to create index for the files in directory E:\luceneData
package test; import java.io.File; import java.io.IOException; import org.apache.lucene.document.Document; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.Term; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TermQuery; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; public class TxtFileSearch { private static final String INDEX_DIR = "E:\\luceneIndex";// 索引文件位置 private static Directory diskDir;// 索引存儲位置 // 加載索引Directory static { try { diskDir = FSDirectory.open(new File(INDEX_DIR)); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) throws IOException { IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(diskDir)); Term term = new Term("contents", "searcher"); TermQuery luceneQuery = new TermQuery(term); //Finds the top n hits for query ScoreDoc[] hitsAfter = searcher.search(luceneQuery, 500).scoreDocs; for(ScoreDoc sDoc : hitsAfter){ Document hitDoc = searcher.doc(sDoc.doc); System.out.println(hitDoc.get("path")); } } }