lucene簡單實例

Lucene 其實很簡單的,它最主要就是做兩件事:建立索引和進行搜索    
        來看一些在lucene中使用的術語,這裏並不打算作詳細的介紹,只是點一下而已----因爲這一個世界有一種好東西,叫搜索。    

        IndexWriter:lucene中最重要的的類之一,它主要是用來將文檔加入索引,同時控制索引過程中的一些參數使用。    

        Analyzer:分析器,主要用於分析搜索引擎遇到的各種文本。常用的有StandardAnalyzer分析器,StopAnalyzer分析器,WhitespaceAnalyzer分析器等。    

        Directory:索引存放的位置;lucene提供了兩種索引存放的位置,一種是磁盤,一種是內存。一般情況將索引放在磁盤上;相應地lucene提供了FSDirectory和RAMDirectory兩個類。    

        Document:文檔;Document相當於一個要進行索引的單元,任何可以想要被索引的文件都必須轉化爲Document對象才能進行索引。    

        Field:字段。    

        IndexSearcher:是lucene中最基本的檢索工具,所有的檢索都會用到IndexSearcher工具;    

        Query:查詢,lucene中支持模糊查詢,語義查詢,短語查詢,組合查詢等等,如有TermQuery,BooleanQuery,RangeQuery,WildcardQuery等一些類。    

        QueryParser: 是一個解析用戶輸入的工具,可以通過掃描用戶輸入的字符串,生成Query對象。    

        Hits:在搜索完成之後,需要把搜索結果返回並顯示給用戶,只有這樣纔算是完成搜索的目的。在lucene中,搜索的結果的集合是用Hits類的實例來表示的。    

        上面作了一大堆名詞解釋,下面就看幾個簡單的實例吧:    
        1、簡單的的StandardAnalyzer測試例子    

package com.lht;        
        
import java.io.IOException;        
import java.io.StringReader;        
        
import org.apache.lucene.analysis.Analyzer;        
import org.apache.lucene.analysis.Token;        
import org.apache.lucene.analysis.TokenStream;        
import org.apache.lucene.analysis.standard.StandardAnalyzer;        
/*        
*        提示一下:        
        StandardAnalyzer是lucene中內置的"標準分析器",可以做如下功能:        
        1、對原有句子按照空格進行了分詞        
        2、所有的大寫字母都可以能轉換爲小寫的字母        
        3、可以去掉一些沒有用處的單詞,例如"is","the","are"等單詞,也刪除了所有的標點        
        查看一下結果與"new StringReader("lighter javaeye com is the are on")"作一個比較就清楚明瞭。        
        這裏不對其API進行解釋了,具體見lucene的官方文檔。需要注意一點,這裏的代碼使用的是lucene2的API,與1.43版有一些明顯的差別。        
        */
        
public class StandardAnalyzerTest        
{        
        //構造函數,                
        public StandardAnalyzerTest()        
         {        
        }        
        public static void main(String[] args)        
         {        
                //生成一個StandardAnalyzer對象                
                Analyzer aAnalyzer = new StandardAnalyzer();        
                //測試字符串                
                StringReader sr = new StringReader("lighter javaeye com is the are on");        
                //生成TokenStream對象                
                TokenStream ts = aAnalyzer.tokenStream("name", sr);        
                try    {        
                        int i=0;        
                        Token t = ts.next();        
                        while(t!=null)        
                         {        
                                //輔助輸出時顯示行號                
                                i++;        
                                //輸出處理後的字符                
                                System.out.println("第"+i+"行:"+t.termText());        
                                //取得下一個字符                
                                t=ts.next();        
                        }        
                } catch (IOException e)    {        
                        e.printStackTrace();        
                }        
        }        
}                
顯示結果:    

        引用    
        第1行:lighter    
        第2行:javaeye    
        第3行:com    

        提示一下:    
        StandardAnalyzer是lucene中內置的"標準分析器",可以做如下功能:    
        1、對原有句子按照空格進行了分詞    
        2、所有的大寫字母都可以能轉換爲小寫的字母    
        3、可以去掉一些沒有用處的單詞,例如"is","the","are"等單詞,也刪除了所有的標點    
        查看一下結果與"new StringReader("lighter javaeye com is the are on")"作一個比較就清楚明瞭。    
        這裏不對其API進行解釋了,具體見lucene的官方文檔。需要注意一點,這裏的代碼使用的是lucene2的API,與1.43版有一些明顯的差別。    

        2、看另一個實例,簡單地建立索引,進行搜索    

package com.lht;        
        
import org.apache.lucene.analysis.standard.StandardAnalyzer;        
import org.apache.lucene.document.Document;        
import org.apache.lucene.document.Field;        
import org.apache.lucene.index.IndexWriter;        
import org.apache.lucene.queryParser.QueryParser;        
import org.apache.lucene.search.Hits;        
import org.apache.lucene.search.IndexSearcher;        
import org.apache.lucene.search.Query;        
import org.apache.lucene.store.FSDirectory;        
        
public class FSDirectoryTest {        
        
        //建立索引的路徑                
        public static final String path = "c:\\index2";        
        
        public static void main(String[] args) throws Exception    {        
                        
                /**建立索引*/        
                Document doc1 = new Document();        
                // Document:文檔;Document相當於一個要進行索引的單元,任何可以想要被索引的文件都必須轉化爲Document對象才能進行索引。        
                doc1.add( new Field("name", "lighter lighter javaeye com",    Field.Store.YES,Field.Index.TOKENIZED));        
                Document doc2 = new Document();        
                doc2.add(new Field("name", "lighter lighter lighter blog VerRan",Field.Store.YES,Field.Index.TOKENIZED));        
                Document doc3=new Document();        
                doc3.add(new Field("name","lighter VerRan",Field.Store.YES,Field.Index.TOKENIZED));        
                //IndexWriter:lucene中最重要的的類之一,它主要是用來將文檔加入索引,同時控制索引過程中的一些參數使用。            
                // Directory:索引存放的位置;lucene提供了兩種索引存放的位置,一種是磁盤,一種是內存。一般情況將索引放在磁盤上;        
                //相應地lucene提供了FSDirectory和RAMDirectory兩個類。            
                IndexWriter writer = new IndexWriter(FSDirectory.getDirectory(path, true),new StandardAnalyzer(), true);        
                writer.setMaxFieldLength(10);        
                writer.addDocument(doc1);        
                writer.setMaxFieldLength(10);        
                writer.addDocument(doc2);        
                writer.addDocument(doc3);        
                writer.close();        
                        
                /** 搜索*/        
             // IndexSearcher:是lucene中最基本的檢索工具,所有的檢索都會用到IndexSearcher工具;            
                IndexSearcher searcher = new IndexSearcher(path);        
                //Hits:在搜索完成之後,需要把搜索結果返回並顯示給用戶,只有這樣纔算是完成搜索的目的。        
                //在lucene中,搜索的結果的集合是用Hits類的實例來表示的。        
                Hits hits = null;        
                // Query:查詢,lucene中支持模糊查詢,語義查詢,短語查詢,組合查詢等等,如有TermQuery,BooleanQuery,RangeQuery,WildcardQuery等一些類。            
                //QueryParser: 是一個解析用戶輸入的工具,可以通過掃描用戶輸入的字符串,生成Query對象。        
                Query query = null;        
                QueryParser qp = new QueryParser("name",new StandardAnalyzer());//這裏的name和Document的name的參數對應        
        
                query = qp.parse("lighter");        
                hits = searcher.search(query);        
                System.out.println("查找\"lighter\" 共" + hits.length() + "個結果");        
                query = qp.parse("javaeye");        
                hits = searcher.search(query);        
                System.out.println("查找\"javaeye\" 共" + hits.length() + "個結果");        
                query = qp.parse("VerRan");        
                hits = searcher.search(query);        
                System.out.println("查找\"VerRan\" 共" + hits.length() + "個結果");        
        
        }        
        
}            
運行結果:    

        代碼    
查找"lighter" 共3個結果
查找"javaeye" 共1個結果
查找"VerRan" 共2個結果
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章