打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Lucene3.5 实现创建索引和搜索实例

                

       最近在做关于大数据量查询,发现Lucene是个不错的选择。关于Luncene3.5的代码在网上不是很多,参考网上一些代码并看API,做出如下代码,有什么问题,给留言。

 

  

       1.数据库作为数据源,创建索引:

  

           

Java代码
 
  1. //创建索引   
  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代码
 
  1. /**  
  2.      * 创建索引  
  3.      * @return  
  4.      */  
  5.     public IndexWriter createIndexWriter(OpenMode openMode)throws Exception{   
  •            
  •         //索引存放位置设置   
  •         Directory dir = FSDirectory.open(new File("E:\\index"));           
  •         // 索引配置类设置   
  •         IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_35,   
  •                 new StandardAnalyzer(Version.LUCENE_35));   
  •         iwc.setOpenMode(openMode);   
  •         IndexWriter writer = new IndexWriter(dir, iwc);   
  •         return writer;   
  •     }  
  •  

     

     

         

       2.检索数据:

     

        

    Java代码
     
    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 + "条数!");   
    26.     }  
    本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
    打开APP,阅读全文并永久保存 查看更多类似文章
    猜你喜欢
    类似文章
    lucene3.0创建索引及多目录搜索详解
    lucene、lucene.NET详细使用与优化详解
    lucene入门与使用
    lucene2.0 学习文档
    lucene爬数据库中的数据无非也是查询数据。所有我们用lucene搜索数据主要有下面几个步骤
    关于lucene2.0的创建、检索和删除功能的完整实现 - xiaodaoxiaodao—...
    更多类似文章 >>
    生活服务
    热点新闻
    分享 收藏 导长图 关注 下载文章
    绑定账号成功
    后续可登录账号畅享VIP特权!
    如果VIP功能使用有故障,
    可点击这里联系客服!

    联系客服