Lucene索引文件示例
来源:互联网 发布:如何当一名网络写手 编辑:程序博客网 时间:2024/04/30 09:51
package iLucene;import java.io.File;import java.io.FileNotFoundException;import java.io.IOException;import java.util.Date;import org.apache.lucene.analysis.standard.StandardAnalyzer;import org.apache.lucene.demo.FileDocument;import org.apache.lucene.index.CorruptIndexException;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.store.FSDirectory;import org.apache.lucene.util.Version;/** * 索引某一目录下的所有文本文件 * * @author wujinsong */public class IndexFiles { private IndexFiles() { } static final File INDEX_DIR = new File("c:\\index"); public static void main(String[] args) { if (INDEX_DIR.exists()) { System.out.println("Cannot save index to '" + INDEX_DIR + " ' directory,please delete it first"); System.exit(1); } final File docDir = new File("C:\\kankan"); if (!docDir.exists() || !docDir.canRead()) { System.out.println("Document directory '" + docDir.getAbsolutePath() + "' does not exist or is not readable, please check the path"); System.exit(1); } Date start = new Date(); try { IndexWriter writer = new IndexWriter(FSDirectory.open(INDEX_DIR), new StandardAnalyzer(Version.LUCENE_CURRENT), true, IndexWriter.MaxFieldLength.LIMITED); System.out.println("Indexing to directory '" + INDEX_DIR + "'..."); indexDocs(writer, docDir); System.out.println("Optimizing..."); writer.optimize(); writer.close(); Date end = new Date(); System.out.println(end.getTime() - start.getTime() + " total milliseconds!"); } catch (IOException e) { System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage()); } } /** * 索引文件 * * @param writer * @param file * @throws CorruptIndexException * @throws IOException */ static void indexDocs(IndexWriter writer, File file) throws CorruptIndexException, IOException { // 不要试图索引不可读的文件 if (file.canRead()) { if (file.isDirectory()) { String[] files = file.list(); // 一个IO错误可能发生 if (files != null) { for (int i = 0; i < files.length; i++) { indexDocs(writer, new File(file, files[i])); } } } else { System.out.println("adding " + file); try { writer.addDocument(FileDocument.Document(file)); } // 至少在windows系统,一些临时文件会导致“访问拒绝”异常,检查文件是否可以被读取将不起作用。 catch (FileNotFoundException exception) { } } } }}
可以从https://github.com/iswu2013/iLucene/ 上获取代码!
转载请注明出去!
0 0
- Lucene索引文件示例
- Lucene数据索引搜索示例
- lucene的索引文件
- lucene的索引文件
- lucene 索引文件简介
- Lucene系列-索引文件
- 用Lucene建立索引及查询示例
- 用Lucene建立索引及查询示例
- 用Lucene建立索引及查询示例
- 用Lucene建立索引及查询示例
- lucene删除索引中的Document示例
- lucene的索引文件结构
- 深入Lucene的索引文件
- lucene生成索引相关文件
- lucene索引文件检测CheckIndex
- lucene搜索引擎(文件索引、数据库索引)
- lucene搜索引擎(文件索引、数据库索引)
- 通过Lucene索引文件学习Lucene索引过程
- 招优质人才,找靠谱Offer,亲就得爱内推
- 传智基础与加强day05
- Java实现文档在线预览(openoffice+swfTools+FlexPaper)
- ZOJ 1089-Lotto
- java String 面试题详解
- Lucene索引文件示例
- 读《我们如何思维》笔记
- 求最小面积外接矩阵和最小周长外接矩阵
- TreeView控件动态增加节点
- Email开发初步总结
- Default-568h@2x.png pngcrush caught libpng error:
- ios的NSArray排序
- EnumWindows
- Conditional validation on Symfony2 fom checkbox widget