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");
}
}
}
相关推荐
分析阶段将输入文本拆分成有意义的单元——术语,然后创建术语文档表,最后构建倒排索引,使得搜索时能快速定位到包含特定术语的文档。 3. 搜索过程:搜索时,用户输入的查询会被分析,生成对应的术语,然后通过倒...
2. 索引(Index):Lucene创建倒排索引,这是一种数据结构,使得快速查找包含特定词项的文档变得可能。索引由多个段(Segments)组成,每个段是一个独立的、不可变的搜索索引。 3. 查询解析器(QueryParser):将...
总之,Lucene-5.2.1是一个强大而灵活的全文检索框架,不仅适用于大型网站的搜索功能,也可以用于企业内部信息检索、文档管理系统等多个领域。通过学习和实践这些经典实例,开发者能够掌握如何利用Lucene构建高效的...
1. **分词和索引**:Lucene能够对输入文档进行分词,创建一个高效的倒排索引,使得在大量数据中快速查找匹配项成为可能。这个过程包括分析、tokenization、词干提取和停用词处理等步骤。 2. **查询解析**:用户输入...
`lucene-core-2.3.0` 是Lucene的一个早期版本,它包含了构建基本搜索引擎所需的基本组件,如文档处理、索引创建、查询解析和结果排序等。这个版本可能不包含后来版本中的一些优化和特性,比如更先进的分词技术、性能...
首先,Lucene的核心概念包括文档(Document)、字段(Field)、索引(Index)和查询(Query)。一个文档可以包含多个字段,如标题、内容等,每个字段都可以被索引以便搜索。索引是Lucene的核心,它将文本数据转换...
Lucene是一个全文搜索框架...因此它并不像http://www.baidu.com/ 或者google Desktop那么拿来就能用,它只是提供了一种工具让你能实现这些产品,lucene-core:其中包括了常用的文档,索引,搜索,存储等相关核心代码。
在Lucene中,索引的过程包括分析、文档添加和写入等步骤。`Indexer.java`中可能会包含对`Directory`(存储索引的容器)、`Analyzer`(负责文本分析,如分词)和`IndexWriter`(用于创建和更新索引)的使用。通过调用...
- **创建索引**:使用`IndexWriter`类创建和更新索引,添加`Document`对象并调用`addDocument`方法。 - **搜索索引**:通过`IndexSearcher`类执行查询,`Query`对象表示用户输入的查询条件,`TopDocs`返回匹配文档...
- **倒排索引**:Lucene采用倒排索引技术,将文档中的词汇与文档ID关联,提高了查询速度。 - **分词器(Analyzer)**:Lucene提供多种分词器,如标准分词器、中文分词器等,用于将输入文本分解成关键词。 2. **...
通过阅读文档,学习如何创建索引、执行查询、管理和优化索引,以及如何与其他系统集成,如Web应用、数据库等。 总的来说,Apache Lucene 6.5.0工具包为开发人员提供了一个强大且灵活的全文检索引擎,帮助他们构建...
- **IndexWriter**:用于创建和更新索引,它管理索引的生命周期,包括添加、删除和修改文档。 - **IndexReader**:用于读取索引,提供搜索操作。 - **IndexSearcher**:执行搜索查询,并返回匹配的Document结果。...
在使用这个压缩包时,首先需要解压,然后根据提供的API文档和示例代码,将Lucene集成到你的Java项目中,创建和管理索引,执行搜索查询,并处理搜索结果。同时,确保你的系统环境满足Lucene的运行要求,例如Java版本...
Lucene 的核心功能之一是创建高效的索引。索引过程将文档内容转化为便于搜索的数据结构。在4.6.1版本中,Lucene 支持分词、分析和标准化,这使得搜索时能够进行模糊匹配和同义词查询。同时,它支持倒排索引,这是一...
它包括文档的创建、索引、查询以及结果的排序和过滤。这个jar包提供了如Analyzer、IndexWriter、Directory、QueryParser等关键类。 2. **lucene-analyzers-common-5.0.0.jar**:这个包包含了Lucene的通用分析器,...
在3.5.0版本中,Lucene可能已经具备了如分词器、分析器、文档处理、索引优化、查询解析等功能。开发者可以利用这些工具来构建自己的搜索引擎,实现对文本数据的快速索引和检索。 描述中提到的“Junit各个版本的集合...
1. **创建索引**:通过`IndexWriter`类创建和管理索引,添加或删除文档。 2. **搜索索引**:使用`IndexSearcher`进行搜索,`QueryParser`帮助解析查询字符串。 3. **读取索引**:`DirectoryReader`用于读取索引,...
汉纳拼音Lucene插件(hanlp-lucene-plugin)是一个专为中文处理设计的扩展工具,它将著名的HanLP中文分词库与流行的全文检索框架Lucene进行了集成,旨在提升在Lucene及其衍生产品(如Solr)中的中文处理能力。...
它的核心功能包括文档的索引、搜索以及相关的高级特性。其中,"lucene-analysis.jar"是Lucene项目中的一个重要组件,主要负责文本的预处理工作,即文本分析。本文将深入探讨这个jar包及其包含的文件,以揭示其在...
4. 索引(Index):索引是Lucene的核心,它是一种倒排索引结构,允许快速查找包含特定词元的文档。5.2.1版本对索引结构进行了优化,提高了查询速度和存储效率。 二、主要组件 1. 分析器(Analyzer):分析器负责将...