kingpub

          海內存知己,博客若比鄰

           

          Lucene基本使用介紹

          今天用了下Lucene,發現網上雖然也有不少介紹它的文檔,不過很多都偏向介紹概念呀、設計或者是一些更為深入的東西,對于其入門使用的介紹性的文檔并不多,就寫了這么一篇。

          Lucene 基本使用介紹

          ?

          本文的目的不在于對 Lucene 的概念和設計這些進行介紹,僅在于介紹怎么樣去使用 Lucene 來達到自己想要的幾種常見的全文檢索的需求,如果想深入了解 Lucene 的話本文不會帶給你什么收獲的??赐瓯疚暮笙敫钊氲牧私?/span> Lucene 請訪問:http:// lucene.apache.org

          ?

          一.? 概述

          隨著系統信息的越來越多,怎么樣從這些信息海洋中撈起自己想要的那一根針就變得非常重要了,全文檢索是通常用于解決此類問題的方案,而 Lucene 則為實現全文檢索的工具,任何應用都可通過嵌入它來實現全文檢索。

          二.? 環境搭建

          lucene.apache.org 上下載最新版本的 lucene.jar ,將此 jar 作為項目的 build path ,那么在項目中就可以直接使用 lucene 了。

          三.? 使用說明

          3.1.?????? 基本概念

          這里介紹的主要為在使用中經常碰到一些概念,以大家都比較熟悉的數據庫來進行類比的講解,使用 Lucene 進行全文檢索的過程有點類似數據庫的這個過程, table--- à 查詢相應的字段或查詢條件 ---- à 返回相應的記錄,首先是 IndexWriter ,通過它建立相應的索引表,相當于數據庫中的 table ,在構建此索引表時需指定的為該索引表采用何種方式進行構建,也就是說對于其中的記錄的字段以什么方式來進行格式的劃分,這個在 Lucene 中稱為 Analyzer , Lucene 提供了幾種環境下使用的 Analyzer SimpleAnalyzer 、 StandardAnalyzer GermanAnalyzer 等,其中 StandardAnalyzer 是經常使用的,因為它提供了對于中文的支持,在表建好后我們就需要往里面插入用于索引的記錄,在 Lucene 中這個稱為 Document ,有點類似數據庫中 table 的一行記錄,記錄中的字段的添加方法,在 Lucene 中稱為 Field ,這個和數據庫中基本一樣,對于 Field Lucene 分為可被索引的,可切分的,不可被切分的,不可被索引的幾種組合類型,通過這幾個元素基本上就可以建立起索引了。在查詢時經常碰到的為另外幾個概念,首先是 Query Lucene 提供了幾種經??梢杂玫降?/span> Query TermQuery MultiTermQuery BooleanQuery 、 WildcardQuery 、 PhraseQuery PrefixQuery 、 PhrasePrefixQuery 、 FuzzyQuery 、 RangeQuery 、 SpanQuery , Query 其實也就是指對于需要查詢的字段采用什么樣的方式進行查詢,如模糊查詢、語義查詢、短語查詢、范圍查詢、組合查詢等,還有就是 QueryParser , QueryParser 可用于創建不同的 Query ,還有一個 MultiFieldQueryParser 支持對于多個字段進行同一關鍵字的查詢, IndexSearcher 概念指的為需要對何目錄下的索引文件進行何種方式的分析的查詢,有點象對數據庫的哪種索引表進行查詢并按一定方式進行記錄中字段的分解查詢的概念,通過 IndexSearcher 以及 Query 即可查詢出需要的結果, Lucene 返回的為 Hits . 通過遍歷 Hits 可獲取返回的結果的 Document ,通過 Document 則可獲取 Field 中的相關信息了。

          通過對于上面在建立索引和全文檢索的基本概念的介紹希望能讓你對 Lucene 建立一定的了解。

          3.2.?????? 全文檢索需求的實現

          索引建立部分的代碼:

          private ? void ?createIndex(String?indexFilePath)?throws?Exception {

          ????????IndexWriter?iwriter
          = getWriter(indexFilePath);

          ????????Document?doc
          = new ?Document();

          ????????doc.add(Field.Keyword(
          " name " , " jerry " ));

          ????????doc.add(Field.Text(
          " sender " , " bluedavy@gmail.com " ));

          ????????doc.add(Field.Text(
          " receiver " , " google@gmail.com " ));

          ????????doc.add(Field.Text(
          " title " , " 用于索引的標題 " ));

          ????????doc.add(Field.UnIndexed(
          " content " , " 不建立索引的內容 " ));

          ????????Document?doc2
          = new ?Document();

          ????????doc2.add(Field.Keyword(
          " name " , " jerry.lin " ));

          ????????doc2.add(Field.Text(
          " sender " , " bluedavy@hotmail.com " ));

          ????????doc2.add(Field.Text(
          " receiver " , " msn@hotmail.com " ));

          ????????doc2.add(Field.Text(
          " title " , " 用于索引的第二個標題 " ));

          ????????doc2.add(Field.Text(
          " content " , " 建立索引的內容 " ));

          ????????iwriter.addDocument(doc);

          ????????iwriter.addDocument(doc2);

          ????????iwriter.optimize();

          ????????iwriter.close();

          ????}


          ????

          ????
          private ?IndexWriter?getWriter(String?indexFilePath)?throws?Exception {

          ????????boolean?append
          = true ;

          ????????File?file
          = new ?File(indexFilePath + File.separator + " segments " );

          ????????
          if (file.exists())

          ????????????append
          = false ;?

          ????????
          return ? new ?IndexWriter(indexFilePath,analyzer,append);

          ????}


          3.2.1.?????? 對于某字段的關鍵字的模糊查詢

          Query?query = new ?WildcardQuery( new ?Term( " sender " , " *davy* " ));

          ????????

          ????????Searcher?searcher
          = new ?IndexSearcher(indexFilePath);

          ????????Hits?hits
          = searcher.search(query);

          ????????
          for ?( int ?i? = ? 0 ;?i? < ?hits.length();?i ++ )? {

          ????????????System.
          out .println(hits.doc(i). get ( " name " ));

          ????????}


          3.2.2.?????? 對于某字段的關鍵字的語義查詢

          Query?query = QueryParser.parse( " 索引 " , " title " ,analyzer);

          ????????

          ????????Searcher?searcher
          = new ?IndexSearcher(indexFilePath);

          ????????Hits?hits
          = searcher.search(query);

          ????????
          for ?( int ?i? = ? 0 ;?i? < ?hits.length();?i ++ )? {

          ????????????System.
          out .println(hits.doc(i). get ( " name " ));

          ????????}


          3.2.3.?????? 對于多字段的關鍵字的查詢

          Query?query = MultiFieldQueryParser.parse( " 索引 " , new ?String[] { " title " , " content " } ,analyzer);

          ????????

          ????????Searcher?searcher
          = new ?IndexSearcher(indexFilePath);

          ????????Hits?hits
          = searcher.search(query);

          ????????
          for ?( int ?i? = ? 0 ;?i? < ?hits.length();?i ++ )? {

          ????????????System.
          out .println(hits.doc(i). get ( " name " ));

          ????????}


          3.2.4.?????? 復合查詢(多種查詢條件的綜合查詢)

          Query?query = MultiFieldQueryParser.parse( " 索引 " , new ?String[] { " title " , " content " } ,analyzer);

          ????????Query?mquery
          = new ?WildcardQuery( new ?Term( " sender " , " bluedavy* " ));

          ????????TermQuery?tquery
          = new ?TermQuery( new ?Term( " name " , " jerry " ));

          ????????

          ????????BooleanQuery?bquery
          = new ?BooleanQuery();

          ????????bquery.add(query,
          true , false );

          ????????bquery.add(mquery,
          true , false );

          ????????bquery.add(tquery,
          true , false );

          ????????

          ????????Searcher?searcher
          = new ?IndexSearcher(indexFilePath);

          ????????Hits?hits
          = searcher.search(bquery);

          ????????
          for ?( int ?i? = ? 0 ;?i? < ?hits.length();?i ++ )? {

          ????????????System.
          out .println(hits.doc(i). get ( " name " ));

          ????????}


          四.? 總結

          相信大家通過上面的說明能知道 Lucene 的一個基本的使用方法,在全文檢索時建議大家先采用語義時的搜索,先搜索出有意義的內容,之后再進行模糊之類的搜索, ^_^ ,這個還是需要根據搜索的需求才能定了, Lucene 還提供了很多其他更好用的方法,這個就等待大家在使用的過程中自己去進一步的摸索了,比如對于 Lucene 本身提供的 Query 的更熟練的掌握,對于 Filter 、 Sorter 的使用,自己擴展實現 Analyzer ,自己實現 Query 等等,甚至可以去了解一些關于搜索引擎的技術 ( 切詞、索引排序 etc) 等等。

          posted on 2006-08-15 10:03 xiaofeng 閱讀(294) 評論(0)  編輯  收藏 所屬分類: weblucene

          導航

          統計

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          收藏夾

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 郧西县| 山阴县| 玛多县| 望奎县| 吉木乃县| 广南县| 墨江| 牙克石市| 湖北省| 华宁县| 北川| 金秀| 白河县| 西昌市| 祁阳县| 潮安县| 通山县| 资阳市| 兴国县| 延吉市| 阳原县| 堆龙德庆县| 汾西县| 林芝县| 团风县| 吴桥县| 六枝特区| 浮梁县| 庄浪县| 璧山县| 榆中县| 连州市| 云龙县| 张北县| 东阳市| 泰安市| 鄂州市| 涟源市| 鲁山县| 利津县| 桐柏县|