Lucene 最新版代码使用实例之【创建index】

来源:互联网 发布:js settimeout重复 编辑:程序博客网 时间:2024/05/25 05:36
/**

描述:使用最新版Lucene3.5.0的代码示例。

功能:搜索指定文件夹下的html文件,创建索引。

* 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();}}}


原创粉丝点击