日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Lucene3.5 實現(xiàn)創(chuàng)建索引和搜索實例

 閑來看看 2012-05-16

                

       最近在做關(guān)于大數(shù)據(jù)量查詢,發(fā)現(xiàn)Lucene是個不錯的選擇。關(guān)于Luncene3.5的代碼在網(wǎng)上不是很多,參考網(wǎng)上一些代碼并看API,做出如下代碼,有什么問題,給留言。

 

  

       1.數(shù)據(jù)庫作為數(shù)據(jù)源,創(chuàng)建索引:

  

           

Java代碼 復(fù)制代碼 收藏代碼
  1. //創(chuàng)建索引   
  2. ublic void createIndex(OpenMode  openMode,List<Authors> list){   
  3.             try {   
  4.     IndexWriter indexWriter=null;   
  5.     try {   
  6.         indexWriter=new LTes ().createIndexWriter(openMode);   
  7.     } catch (Exception e) {   
  8.         e.printStackTrace();   
  9.     }   
  10.     //List<Authors> list=new  PersistenceFacade().query("from Authors where id=300");   
  11.     for(Authors au:list){   
  12.           Document doc=new Document();   
  13.           doc.add(new Field("id",au.getId()+"",Field.Store.YES,Field.Index.ANALYZED));   
  14.           doc.add(new Field("authorName",au.getAuthorName(),Field.Store.YES,Field.Index.ANALYZED));   
  15.           doc.add(new Field("authorID",au.getAuthorID(),Field.Store.YES,Field.Index.NO));   
  16.           doc.add(new Field("phone",au.getPhone(),Field.Store.YES,Field.Index.NO));   
  17.           doc.add(new Field("Address",au.getAddress() ,Field.Store.YES,Field.Index.NO));   
  18.           doc.add(new Field("City",au.getAuthorID(),Field.Store.YES,Field.Index.NO));   
  19.           doc.add(new Field("status",au.getPhone(),Field.Store.YES,Field.Index.NO));   
  20.           doc.add(new Field("zip",au.getPhone(),Field.Store.YES,Field.Index.NO));   
  21.           //indexWriter.addDocument(doc);   
  22.           if (indexWriter.getConfig().getOpenMode() == IndexWriterConfig.OpenMode.CREATE)   
  23.           {   
  24.               logger.info("OpenModel:CREATE");   
  25.               indexWriter.addDocument(doc);   
  26.           }   
  27.           else  
  28.           {   
  29.               logger.info("OpenModel:APPEND");   
  30.               indexWriter.updateDocument(new Term("id",String.valueOf(au.getId())), doc);   
  31.           }        
  32.     }   
  33.         indexWriter.close();   
  34.     } catch (CorruptIndexException e) {   
  35.         e.printStackTrace();   
  36.     } catch (IOException e) {   
  37.         e.printStackTrace();   
  38.     }   
  39.   }   
  40.     

 

 

         提示:  indexWriter.updateDocument(new Term("id","123"), doc);

   表示更新ID為123的索引,如果沒有則新增。

  

Java代碼 復(fù)制代碼 收藏代碼
  1. /**  
  2.      * 創(chuàng)建索引  
  3.      * @return  
  4.      */  
  5.     public IndexWriter createIndexWriter(OpenMode openMode)throws Exception{   
  6.            
  7.         //索引存放位置設(shè)置   
  8.         Directory dir = FSDirectory.open(new File("E:\\index"));           
  9.         // 索引配置類設(shè)置   
  10.         IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_35,   
  11.                 new StandardAnalyzer(Version.LUCENE_35));   
  12.         iwc.setOpenMode(openMode);   
  13.         IndexWriter writer = new IndexWriter(dir, iwc);   
  14.         return writer;   
  15.     }  

 

 

 

     

   2.檢索數(shù)據(jù):

 

    

Java代碼 復(fù)制代碼 收藏代碼
  1. public  List<Authors> search(String queryString) throws Exception{   
  2.         String[] queryFields={"authorName"};   
  3.         List<Authors> list=new ArrayList<Authors>();   
  4.         IndexReader reader=IndexReader.open(FSDirectory.open(new File("E:\\index")));   
  5.         IndexSearcher searcher=new IndexSearcher(reader);   
  6.         QueryParser parser = new MultiFieldQueryParser(Version.LUCENE_35, queryFields,new StandardAnalyzer(Version.LUCENE_35));   
  7.         Query query = parser.parse(queryString);   
  8.         TopDocs results = searcher.search(query,null,1000);   
  9.         ScoreDoc[] hits=results.scoreDocs;   
  10.         for(ScoreDoc sd:hits){   
  11.            int docID=sd.doc;   
  12.            Document doc=searcher.doc(docID);   
  13.            Authors authors=new Authors();   
  14.            authors.setId(Integer.valueOf(doc.get("id")));   
  15.            authors.setAddress(doc.get("Address"));            
  16.            authors.setAuthorID(doc.get("authorID"));   
  17.            authors.setAuthorName(doc.get("authorName"));   
  18.            authors.setCity(doc.get("city"));   
  19.            authors.setPhone(doc.get("phone"));   
  20.            authors.setStatus(doc.get("status"));   
  21.            authors.setZip(doc.get("zip"));   
  22.            list.add(authors);   
  23.         }   
  24.         return list;   
  25.         //System.out.println("總符合: " + results.totalHits + "條數(shù)!");   
  26.     }  

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

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多