国产一区二区精品久久_蜜桃狠狠狠狠狠狠狠狠狠_午夜视频精品_激情都市一区二区

當前位置:首頁 > 網站舊欄目 > 學習園地 > 設計軟件教程 > Lucene(Lucence)建立索引(字段)

Lucene(Lucence)建立索引(字段)
2010-01-13 23:09:43  作者:  來源:
   Lucene,這是官方稱謂,也有許多人叫它Lucence,做搜索和分詞用的工具包.也有人說是Java下的搜索引擎框架庫,見仁見智的說法罷了.不管叫什么,確實非常有用,比如做全站的搜索,其實它的用處遠大于此,但凡涉及到文本搜索的地方就能用到它.我們就以做全站搜索為例,演示一下如何應用Lucene建立索引.
Java代碼 復制代碼
  1. public void index(List<IArticle> list)   
  2. {   
  3.   //IArticle接口提供getName(標題)和getContent(內容)   
  4.   //list就是從數據庫里查詢出來的要建立索引的對象的列表   
  5.   if(list != null && list.size() > 0)   
  6.   {   
  7.     try {   
  8.            //標記是否重新建立索引,true為重新建立索引   
  9.            boolean flag = true;   
  10.            //如果已存在索引,則追加索引   
  11.            if(IndexReader.indexExists(path))   
  12.       {   
  13.          flag = false;   
  14.            }   
  15.       ChineseAnalyzer ca = new ChineseAnalyzer();   
  16.       IndexWriter indexWriter = new IndexWriter("c:/lucene/index",ca,flag);            
  17.       Document doc = null;   
  18.       for(int i=0;i<list.size();i++)   
  19.           {   
  20.         doc = new Document();   
  21.         doc.add(new Field("title",article.getName(),Field.Store.YES,Field.Index.TOKENIZED));   
  22.        //添加內容屬性,內容只索引,不存儲   
  23.        doc.add(new Field("content",new StringReader(list.get(i).getContent())));   
  24.        indexWriter.addDocument(doc);   
  25.       }   
  26.            //優化并關閉   
  27.       indexWriter.optimize();   
  28.       indexWriter.close();   
  29.        } catch (Exception e)    
  30.        {   
  31.       // TODO 自動生成 catch 塊   
  32.       //e.printStackTrace();   
  33.        }   
  34.   }   
  35. }  

簡單說下需要注意的地方:
1.ChineseAnalyzer ca = new ChineseAnalyzer();這個是分析器,Lucene內置多個,處理中文搜索我會用ChineseAnalyzer.
2.IndexWriter indexWriter = new IndexWriter(c:/lucene/index,ca,true);處理索引的類,注意其構造方法里的最后一個參數,如果為true則表示,下次建立索引時會清除這次建立的索引重新建立索引,如果為false則表示追加索引,在原來索引的基礎上追加.看實際情況定true或false.
3.doc.add(new Field("title",article.getName(),Field.Store.YES,Field.Index.TOKENIZED));這一句表示為文章標題建立索引并存儲.
4.doc.add(new Field("content",new StringReader(list.get(i).getContent())));這句是為內容建立索引但不存儲
   這樣我們就為文章對象建立好索引了,然后就可以利用Lucene的其他類對這個索引目錄進行搜索了,關于搜索部分我們稍后再補充上.
   下面是搜索部分的代碼,寫的簡陋了點,比較簡單,不再多說,請參看注釋:
Java代碼 復制代碼
  1. public class Search   
  2. {   
  3.   //定義一個索引搜索類對象   
  4.   private IndexSearcher searcher = null;   
  5.   //定義一個Query對象   
  6.   private Query query = null;   
  7.   //定義中文分析器   
  8.   ChineseAnalyzer analyzer = new ChineseAnalyzer();   
  9.   //構造方法里完成searcher的實例化   
  10.   public Search()   
  11.   {   
  12.     try  
  13.     {   
  14.      //這里的參數就是上面我們生成索引的目錄   
  15.      searcher = new IndexSearcher(IndexReader.open("c:/lucene/index"));   
  16.     }catch(Exception e)   
  17.     {   
  18.       e.printStackTrace();   
  19.     }   
  20.   }   
  21.   public Hits search(String keyword) throws Exception   
  22.   {   
  23.     //開始搜索的時間   
  24.     Date start = new Date();   
  25.     //對我們索引的content字段進行搜索   
  26.     QueryParser qp = new QueryParser("content",analyzer);   
  27.     this.query = qp.parse(keyword);   
  28.     Hits hits = this.searcher.search(query);   
  29.     Date end = new Date();   
  30.     System.out.println("檢索完成,用時"+(end.getTime()-start.getTime())+"毫秒");   
  31.     //////////打印測試////////   
  32.     if(hits != null && hits.length() > 0)   
  33.     {   
  34.       for(int i = 0; i < hits.length(); i++)   
  35.       {   
  36.         try  
  37.         {   
  38.           Document doc = hits.doc(i);   
  39.           System.out.println("結果"+(i+1)+":"+doc.get("title")+" createTime:"+doc.get("content"));    
  40.           //System.out.println(doc.get("path"));   
  41.         }catch(Exception e)   
  42.         {   
  43.           e.printStackTrace();   
  44.         }   
  45.       }   
  46.     }   
  47.     return hits;   
  48.   }   
  49.   ///調用,主方法   
  50.   public static void main(String[] args)   
  51.   {   
  52.     try    
  53.     {   
  54.       Search test = new Search();   
  55.       Hits h = test.search("你好");   
  56.     } catch (Exception e)    
  57.     {   
  58.       // TODO 自動生成 catch 塊   
  59.        e.printStackTrace();   
  60.     }   
  61.   }   
  62. }  

安徽新華電腦學校專業職業規劃師為你提供更多幫助【在線咨詢