Lucene基本適用介紹(轉自BlogJava的BlueDavy)

Lucene基本適用介紹(轉自BlogJava的BlueDavy)
 想要找一個使用Lucene的案例,怎麼也找不到,多數都是概念介紹,BlogJava的BlueDavy

老兄的文章不錯,希望以後能自己提供將Lucene整合到自己應用的案例和代碼,塞!這裏借花獻佛了!

----------------------------------------------------------------------------------------------------------

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

Lucene基本使用介紹

 

 本文的目的不在於對Lucene的概念和設計這些進行介紹,僅在於介紹怎麼樣去使用Lucene來達到自己想要的幾種常見的全文檢索的需求,如果想深入瞭解Lucene的話本文不會帶給你什麼收穫的。看完本文後想更深入的瞭解Lucene請訪問:http://lucene.apache.org

 

  一.  概述

 

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

 

二.  環境搭建

 

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

 

三.  使用說明

 

3.1.       基本概念

 

這裏介紹的主要爲在使用中經常碰到一些概念,以大家都比較熟悉的數據庫來進行類比的講解,使用Lucene進行全文檢索的過程有點類似數據庫的這個過程,table---à查詢相應的字段或查詢條件----à返回相應的記錄,首先是IndexWriter,通過它建立相應的索引表,相當於數據庫中的table,在構建此索引表時需指定的爲該索引表採用何種方式進行構建,也就是說對於其中的記錄的字段以什麼方式來進行格式的劃分,這個在Lucene中稱爲AnalyzerLucene提供了幾種環境下使用的AnalyzerSimpleAnalyzerStandardAnalyzerGermanAnalyzer等,其中StandardAnalyzer是經常使用的,因爲它提供了對於中文的支持,在表建好後我們就需要往裏面插入用於索引的記錄,在Lucene中這個稱爲Document,有點類似數據庫中table的一行記錄,記錄中的字段的添加方法,在Lucene中稱爲Field,這個和數據庫中基本一樣,對於Field Lucene分爲可被索引的,可切分的,不可被切分的,不可被索引的幾種組合類型,通過這幾個元素基本上就可以建立起索引了。在查詢時經常碰到的爲另外幾個概念,首先是QueryLucene提供了幾種經常可以用到的QueryTermQueryMultiTermQueryBooleanQueryWildcardQueryPhraseQueryPrefixQueryPhrasePrefixQueryFuzzyQueryRangeQuerySpanQueryQuery其實也就是指對於需要查詢的字段採用什麼樣的方式進行查詢,如模糊查詢、語義查詢、短語查詢、範圍查詢、組合查詢等,還有就是QueryParserQueryParser可用於創建不同的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","[email protected]"));

        doc.add(Field.Text(
"receiver","[email protected]"));

        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","[email protected]"));

        doc2.add(Field.Text(
"receiver","[email protected]"));

        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的更熟練的掌握,對於FilterSorter的使用,自己擴展實現Analyzer,自己實現Query等等,甚至可以去了解一些關於搜索引擎的技術(切詞、索引排序 etc)等等。

 
<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script>
發佈了20 篇原創文章 · 獲贊 3 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章