一、Lucene的核心jar包java
lucene-core-5.2.1.jarapache
lucene-analyzers-common-5.2.1.jar工具
lucene-queryparser-5.2.1.jarthis
二、主要開發包說明spa
org.apache.lucene.analysis:語言分析器,主要用於分詞code
org.apache.lucene.document:索引文檔的管理索引
org.apache.lucene.index:索引管理,如增、刪、改內存
org.apache.lucene.queryparser:查詢分析開發
org.apache.lucene.search:檢索管理文檔
org.apache.lucene.store:數據存儲管理
org.apache.lucene.util:工具包
三、寫入索引操做的核心類
Directory:表明索引文檔的存儲位置,這是一個抽象類有FSDirectory和RAMDirectory兩個主要子類。前者將索引寫入文件系統,後者將索引文檔寫入內存。
Analyzer:創建索引時使用的分析器,主要子類有StandardAnalyzer(一個漢字一個詞),還能夠由第三方提供如開源社區提供一些中文分詞器。
IndexWriterConfig:操做索引庫的配置信息
IndexWriter:創建索引的核心類,用來操做索引(增、刪、改)
Document:表明一個索引文檔
Field:表明索引文檔中存儲的數據,新版本的Lucene進行了細化給出了多個子類:IntField、LongField、FloatField、DoubleField、TextField、StringField等。
四、寫入索引
package cn.harmel.lucene; import java.io.IOException; import java.nio.file.Paths; 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.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; public class First { public static void main(String[] args) throws IOException { Analyzer a = new StandardAnalyzer(); Directory dir = FSDirectory.open(Paths.get("./index")); IndexWriterConfig iwc = new IndexWriterConfig(a); IndexWriter iw = new IndexWriter(dir, iwc); Document doc = new Document(); doc.add(new TextField("info", "this is my first lucene test", Field.Store.YES)); iw.addDocument(doc); iw.close(); dir.close(); } }
五、查詢索引操做的核心類
IndexReader:讀取索引的工具類,經常使用子類有DirectoryReader
IndexSearch:查詢索引的核心類
QueryParser:查詢分析器,表示從哪裏查用哪一個分析器
Query:表明一次查詢
TopDocs:封裝了匹配狀況,好比匹配多少個
ScoreDoc:匹配的數據,裏面封裝了索引文檔的得分和索引ID
六、查詢索引
package cn.harmel.lucene; import java.io.IOException; import java.nio.file.Paths; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexReader; import org.apache.lucene.queryparser.classic.ParseException; import org.apache.lucene.queryparser.classic.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 Second { public static void main(String[] args) throws IOException, ParseException { Analyzer a = new StandardAnalyzer(); Directory dir = FSDirectory.open(Paths.get("./index")); IndexReader reader = DirectoryReader.open(dir); IndexSearcher is = new IndexSearcher(reader); QueryParser parser = new QueryParser("info", a); Query query = parser.parse("lucene"); TopDocs topDocs = is.search(query, 1000); System.out.println("總共匹配多少個:" + topDocs.totalHits); ScoreDoc[] hits = topDocs.scoreDocs; // 應該與topDocs.totalHits相同 System.out.println("多少條數據:" + hits.length); for (ScoreDoc scoreDoc : hits) { System.out.println("匹配得分:" + scoreDoc.score); System.out.println("文檔索引ID:" + scoreDoc.doc); Document document = is.doc(scoreDoc.doc); System.out.println(document.get("info")); } reader.close(); dir.close(); } }