Lucene-全文索引,lucene增量索引

來源:互聯網
上載者:User

Lucene-全文索引,lucene增量索引

          最近接觸了lucene,我想也有很多人曾經聽過,於是帶著好奇心,我開始對lucene進行瞭解,給我影響最深的是它非常多的應用了索引表,這個工具之所以快是就是因為大量引用到了索引表。今天只說下我剛開始做的校曆例子,建立索引。

下面對lucene從概念上做個介紹,Lucene是一個資訊檢索的函數庫(Library),利用它你可以為你的應用加上索引和搜尋的功能.Lucene的使用者不需要深入瞭解有關全文檢索索引的知識,僅僅學會使用庫中的一個類,你就為你的應用實現全文檢索索引的功能.不過千萬別以為Lucene是一個象google那樣的搜尋引擎,Lucene甚至不是一個應用程式,它僅僅是一個工具,一個Library.你也可以把它理解為一個將索引,搜尋功能封裝的很好的一套簡單易用的API.利用這套API你可以做很多有關搜尋的事情,而且很方便.  

          那麼lucene可以做什麼呢?Lucene可以對任何的資料做索引和搜尋. Lucene不管資料來源是什麼格式,只要它能被轉化為文字的形式,就可以被Lucene所分析利用.也就是說不管是MS word, Html ,pdf還是其他什麼形式的檔案只要你可以從中抽取出文字形式的內容就可以被Lucene所用.你就可以用Lucene對它們進行索引以及搜尋. 下面是我做的一個小例子,就是一個查詢產生索引的例子:

<span style="font-size:14px;">package com.jikexueyuan.study;import java.io.File;import java.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.IntField;import org.apache.lucene.document.Field.Store;import org.apache.lucene.document.StringField;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.IndexWriterConfig;import org.apache.lucene.index.IndexWriterConfig.OpenMode;import org.apache.lucene.store.Directory;import org.apache.lucene.store.FSDirectory;import org.apache.lucene.util.Version;public class IndexCreate {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubAnalyzer analyzer=new StandardAnalyzer(Version.LUCENE_46);//StandardAnalyzer是將英文按照空格、標點符號等進行分詞,將中文按照單個字進行分詞,一個漢字算一個詞IndexWriterConfig indexWriterConfig=new IndexWriterConfig(Version.LUCENE_46,analyzer);//把寫入的檔案用指定的分詞器將文章分詞(這樣檢索的時候才能查的快),然後將詞放入索引檔案。indexWriterConfig.setOpenMode(OpenMode.CREATE_OR_APPEND);Directory directory=null;IndexWriter indexWriter=null;try {directory=FSDirectory.open(new File("E://index/test"));// //索引庫存放在這個檔案夾裡  ,Directory表示索引檔案儲存的地方,是抽象類別,兩個子類FSDirectory表示檔案中,RAMDirectory 表示儲存在記憶體中  if(indexWriter.isLocked(directory)){indexWriter.unlock(directory);}indexWriter=new IndexWriter(directory,indexWriterConfig);} catch (Exception e) {e.printStackTrace();}//Document document=new Document();Document doc = new Document();doc.add(new StringField("id","abcde", Store.YES));doc.add(new org.apache.lucene.document.TextField("content","極客學院",Store.YES));doc.add(new IntField("num",1,Store.YES));try {indexWriter.addDocument(doc);//向索引中添加文檔(Insert)} catch (Exception e) {e.printStackTrace();}Document doc1 = new Document();doc1.add(new StringField("id","sdfsd", Store.YES));doc1.add(new org.apache.lucene.document.TextField("content","Lucene案例",Store.YES));doc1.add(new IntField("num",1,Store.YES));try {indexWriter.addDocument(doc1);} catch (Exception e) {e.printStackTrace();}try {indexWriter.commit();indexWriter.close();directory.close();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}</span>


        結果會產生一系列的有關索引的檔案,如:

         從上面的例子我們可以看出建立索引需要的三個要素分別是:

          1、indexWriter

          2、Directory

          3、Anayzer

          4、Document

         5、Field

      對於lucene的分享還要繼續,希望有越來越多的人可以共同努力!

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.