Lucene學習之入門

來源:互聯網
上載者:User

標籤:document   lucene   

  今天開始接觸Lucene搜尋,Lucene是一個全文檢索索引的架構,主要適用於搜尋,這裡的搜尋不同於資料庫的查詢。Lucene是建立索引然後存在你設定的路徑或者記憶體中,然後當你輸入條件的時候就會去索引檔案檢索查詢。Lucene能夠實現分詞和查詢結構高亮的功能,而且在其強大的架構下全文檢索索引的速度是比較快的。由於Lucene將資料分詞後以索引方式儲存,這就勢必會佔記憶體或空間(Lucene的索引儲存一般有檔案儲存體和記憶體儲存2種方式),我們就會將不必要的東西不儲存。而我們一般在使用Lucene和資料庫結合的時候,像主鍵這些是要存的,就有利於我們在搜尋的時候得到document對象同時也能得到儲存在裡面的主鍵,然後就可以通過主鍵擷取資料庫裡面的資料。

  接下來開始第一個Lucene程式,首先匯入lucene-core.jar的包,Lucene基本所有重要的類都在core包中。

public class Test1 {private static Directory directory;static{    directory = new RAMDirectory();}public static void main(String[] args) {    Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_35);    IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_35, analyzer);    try {        IndexWriter writer = new IndexWriter(directory, conf);        Document doc = null;        for(int i=1;i<=10;i++){            doc = new Document();            doc.add(new Field("name", "dlm"+i, Field.Store.YES, Field.Index.NOT_ANALYZED));            doc.add(new Field("pass", "wei dlm"+i, Field.Store.YES, Field.Index.ANALYZED));            writer.addDocument(doc);        }        doc = new Document();        doc.add(new Field("name", "sdlm", Field.Store.YES, Field.Index.NOT_ANALYZED));        writer.addDocument(doc);        writer.commit();        IndexReader reader = IndexReader.open(directory);        IndexSearcher searcher = new IndexSearcher(reader);        TermQuery query = new TermQuery((new Term("name","dlm1")));        TopDocs topDocs = searcher.search(query, 20);        ScoreDoc[] scoreDocs = topDocs.scoreDocs;        for(ScoreDoc s:scoreDocs){            Document d = searcher.doc(s.doc);            System.out.println(s.score+"===="+d.get("name")+"--"+d.get("pass"));        }    searcher.close();    reader.close();    writer.close();    } catch (IOException e) {        // TODO Auto-generated catch block        e.printStackTrace();    }}}

 

   Directory對象是指管理索引檔案儲存體位置的對象,RAMDirectory即表示將索引儲存在記憶體,當然程式一運行結束,索引也就不存在了。然後建立分詞器Analyzer表示分詞器,在這裡我使用的是標準分詞器StandardAnalyzer,當然不同的分詞器實現的功能也不是一樣的(StopAnalyzer停詞分詞器,WhitespaceAnalyzer空格分詞器),這些所有的分詞器都是不支援中文分詞的。然後建立IndexWriterConfig對象以此建立IndexWriter對象,IndexWriter主要用於儲存索引,所以我們傳入Directory和IndexWriterConfig。Document是存放待儲存內容和待建立的索引對象,Field.Store.YES就是前面提到的是否儲存,一一般內容不需要儲存,這裡儲存指儲存到索引檔案中,我們後面查詢出來能夠Document.get(name)擷取到儲存的值,設定NO即不能儲存就不能取出值。Field.Index.ANALYZED 進行分詞和索引,適用於文章標題、內容, Field.Index.NOT_ANALYZED 進行索引但是不分詞,如身份證、ID,適用於精確搜尋Field.Index.NOT_ANALYZED_NO_NORMS 既不分詞也不儲存normal資訊, Field.Index.ANALYZED_NO_NORMS 分詞不儲存normal資訊,Field.Index.NO 不分詞不索引。添加完後需要提交commit,不然搜尋不到索引。

   IndexReader由open(Directory)即找到索引的位置,IndexSearcher由reader擷取得到,TermQuery表示精確查詢,在這裡輸入dlm是尋找不到結果的,searcher.search(query, 20);表示尋找20條資料,topDocs.scoreDocs獲得查詢的結果集,然後遍曆結果集,searcher.doc(s.doc);s.doc表示該查詢結構所在的位置,然後得到document對象,就可以獲得儲存在索引檔案中的值了。


本文出自 “成長之路” 部落格,請務必保留此出處http://weidlm.blog.51cto.com/8147553/1878864

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.