小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Lucene 3.6 (3.X) 入門實例

 CevenCheng 2012-05-12
 

第一個Lucene 3.6  (3.X) 入門實例

 86人閱讀 評論(0) 收藏 舉報
  1. 運行l(wèi)ucene所需要的JAR包  
  2. lucene-core-3.6.0.jar(核心包)  
  3. lucene-analyzers-3.6.0.jar(分詞器)  
  4. lucene-highlighter-3.6.0.jar(高亮)  
  5. lucene-memory-3.6.0.jar(高亮)  
  6.   
  7. public class HelloWord {  
  8.     public static void createIndexFile() {  
  9.         IndexWriter indexWriter=null;  
  10.         try {  
  11.             // 需要的分詞器  
  12.             Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);  
  13.             // 創(chuàng)建的是哪個版本的IndexWriterConfig  
  14.             IndexWriterConfig indexWriterConfig = new IndexWriterConfig(  
  15.                     Version.LUCENE_36, analyzer);  
  16.             // 創(chuàng)建系統(tǒng)文件----- ./ 當(dāng)前路徑下的  
  17.             Directory directory = new SimpleFSDirectory(new File("./indexDir/"));  
  18.             indexWriter = new IndexWriter(directory,indexWriterConfig);   
  19.             //獲取實體對象  
  20.             Article article=new Article(11,"最XX的城市","XX");    
  21.             //indexWriter添加索引  
  22.             Document doc=new Document();  
  23.             //文本中添加內(nèi)容            標(biāo)題      內(nèi)容  
  24.             /*doc.add(new Field("title","中國的首都在哪里",Store.YES,Index.ANALYZED));  
  25.             doc.add(new Field("content","中國的首都在北京",Store.YES,Index.ANALYZED));*/      
  26.             doc.add(new Field("id",article.getId().toString(),Store.YES,Index.ANALYZED));  
  27.             doc.add(new Field("title",article.getTitle().toString(),Store.YES,Index.ANALYZED));  
  28.             doc.add(new Field("content",article.getContent().toString(),Store.YES,Index.ANALYZED));   
  29.             //添加到索引中去  
  30.             indexWriter.addDocument(doc);     
  31.         } catch (IOException e) {  
  32.             // TODO Auto-generated catch block  
  33.             e.printStackTrace();  
  34.         }finally{  
  35.             if(indexWriter!=null){  
  36.                 try {  
  37.                     indexWriter.close();  
  38.                 }  catch (IOException e) {  
  39.                     // TODO Auto-generated catch block  
  40.                     e.printStackTrace();  
  41.                 }  
  42.             }  
  43.         }  
  44.     }  
  45.     //如果查詢是需要用到解析器,那解析器必須和創(chuàng)建時的解析器相同  
  46.     public static void searchIndexFileResult() throws IOException {   
  47.         List<Article> articles=new ArrayList<Article>();      
  48.         //得到索引的目錄  
  49.         Directory directory = new SimpleFSDirectory(new File("./indexDir/"));  
  50.         //根據(jù)目錄打開一個indexReader  
  51.         IndexReader indexReader=IndexReader.open(directory);  
  52.         //System.out.println(indexReader.maxDoc());   
  53.         //獲取最小值的document對象  
  54.         //Document doc=indexReader.document(0);  
  55.         //獲取最大值的document對象  
  56.         //Document doc=indexReader.document(indexReader.maxDoc()-1);  
  57.         //document對象的get(字段名稱)方法獲取字段的值  
  58.         /*System.out.println(doc.get("id"));  
  59.         System.out.println(doc.get("title"));  
  60.         System.out.println(doc.get("content"));*/     
  61.         int n=indexReader.maxDoc();  
  62.         for(int i=0;i<n;i++){  
  63.             Document doc=indexReader.document(i);  
  64.             Article article=new Article();  
  65.             if(doc.get("id")==null){  
  66.                 System.out.println("id為空");  
  67.             }else{  
  68.                 article.setId(Integer.parseInt(doc.get("id")));  
  69.                 article.setTitle(doc.get("title"));  
  70.                 article.setContent(doc.get("content"));  
  71.                 articles.add(article);  
  72.             }  
  73.         }  
  74.         for(Article article:articles){  
  75.             System.out.println(article.toString());  
  76.         }     
  77.     }  
  78.     public static void main(String[] args) throws IOException {  
  79.         // 建立要索引的文件  
  80.     //  createIndexFile();  
  81.         // 從索引文件中查詢數(shù)據(jù)  
  82.         searchIndexFileResult();  
  83.         // 獲得結(jié)果,然后交由相關(guān)應(yīng)用程序處理  
  84.     }  
  85. }  

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多