lucene-创建文档索引处理框架

本文介绍了一个用于文件索引操作的框架设计,包括关键类如DocumentHandler和ExtensionFileHandler的职责,异常处理机制,以及如何通过FileIndexer类递归遍历文件系统并建立索引。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、组成文件索引操作框架的JAVA类

1)DocumentHandler:定义getDocument(InputStream)方法,此方法由所有的文档解析器实现

2)DocumentHandlerException:遇到错误情况时,该类将检测所有从文档解析器抛出的异常

3)FileHanlder:定义getDocument(File)方法,该方法由ExtensionFleHandler类实现

4)FileHanlderException:检测从实现了FileHandler接口的具体类中抛出的异常

5)ExtensionFileHandler:实现FileHandler接口的类与实现Document接口的类具有相同的工作方式,它传经根据由方法getDocument(File)传递给该实现类的文件扩展名来调用相应的分析器对不同的文档进行处理

2、

FileHandler接口

public interface FileHandler{

    DocumentgetDocument(File file){

        throws FileHandlerException;

    }

}

3、

ExtensionFileHandler类实现了FileHandler接口。

public class ExtensionFileHandler implements FileHandler{

    privateProperties handlerProps;

    publicExtensionFileHandler(Proprops) throws IOException{

       handlerProps=props;//映射文件扩展名

    }

    publicDocument getDocument(File file) throws FileHandlerException{

       Document doc=null;

       String name=file.getName();

       int dotIndex=name.indexOf(".");

       if((dotIndex>0)&&(dotIndex<name.length())){//提取文件扩展

           String ext=name.substring(dotIndex+1,name.length());

           StringhandlerClassName=handlerProps.getProperty(ext);//将文件传递给解析器实现

           try{//查找解析器名称

              Class handler handler=(DocumentHandler)handlerClass.newIntance();

              return handler.getDocument(new FileInputStream(file));//

            }

           catch (ClassNotFoundExceptoin e){

               thrownew FileHandlerException

                   ("cannotcreate instanceof:"+handlerClassName,e);         

            }

           catch (InstantiationExceptoin e){

               thrownew FileHandlerException

                   ("cannotcreate instanceof:"+handlerClassName,e);         

            }

           catch (IllegalAccessExceptoin e){

               thrownew FileHandlerException

                   ("cannotcreate instanceof:"+handlerClassName,e);         

            }

           catch (FileNotFoundExceptoin e){

               thrownew FileHandlerException

                   ("Filenotfound:"+file.getAsbsolutePath(),e);         

            }

           catch (DocumentHandlerExceptoin e){

               thrownew FileHandlerException

                   ("Documentcannot behandler:"+file.getAsbsolutePath(),e);         

            }                    

         }

        return null;

       }

       

       public static void main(String[] args) throws Exception{

         if (args.length<2) {

                usage();

                System.exit(0);

          }

 

         Properties props=new Properties();

          props.load(newFileInputStream(args[0]));//装载属性文件

         

         ExtensionFileHandler fileHandler=newExtensionFileHandler(props);

          Documentdoc=fileHandler.getDocument(new File(args[1]));

        }

       

        privatestatic void usage(){

           System.err.println("USAGE:java"+ExtensionFileHandler.class.getName()

              +"/path/to/properties /path/to/document");

       }

    }

}

4、FileIndex把搜索组件连接在一起,递归地遍历文件系统目录并同时索引其中的所有文件。

public class FileIndexer{

    protected FileHandler fileHandler;

     publicFileIndexer(Properties props) throws IOException{

       fileHandler=newExtensionFileHandler(props);//使用ExtensionFileHandler接口

    }

    public void index(IndexWriter writer,File file) throwsFileHandlerException{

        //index方法

       if (file.canRead()){ //递归遍历可读的目录

           if (file.isDirectory()){

                String[] files=file.list();

                if (files!=null){

                    for (int i=0;i<files.length;i++){

                        index(writer,newFile(file,files[i]));

                    }

                }

           }

            else{

                System.out.println("Indexing"+file);//将文件传递给ExtensionFileHandler

               try{

                   Docuement doc=fileHandler.getDocument(file);

                   if (doc!=null){

                      writer.addDocument(doc);//将返回的lucene文档增加到索引中

                    }

                   else{

                       System.err.println("cannothandler"+file.getAbsolutedPath()

                             +" ;skipping("+e.getMessage()+")");

                   }

               }

            }

       }

        publicstatic void main(String[] args) throws Exception{

           if (args.length<3){

               usage();

                System.exit(0);

            }

          

           Properties props=new Properties();

           props.load(new FileInputStream(args[0]));//读取命令行指定的属性文件

          

           Directory dir=FSDirectory.getDirectory(args[2],true);

            Analyzeranalyzer=new SimpleAnalyzer();

           IndexWriter writer=new IndexWriter(dir,analyzer,true);//打开索引

           

           FileIndexer indexer=new FleIndexer(props);//创建FileIndexer实例

           long start=new Date().getTime();

           indexer.index(writer,new File(args[1]));//首次调用index方法

            writer.optimize();//优化索引:关闭索引写入器

            writer.close();

           long end=new Date().getTime();

           

           System.out.println();

            IndexReaderreader=IndexReader.open(dir);

           //面向用户的摘要信息

           System.out.println("Document indexed:"+reader.numDocs());

           System.out.println("Totaltime:"+(end-start)+"ms");           

           reader.close();           

       }

       

        privatestatic void usage(){

           System.err.println("USAGE:java"+ExtensionFileHandler.class.getName()

              +"/path/to/properties /path/to/document");

       }

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值