摘要:系列文章系列一快速入門系列二使用及索引文檔的基本操作系列三查詢及高亮入門簡介地址下載地址是一個用于搜索引擎的,方便開發(fā)和診斷的可視化工具。使用作為其最低級別的搜索引擎基礎。截止,上述代碼所用的包皆為最新。
系列文章:
Lucene系列(一)快速入門
Lucene系列(二)luke使用及索引文檔的基本操作
Lucene系列(三)查詢及高亮
luke入門 簡介:github地址:https://github.com/DmitryKey/luke
下載地址:https://github.com/DmitryKey/luke/releases
Luke是一個用于Lucene/Solr/Elasticsearch 搜索引擎的,方便開發(fā)和診斷的 GUI(可視化)工具。
它有以下功能:
查看文檔并分析其內容(用于存儲字段)
在索引中搜索
執(zhí)行索引維護:索引運行狀況檢查;索引優(yōu)化(運行前需要備份)
從hdfs讀取索引
將索引或其部分導出為XML格式
測試定制的Lucene分析工具
創(chuàng)建自己的插件
luke適用的搜索引擎
Apache Lucene. 大多數(shù)情況下,luke可以打開由純Lucene生成的lucene索引。 現(xiàn)在人們做出純粹的Lucene索引嗎?
Apache Solr. Solr和Lucene共享相同的代碼庫,所以luke很自然可以打開Solr生成的Lucene索引。
Elasticsearch. Elasticsearch使用Lucene作為其最低級別的搜索引擎基礎。 所以luke也可以打開它的索引!
下載安裝與簡單使用下載安裝
1.
3.
4.
索引文檔的CRUD操作
創(chuàng)建項目并添加Maven依賴
junit junit 4.12 test org.apache.lucene lucene-core 7.2.1 org.apache.lucene lucene-queryparser 7.2.1 org.apache.lucene lucene-analyzers-common 7.2.1
我們下面要用到單元測試,所以這里我們添加了Junit單元測試的依賴(版本為4.12,2018/3/30日最新的版本)
相關測試代碼
主方法:
package lucene_index_crud; 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.StringField; import org.apache.lucene.document.TextField; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.index.Term; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.junit.Test; public class Txt1 { // 下面是測試用到的數(shù)據(jù) private String ids[] = { "1", "2", "3" }; private String citys[] = { "qingdao", "nanjing", "shanghai" }; private String descs[] = { "Qingdao is a beautiful city.", "Nanjing is a city of culture.", "Shanghai is a bustling city." }; //Directory對象 private Directory dir; }
相關測試方法編寫:
1)測試創(chuàng)建索引
/** * 創(chuàng)建索引 * @throws Exception */ @Test public void testWriteIndex() throws Exception { //寫入索引文檔的路徑 dir = FSDirectory.open(Paths.get("D:luceneindex_crudindexdata")); IndexWriter writer = getWriter(); for (int i = 0; i < ids.length; i++) { //創(chuàng)建文檔對象,文檔是索引和搜索的單位。 Document doc = new Document(); doc.add(new StringField("id", ids[i], Field.Store.YES)); doc.add(new StringField("city", citys[i], Field.Store.YES)); doc.add(new TextField("desc", descs[i], Field.Store.NO)); // 添加文檔 writer.addDocument(doc); } writer.close(); }
通過luke查看相關信息:
注意: 創(chuàng)建索引之后,后續(xù)測試方法才能正確運行。
2)測試寫入了幾個文檔:
/** * 測試寫了幾個文檔 * * @throws Exception */ @Test public void testIndexWriter() throws Exception { //寫入索引文檔的路徑 dir = FSDirectory.open(Paths.get("D:luceneindex_crudindexdata")); IndexWriter writer = getWriter(); System.out.println("寫入了" + writer.numDocs() + "個文檔"); writer.close(); }
3)測試讀取了幾個文檔:
/** * 測試讀取了幾個文檔 * * @throws Exception */ @Test public void testIndexReader() throws Exception { //寫入索引文檔的路徑 dir = FSDirectory.open(Paths.get("D:luceneindex_crudindexdata")); IndexReader reader = DirectoryReader.open(dir); System.out.println("最大文檔數(shù):" + reader.maxDoc()); System.out.println("實際文檔數(shù):" + reader.numDocs()); reader.close(); }
4)測試刪除 在合并前:
/** * 測試刪除 在合并前 * * @throws Exception */ @Test public void testDeleteBeforeMerge() throws Exception { //寫入索引文檔的路徑 dir = FSDirectory.open(Paths.get("D:luceneindex_crudindexdata")); IndexWriter writer = getWriter(); System.out.println("刪除前:" + writer.numDocs()); writer.deleteDocuments(new Term("id", "1")); writer.commit(); System.out.println("writer.maxDoc():" + writer.maxDoc()); System.out.println("writer.numDocs():" + writer.numDocs()); writer.close(); }
5)測試刪除 在合并后:
我們這里先把dataindex目錄下的文件刪除,然后運行上面的testWriteIndex() 方法之后再測試。
/** * 測試刪除 在合并后 * * @throws Exception */ @Test public void testDeleteAfterMerge() throws Exception { //寫入索引文檔的路徑 dir = FSDirectory.open(Paths.get("D:luceneindex_crudindexdata")); IndexWriter writer = getWriter(); System.out.println("刪除前:" + writer.numDocs()); writer.deleteDocuments(new Term("id", "1")); writer.forceMergeDeletes(); // 強制刪除 writer.commit(); System.out.println("writer.maxDoc():" + writer.maxDoc()); System.out.println("writer.numDocs():" + writer.numDocs()); writer.close(); }
6)測試更新操作:
我們這里先把dataindex目錄下的文件刪除,然后運行上面的testWriteIndex() 方法之后再測試。
/** * 測試更新 * * @throws Exception */ @Test public void testUpdate() throws Exception { // 寫入索引文檔的路徑 dir = FSDirectory.open(Paths.get("D:luceneindex_crudindexdata")); IndexWriter writer = getWriter(); Document doc = new Document(); doc.add(new StringField("id", "1", Field.Store.YES)); doc.add(new StringField("city", "beijing", Field.Store.YES)); doc.add(new TextField("desc", "beijing is a city.", Field.Store.NO)); writer.updateDocument(new Term("id", "1"), doc); writer.close(); }
歡迎關注我的微信公眾號:“Java面試通關手冊”(分享各種Java學習資源,面試題,以及企業(yè)級Java實戰(zhàn)項目回復關鍵字免費領?。?/strong>
Lucene我想暫時先更新到這里,僅僅這三篇文章想掌握Lucene是遠遠不夠的。另外我這里三篇文章都用的最新的jar包,Lucene更新太快,5系列后的版本和之前的有些地方還是有挺大差距的,就比如為文檔域設置權值的setBoost方法6.6以后已經(jīng)被廢除了等等。因為時間有限,所以我就草草的看了一下Lucene的官方文檔,大多數(shù)內容還是看java1234網(wǎng)站的這個視頻來學習的,然后在版本和部分代碼上做了改進。截止2018/4/1,上述代碼所用的jar包皆為最新。
最后推薦一下自己覺得還不錯的Lucene學習網(wǎng)站/博客:
官方網(wǎng)站:Welcome to Apache Lucene
Github:Apache Lucene and Solr
Lucene專欄
搜索系統(tǒng)18:lucene索引文件結構
Lucene6.6的介紹和使用
文章版權歸作者所有,未經(jīng)允許請勿轉載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉載請注明本文地址:http://hztianpu.com/yun/68950.html
摘要:系列文章系列一快速入門系列二使用及索引文檔的基本操作系列三查詢及高亮是什么在維基百科的定義是一套用于全文檢索和搜索的開放源代碼程序庫,由軟件基金會支持和提供。全面準確和快速是衡量全文檢索系統(tǒng)的關鍵指標。結果列表有相關度排序。 系列文章: Lucene系列(一)快速入門 Lucene系列(二)luke使用及索引文檔的基本操作 Lucene系列(三)查詢及高亮 Lucene是什么? Luc...
摘要:有關進行調用的進一步危害,請觀看這段有關安全漏洞的討論。索引索引基本上會復制數(shù)據(jù)庫中的信息片段,這樣有利于它迅速找到節(jié)點。不管怎樣,它都能事務性地依次通過數(shù)據(jù)庫中的所有節(jié)點。 【編者按】你會怎么選擇數(shù)據(jù)庫,是關系數(shù)據(jù)庫、XML 數(shù)據(jù)庫、資源描述框架(RDF),還是圖形數(shù)據(jù)庫?本文的第1部分深入而生動地探討了各種選擇。在第2部分,將深入介紹使用 Neo4j 的注意點。文章系國內 ITOM...
摘要:基本概念在深入解讀之前,先了解下的幾個基本概念,以及這幾個概念背后隱藏的一些東西。如圖是一個內的基本組成,內數(shù)據(jù)只是一個抽象表示,不代表其內部真實數(shù)據(jù)結構。即詞典,是根據(jù)條件查找的基本索引。 前言 Apache Lucene是一個開源的高性能、可擴展的信息檢索引擎,提供了強大的數(shù)據(jù)檢索能力。Lucene已經(jīng)發(fā)展了很多年,其功能越來越強大,架構也越來越精細。它目前不僅僅能支持全文索引,也...
閱讀 3152·2021-11-16 11:42
閱讀 3910·2021-09-08 09:36
閱讀 1068·2019-08-30 12:52
閱讀 2613·2019-08-29 14:12
閱讀 907·2019-08-29 13:53
閱讀 3770·2019-08-29 12:16
閱讀 750·2019-08-29 12:12
閱讀 2598·2019-08-29 11:16