4.Lucene3.案例介绍,创建索引,查询等操作验证
案例:
| Article.java |
| package cn.toto.lucene.quickstart; ? publicclassArticle { ??privateintid; ??private Stringtitle; ??private Stringcontent; ??/** ???* @return the id ???*/ ??publicint getId() { ?????returnid; ??} ??/** ???* @param id the id to set ???*/ ?? ??publicvoid setId(int id) { ?????this.id = id; ??} ??/** ???* @return the title ???*/ ??public String getTitle() { ?????returntitle; ??} ??/** ???* @param title the title to set ???*/ ?? ??publicvoid setTitle(String title) { ?????this.title = title; ??} ??/** ???* @return the content ???*/ ??public String getContent() { ?????returncontent; ??} ??/** ???* @param content the content to set ???*/ ?? ??publicvoid setContent(String content) { ?????this.content = content; ??} } |
| 工具類Configuration.java |
| package cn.toto.lucene.util; ? import java.io.File; ? import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.Version; ? /** ?* @brief Configuration.java配置對象,提供Lucene需要??索引目錄,分詞器 ?* @attention ?* @author toto ?* @date 2014-12-7 ?* @note begin modify by涂作權(quán) ?*/ public class Configuration { ???private static Directory directory; ???private static Analyzer analyzer; ???private static Version version; ??? ???static { ??????????? try { ????????????????????? //設(shè)置磁盤目錄,表示的是本地index目錄 ???????????????????????????directory = FSDirectory.open(new File("index")); ??????????????????} catch (Exception e) { ???????????????????????????e.printStackTrace(); ??????????????????} ??????????? //表示LUCENE版本 ??????????? version = Version.LUCENE_36; ??????????? //表示使用版本 ??????????? analyzer = new StandardAnalyzer(version); ???} ??? ???//提供目錄 ???public static Directory getDirectory() ???{ ??????????? return directory; ???} ??? ???//提供分詞器 ???public static Analyzer getAnalyzer() ???{ ??????????? return analyzer; ???} ??? ???//獲取版本 ???public static Version getVersion() ???{ ??????????? return version; ???} } |
| 工具類LuceneUtils.java |
| package cn.toto.lucene.util; import java.io.IOException; ? 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.search.IndexSearcher; ? // lucene工具類 public class LuceneUtils { ????????private static IndexWriter indexWriter; ????????static { ??????????????????// 索引目錄位置 ??????????????????try { ???????????????????????????// 寫入索引 ???????????????????????????IndexWriterConfig indexWriterConfig = new IndexWriterConfig( ??????????????????????????????????????????????Configuration.getVersion(), Configuration.getAnalyzer()); ???????????????????????????indexWriter = new IndexWriter(Configuration.getDirectory(), ??????????????????????????????????????????????indexWriterConfig); ? ???????????????????????????// 綁定虛擬機退出事件,關(guān)閉IndexWriter ?????????????????????????? Runtime.getRuntime().addShutdownHook(new Thread() { ????????????????????????????????????@Override ????????????????????????????????????public void run() { ??????????????????????????????????????????????try { ???????????????????????????????????????????????????????indexWriter.close(); ??????????????????????????????????????????????} catch (CorruptIndexException e) { ???????????????????????????????????????????????????????e.printStackTrace(); ??????????????????????????????????????????????} catch (IOException e) { ???????????????????????????????????????????????????????e.printStackTrace(); ???????????????????????????????????????????? } ????????????????????????????????????} ???????????????????????????}); ??????????????????} catch (IOException e) { ???????????????????????????e.printStackTrace(); ??????????????????} ????????} ? ????????// 提供獲取IndexWriter對象 ????????public static IndexWriter getIndexWriter() { ??????????????????return indexWriter; ????????} ? ????????// 獲得查詢IndexSeacher對象 ????????public static IndexSearcher getIndexSearcher() throws Exception { ??????????????????return new IndexSearcher(IndexReader.open(Configuration.getDirectory())); ????????} } |
| LuceneTest.java |
| package cn.toto.lucene.api; ? import java.io.File; importjava.io.IOException; ? 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.Field.Store; 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.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.TopDocs; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.store.LockObtainFailedException; import org.apache.lucene.store.RAMDirectory; import org.apache.lucene.util.Version; import org.junit.Test; ? import cn.toto.lucene.quickstart.Article; import cn.toto.lucene.util.LuceneUtils; ? // API詳細分析 publicclass LuceneTest { ??@Test ??//使用LuceneUtils解決 IndexWriter并發(fā)問題 ??@SuppressWarnings("unused") ??publicvoid testLock2() { ?????IndexWriter indexWriter2 = LuceneUtils.getIndexWriter(); ?????IndexWriter indexWriter1 = LuceneUtils.getIndexWriter(); ??} ? ??@Test ??@SuppressWarnings("all") ??//使用兩個IndexWrtier報錯,鎖使用問題 ??publicvoid testLock()throws CorruptIndexException, ????????LockObtainFailedException, IOException { ?????//索引目錄位置 ?????Directory directory = FSDirectory.open(new File("index"));//當前工程index目錄 ?????//分詞器 ?????Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36); ?????//寫入索引 ?????IndexWriterConfig indexWriterConfig = new IndexWriterConfig( ???????????Version.LUCENE_36, analyzer); ?????IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig); ? ?????IndexWriterConfig indexWriterConfig2 = new IndexWriterConfig( ???????????Version.LUCENE_36, analyzer); ?????IndexWriter indexWriter2 = new IndexWriter(directory, ???????????indexWriterConfig2); ??} 上面的運行結(jié)果如下:
? ??@Test ??//測試Store和 Index ??/* ???* Store.YES 存儲、Store.NO不存儲 Index.NO不建立索引 Index.ANALYZED分詞建立索引 ???* Index.NOT_ANALYZED 不分詞建立索引Index.ANALYZED_NO_NORMS 分詞建立索引,不存放權(quán)重信息 ???* Index.NOT_ANALYZED_NO_NORMS 不分詞建立索引,不存放權(quán)重信息 ???*/ ??publicvoid testIndex()throws Exception { ?????//需要建立索引目標數(shù)據(jù) ?????Article article = new Article(); ?????article.setId(100); ?????article.setTitle("學習全文檢索"); ?????article.setContent("lucene是搜索引擎開發(fā)技術(shù),lucene并不是一個現(xiàn)成的產(chǎn)品,由Apache提供"); ? ?????//將索引數(shù)據(jù)轉(zhuǎn)換 Document對象(lucene要求) ?????Document document = new Document(); ?????document.add(new Field("id", article.getId() + "", Store.YES, ???????????Field.Index.NOT_ANALYZED));//對于id通常不分詞 ?????document.add(new Field("title", article.getTitle(), Store.YES, ???????????Field.Index.ANALYZED_NO_NORMS)); ?????document.add(new Field("content", article.getContent(), Store.YES, ???????????Field.Index.ANALYZED)); ? ?????//建立索引庫 ?????//索引目錄位置 ?????Directory directory = FSDirectory.open(new File("index"));//當前工程index目錄 ?????//分詞器 ?????Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36); ?????//寫入索引 ?????IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_36, analyzer); ?????IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig); ? ?????//將document數(shù)據(jù)寫入索引庫 ?????indexWriter.addDocument(document); ?????indexWriter.close(); ??} 上面的單元測試的結(jié)果
? ? ??@Test ??//查詢索引庫 ,查看norms效果 ??publicvoid testQuery()throws Exception { ?????//建立Query對象 ---- 根據(jù)標題 ?????String queryStrng = "檢索"; ?????Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36); ?????QueryParser queryParser = new QueryParser(Version.LUCENE_36,"title", ???????????analyzer); ?????Query query = queryParser.parse(queryStrng); ? ?????//根據(jù)Query查找 ?????Directory directory = FSDirectory.open(new File("index")); ?????IndexSearcher indexSearcher = new IndexSearcher( ???????????IndexReader.open(directory)); ?????//執(zhí)行查詢獲得滿足結(jié)果前多少條記錄 ?????TopDocs topDocs = indexSearcher.search(query, 100);//查詢滿足結(jié)果前100條數(shù)據(jù) ?????System.out.println("滿足結(jié)果記錄條數(shù):" + topDocs.totalHits); ? ?????//獲得每個結(jié)果 ?????ScoreDoc[] scoreDocs = topDocs.scoreDocs; ?????for (int i = 0; i < scoreDocs.length; i++) { ????????//打印得分 ????????System.out.println("得分:" + scoreDocs[i].score); ????????//獲得Document下標 ????????int docID = scoreDocs[i].doc; ????????Document document = indexSearcher.doc(docID); ????????System.out.println("id:" + document.get("id")); ????????System.out.println("title:" + document.get("title")); ????????System.out.println("content:" + document.get("content")); ?????} ? ?????indexSearcher.close(); ??} 上面的運行單元測試之后的結(jié)果如下:
? ??@SuppressWarnings("unused") ??@Test ??//測試路徑寫法 ??publicvoid testDirectory()throwsIOException { ?????//磁盤路徑 ?????FSDirectory.open(new File("index"));//當前工程index目錄,相對路徑 ?????FSDirectory.open(new File("d:\\index"));//絕對路徑 ?????//類路徑 WEB-INF/classes ?????FSDirectory.open(new File(LuceneTest.class.getResource("/").getFile())); ? ?????//內(nèi)存路徑 ?????Directory directory = new RAMDirectory(); ??} } |
?
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的4.Lucene3.案例介绍,创建索引,查询等操作验证的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 家常辣子肉的做法?
- 下一篇: 收割机变速箱回油怎么办?