Lucene3.4索引文件创建过程(有源码)

public class Indexer { private IndexWriter writer; * main(这里用一句话描述这个方法的作用) public static void main(String[] args) throws IOException { String indexDir = "F:/lucene/indexDir"; //指定目录创建索引 String dataDir = "F:/lucene/dataDir"; //存储数据的目录 long beginIndexTime = System.currentTimeMillis(); Indexer indexer = new Indexer( indexDir ); //生成索引写入器 int numIndexed = 0; try { numIndexed = indexer.index( dataDir, new TextFilesFilter() ); //开始索引文件 } catch (Exception e) { // TODO: handle exception e.printStackTrace(); }finally { indexer.close(); } long endIndexTime = System.currentTimeMillis(); System.out.println( "Indexing " + numIndexed + " files took " + (endIndexTime - beginIndexTime) + "miliseconds" ); } public Indexer(String indexDir) throws IOException { Directory dir = FSDirectory.open( new File( indexDir ) ); //建立磁盘索引 writer = new IndexWriter( dir, new StandardAnalyzer( Version.LUCENE_34 ), IndexWriter.MaxFieldLength.UNLIMITED ); } public void close() throws CorruptIndexException, IOException { writer.close(); } public int index( String dataDir, FileFilter filter ) throws IOException { File[] files = new File( dataDir ).listFiles(); for (File f : files) { if ( !f.isDirectory() && !f.isHidden() && f.exists() && f.canRead() && ( filter == null || filter.accept( f ) ) ) { indexFile( f ); } } return writer.numDocs(); } private void indexFile( File f ) throws IOException { System.out.println( "Indexing " + f.getCanonicalPath() ); Document doc = getDocument( f ); writer.addDocument( doc ); } private Document getDocument( File f ) throws IOException { Document doc = new Document(); doc.add( new Field( "contents" , new FileReader( f )) ); doc.add( new Field( "filename", f.getName(), Field.Store.YES, Field.Index.NOT_ANALYZED ) ); doc.add( new Field( "fullpath", f.getCanonicalPath(), Field.Store.YES, Field.Index.NOT_ANALYZED ) ); return doc; } private static class TextFilesFilter implements FileFilter{ /* (non-Javadoc) * @see java.io.FileFilter#accept(java.io.File) */ @Override public boolean accept(File pathname) { // TODO Auto-generated method stub return pathname.getName().endsWith( ".txt" ); } } }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值