Lucene 3.3 學習筆記 1 介紹

來源:互聯網
上載者:User

Lucene 3.3 學習筆記 1 Lucene 3.3 學習筆記 1 包架構
------------------------
1. Lucene介紹

   Lucene的作者說過,Lucene只是一個高效的全文檢索搜尋引擎庫,而不是一個平台,提供了非常簡單的API,使其可以滿足使用者對全文索引的需求。
   http://lucene.apache.org/java/docs/index.html
   這裡可以下載到最新的Lucene庫,其中的wiki是一個不錯的學習的地方
   http://wiki.apache.org/lucene-java/FrontPage?action=show&redirect=FrontPageEN

   目前有很多網站和軟體用到了Lucene,如社交網站LinkedIn和蘋果的ITunes軟體中都用到了Lucene來做即時與全文索引。

2. Lucene架構包介紹   2.1 文檔的一個物件模型抽象庫 - Document

      這個庫提供了索引文檔對象的一個抽象,所有的文檔對於Lucene來說都是一個Document抽象。
  Document => [Field1,Field2,...,FieldN]
  Field    => [Term1,Term2,...,TermN]
   2.2 文本解析庫 - Analysis

       這個庫是對文本進行解析,得到相應的Term對象,在Index與Searcher的時候都會用到,在索引過程中,它會對被索引的檔案進行解析,而在搜尋過程中,它會對查詢條件進行解析。
   2.3 索引庫 - Index

      它主要是用來對文檔建立索引,對索引進行維護(更新索引,最佳化索引等),為查詢提供提供者。
   2.4 存放庫 - store

      它主要是用來儲存索引內容的,這裡提供了兩種儲存方式,一種是磁碟儲存,另一個是記憶體儲存。
   2.5 查詢庫 - search

      它主要是為使用者提供查詢介面的,對使用者的查詢結果進行評估,並返回與使用者最相關的查詢資訊。

    2.6 工具庫 - util

      主要是為上面的庫提供一些I/O或者演算法方法的工具類

  

3. 簡單的例子

   當你下載了Lucene的可執行包後,會在contrib中有一個中demo的目錄,其中就是對Lucene的簡單使用,下面對其進行相應的分析。
   3.1 索引

    這裡的索引就是對文檔建立索引,以方便使用者查詢,我們看org.apache.lucene.demo.IndexFiles.java就是完成這樣的一個工作。下面我們來看一下它的主要的原始碼:

  //~1 這裡產生一個索引目錄      Directory dir = FSDirectory.open(new File(indexPath));  //~2 產生一個標準的分析器,如果你要用自己的分詞器,可以擴充Analyzer與Tokenizer類      Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_31);  //~3 這裡對索引進行配置      IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_31, analyzer);  // 是索引建立一個新的索引還是進行增量索引      if (create) {        // Create a new index in the directory, removing any        // previously indexed documents:        iwc.setOpenMode(OpenMode.CREATE);      } else {        // Add new documents to an existing index:        iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);      }// 配置索引的記憶體,以提高索引效率      // Optional: for better indexing performance, if you      // are indexing many documents, increase the RAM      // buffer.  But if you do this, increase the max heap      // size to the JVM (eg add -Xmx512m or -Xmx1g):      //      // iwc.setRAMBufferSizeMB(256.0);  //~4 產生一個索引輸出類,用於寫了索引內容      IndexWriter writer = new IndexWriter(dir, iwc);  //~5 索引文檔  // 這裡主要是讀出要索引目錄中的檔案,產生相應的抽象文檔對象,  // 然後調用IndexWriter的addDocument或者是updateDocuemtn來建立或者更新索引。      indexDocs(writer, docDir);  // 是否進行索引最佳化,  // NOTE: if you want to maximize search performance,      // you can optionally call optimize here.  This can be      // a costly operation, so generally it's only worth      // it when your index is relatively static (ie you're      // done adding documents to it):      //      // writer.optimize();  //~6 關閉索引寫出器      writer.close();

   3.2 查詢

 這裡的查詢主要是對索引好的資料進行查詢操作,在org.apache.lucene.demo.SearchFiles.java中可以看到相應的查詢代碼,部分主要代碼如一下:

  //~1 產生一個索引查詢器,輸入為索引目錄  IndexSearcher searcher = new IndexSearcher(FSDirectory.open(new File(index)));  //~2 產生一個查詢條件的分析器      Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_31);  //~3 產生一個查詢的分析器,主要是對查詢條件進行分析,產生相應的Query      QueryParser parser = new QueryParser(Version.LUCENE_31, field, analyzer);      //~4 對查詢條件進行分析,產生相應的Query對象       Query query = parser.parse(line);      System.out.println("Searching for: " + query.toString(field));      //~5 調用索引查詢對象的search方法進行索引查詢,得到查詢結果           TopDocs results = searcher.search(query, null, 100);
4. 總結

   這裡對Lucene做了一個簡單的分析,再結合索引與查詢的API調用來簡單瞭解一下其使用方法。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.