【直播】Lucene学习进阶--程序篇1
2011-12-13 10:56
106 查看
/** * V Lucene 3.5 * 创建索引 */ public static void createIndex(){ File indexDir = new File(LUCENEINDEX); File dataDir = new File(LUCENEDATA); Analyzer luceneAnalyzer = new StandardAnalyzer(Version.LUCENE_35); File[] dataFiles = indexDir.listFiles(); IndexWriter indexWriter = null; try { /** * indexWriter = new IndexWriter(SimpleFSDirectory.open(dataDir),luceneAnalyzer, true,IndexWriter.MaxFieldLength.LIMITED); * 在最新版中这种方式已被不使用。 * 现在使用下面的方式创建indexWriter */ IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_35, luceneAnalyzer); indexWriter = new IndexWriter(SimpleFSDirectory.open(dataDir), indexWriterConfig); long startTime = new Date().getTime(); //注意:filed实例在多次添加的时候可以重用,节约构造field实例的时间。 Field f1 = new Field("name", "", Field.Store.YES, Field.Index.NOT_ANALYZED) ; Field f2 = new Field("path", "", Field.Store.YES, Field.Index.NOT_ANALYZED) ; List<FilePojo> result = tree(indexDir); for (FilePojo po : result) { String name = po.getName(); String path = po.getPath(); try { System.out.println("Indexing file: " + path); Document doc = new Document(); f1.setValue(name); doc.add(f1); f2.setValue(path); doc.add(f2); indexWriter.addDocument(doc); } catch (IOException e) { e.printStackTrace(); } } //查看IndexWriter里面有多少个索引 System.out.println("numDocs:"+indexWriter.numDocs()); indexWriter.commit(); long endTime = new Date().getTime(); System.out.println("耗时:" + (endTime - startTime)); } catch (CorruptIndexException e) { e.printStackTrace(); } catch (LockObtainFailedException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { indexWriter.close(); } catch (CorruptIndexException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } // 递归方法 private static List tree(File f) { File[] childs = f.listFiles(); for (int i = 0; i < childs.length; i++) { if (childs[i].isDirectory()) { tree(childs[i]); }else if(childs[i].isFile() && childs[i].getName().endsWith(".html")){ //执行索引 try { FilePojo po = new FilePojo(); po.setName(childs[i].getName()); po.setPath(childs[i].getCanonicalPath()); result.add(po); } catch (IOException e) { e.printStackTrace(); } } } return result; }
相关文章推荐
- 机器人进阶学习(二)--上位机上的程序搭建
- 微信小程序的学习直播
- 写了第一个lucene程序(lucene学习笔记)
- 重新一步一步学习Lucene.NET 一个简单的程序开始(1)
- storm学习(2) 进阶word_count 程序
- Jcrontab - java定时程序进阶学习-Java基础-Java-编程开发
- Jcrontab - java定时程序进阶学习
- 友善之臂视频监控方案源码学习(2) - 主程序实现细节
- iOS从零开始学习直播之音频1.播放本地音频文件
- ios学习--Objective C内存管理进阶(二):理解autorelease
- Scala 学习笔记(6)-程序例子Scala Keams聚类算法
- 【Solr4.7.0学习笔记】系列之一:我的第一个Solr程序
- Opengl学习程序之多实例
- lucene学习笔记(五)lucene高级搜索
- 微信小程序学习笔记 (小程序主体学习 二 逻辑层 app.js 基础食用方法)
- ACM 进阶学习第一课----同余相关之中国剩余定理
- lucene学习笔记(八)lucene实时搜索
- 微信小程序开发display:flex属性的学习
- 学习Lucene.Net 2.1常用的一些网址
- [计算机程序的构造和解释学习笔记]-1.牛顿法求平方根