12月12日北京OSC源創(chuàng)會(huì) —— 開(kāi)源技術(shù)的年終盛典 ?
1、Lucene的核心jar包
lucene-core-5.2.1.jar
lucene-analyzers-common-5.2.1.jar
lucene-queryparser-5.2.1.jar
2、主要開(kāi)發(fā)包說(shuō)明
org.apache.lucene.analysis:語(yǔ)言分析器,主要用于分詞
org.apache.lucene.document:索引文檔的管理
org.apache.lucene.index:索引管理,如增、刪、改
org.apache.lucene.queryparser:查詢(xún)分析
org.apache.lucene.search:檢索管理
org.apache.lucene.store:數(shù)據(jù)存儲(chǔ)管理
org.apache.lucene.util:工具包
3、寫(xiě)入索引操作的核心類(lèi)
Directory:代表索引文檔的存儲(chǔ)位置,這是一個(gè)抽象類(lèi)有FSDirectory和RAMDirectory兩個(gè)主要子類(lèi)。前者將索引寫(xiě)入文件系統(tǒng),后者將索引文檔寫(xiě)入內(nèi)存。
Analyzer:建立索引時(shí)使用的分析器,主要子類(lèi)有StandardAnalyzer(一個(gè)漢字一個(gè)詞),還可以由第三方提供如開(kāi)源社區(qū)提供一些中文分詞器。
IndexWriterConfig:操作索引庫(kù)的配置信息
IndexWriter:建立索引的核心類(lèi),用來(lái)操作索引(增、刪、改)
Document:代表一個(gè)索引文檔
Field:代表索引文檔中存儲(chǔ)的數(shù)據(jù),新版本的Lucene進(jìn)行了細(xì)化給出了多個(gè)子類(lèi):IntField、LongField、FloatField、DoubleField、TextField、StringField等。
4、寫(xiě)入索引
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | 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(); } } |
5、查詢(xún)索引操作的核心類(lèi)
IndexReader:讀取索引的工具類(lèi),常用子類(lèi)有DirectoryReader
IndexSearch:查詢(xún)索引的核心類(lèi)
QueryParser:查詢(xún)分析器,表示從哪里查用哪個(gè)分析器
Query:代表一次查詢(xún)
TopDocs:封裝了匹配情況,比如匹配多少個(gè)
ScoreDoc:匹配的數(shù)據(jù),里面封裝了索引文檔的得分和索引ID
6、查詢(xún)索引
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | 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( "總共匹配多少個(gè):" + topDocs.totalHits); ScoreDoc[] hits = topDocs.scoreDocs; // 應(yīng)該與topDocs.totalHits相同 System.out.println( "多少條數(shù)據(jù):" + 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(); } } |
聯(lián)系客服