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

當(dāng)前位置:首頁(yè) > 網(wǎng)站舊欄目 > 學(xué)習(xí)園地 > 設(shè)計(jì)軟件教程 > Lucene(Lucence)建立索引(字段)

Lucene(Lucence)建立索引(字段)
2010-01-13 23:09:43  作者:  來(lái)源:
   Lucene,這是官方稱謂,也有許多人叫它Lucence,做搜索和分詞用的工具包.也有人說(shuō)是Java下的搜索引擎框架庫(kù),見(jiàn)仁見(jiàn)智的說(shuō)法罷了.不管叫什么,確實(shí)非常有用,比如做全站的搜索,其實(shí)它的用處遠(yuǎn)大于此,但凡涉及到文本搜索的地方就能用到它.我們就以做全站搜索為例,演示一下如何應(yīng)用Lucene建立索引.
Java代碼 復(fù)制代碼
  1. public void index(List<IArticle> list)   
  2. {   
  3.   //IArticle接口提供getName(標(biāo)題)和getContent(內(nèi)容)   
  4.   //list就是從數(shù)據(jù)庫(kù)里查詢出來(lái)的要建立索引的對(duì)象的列表   
  5.   if(list != null && list.size() > 0)   
  6.   {   
  7.     try {   
  8.            //標(biāo)記是否重新建立索引,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.        //添加內(nèi)容屬性,內(nèi)容只索引,不存儲(chǔ)   
  23.        doc.add(new Field("content",new StringReader(list.get(i).getContent())));   
  24.        indexWriter.addDocument(doc);   
  25.       }   
  26.            //優(yōu)化并關(guān)閉   
  27.       indexWriter.optimize();   
  28.       indexWriter.close();   
  29.        } catch (Exception e)    
  30.        {   
  31.       // TODO 自動(dòng)生成 catch 塊   
  32.       //e.printStackTrace();   
  33.        }   
  34.   }   
  35. }  

簡(jiǎn)單說(shuō)下需要注意的地方:
1.ChineseAnalyzer ca = new ChineseAnalyzer();這個(gè)是分析器,Lucene內(nèi)置多個(gè),處理中文搜索我會(huì)用ChineseAnalyzer.
2.IndexWriter indexWriter = new IndexWriter(c:/lucene/index,ca,true);處理索引的類,注意其構(gòu)造方法里的最后一個(gè)參數(shù),如果為true則表示,下次建立索引時(shí)會(huì)清除這次建立的索引重新建立索引,如果為false則表示追加索引,在原來(lái)索引的基礎(chǔ)上追加.看實(shí)際情況定true或false.
3.doc.add(new Field("title",article.getName(),Field.Store.YES,Field.Index.TOKENIZED));這一句表示為文章標(biāo)題建立索引并存儲(chǔ).
4.doc.add(new Field("content",new StringReader(list.get(i).getContent())));這句是為內(nèi)容建立索引但不存儲(chǔ)
   這樣我們就為文章對(duì)象建立好索引了,然后就可以利用Lucene的其他類對(duì)這個(gè)索引目錄進(jìn)行搜索了,關(guān)于搜索部分我們稍后再補(bǔ)充上.
   下面是搜索部分的代碼,寫的簡(jiǎn)陋了點(diǎn),比較簡(jiǎn)單,不再多說(shuō),請(qǐng)參看注釋:
Java代碼 復(fù)制代碼
  1. public class Search   
  2. {   
  3.   //定義一個(gè)索引搜索類對(duì)象   
  4.   private IndexSearcher searcher = null;   
  5.   //定義一個(gè)Query對(duì)象   
  6.   private Query query = null;   
  7.   //定義中文分析器   
  8.   ChineseAnalyzer analyzer = new ChineseAnalyzer();   
  9.   //構(gòu)造方法里完成searcher的實(shí)例化   
  10.   public Search()   
  11.   {   
  12.     try  
  13.     {   
  14.      //這里的參數(shù)就是上面我們生成索引的目錄   
  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.     //開(kāi)始搜索的時(shí)間   
  24.     Date start = new Date();   
  25.     //對(duì)我們索引的content字段進(jìn)行搜索   
  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("檢索完成,用時(shí)"+(end.getTime()-start.getTime())+"毫秒");   
  31.     //////////打印測(cè)試////////   
  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("結(jié)果"+(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.   ///調(diào)用,主方法   
  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 自動(dòng)生成 catch 塊   
  59.        e.printStackTrace();   
  60.     }   
  61.   }   
  62. }  

安徽新華電腦學(xué)校專業(yè)職業(yè)規(guī)劃師為你提供更多幫助【在線咨詢