分享

Lucene 3.6 (3.X) 入门实例

 CevenCheng 2012-05-12
 

第一个Lucene 3.6  (3.X) 入门实例

 86人阅读 评论(0) 收藏 举报
  1. 运行lucene所需要的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.             // 创建的是哪个版本的IndexWriterConfig  
  14.             IndexWriterConfig indexWriterConfig = new IndexWriterConfig(  
  15.                     Version.LUCENE_36, analyzer);  
  16.             // 创建系统文件----- ./ 当前路径下的  
  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.             //文本中添加内容            标题      内容  
  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.     //如果查询是需要用到解析器,那解析器必须和创建时的解析器相同  
  46.     public static void searchIndexFileResult() throws IOException {   
  47.         List<Article> articles=new ArrayList<Article>();      
  48.         //得到索引的目录  
  49.         Directory directory = new SimpleFSDirectory(new File("./indexDir/"));  
  50.         //根据目录打开一个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.         // 从索引文件中查询数据  
  82.         searchIndexFileResult();  
  83.         // 获得结果,然后交由相关应用程序处理  
  84.     }  
  85. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多