`
xtugtf
  • 浏览: 24623 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

代码重构,lucene实例提炼

阅读更多

       昨天按《lucene in Action》 书中的例子动手运行了一下,也把遇到问题及相关用法作了简单总结。今天来把代码好好的梳理了一下,并对代码进行相关的重构(refactory)。

       XP要求我们:测试——编码——重构——测试——编码——重构,我还是没有这种逆瀑布法来作为开发指导,而是以编码——测试——重构——编码——测试——重构来指导自己的开发,并且以keep it simple为原则来设计类。下面就将自己今天的重构过程记一下流水账。

        一、问题

       1)需求总是变化的,如何设计系统能较好的适应需求变化是软件的设计的根本。昨天实现的两个建立索引与进行搜索的类,其职则是单一的,已符合单一职责法则,但只对文本文件进行建立索引,那么当要对word文件或pdf文件进行建立索引呢?这一需求的变化带来了,要么重新设计新类,要么在原有的Indexer类中进行参数判别来实现对不同文件来建立索引。这显然不符合开闭原则(OCP)。

      2) 复用,复用,还是复用。以最少的修改来适应需求的变化,来达到代码的复用是软件重构的目标。现在的两个类显然不能很好的适应用户新的需求,开发人员不能很快修改代码,快速部署来满足用户的新需求。

 

     从上面两个问题来说,我们需要对代码进行重构:满足用户新的需求(这里是预测到的需求变化点),重构达到代码的最佳复用。

 

   二、重构

        复用人们已提出很好的方案:依赖接口编程或抽象类编程来解决快速适应需求变化。

 

      接口类:对外提供统一稳定简单的功能接口;

      具体实现类:实现具体需求所要的接口功能;

  具体的用代码来说明:

     1)接口

 

package com.goodwitkey.seargine.src;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.search.Hits;

/**
 * @author Owner
 *
 */
public interface Ifileseargine {
	
	//建立索引接口
	public  int index(File dataFilesPath,File indexFilePath)throws IOException;
	public  void indexDirectory(IndexWriter indexwr,File dataFilesPath)throws IOException;
	public  void indexFile(IndexWriter writer, File f)throws IOException;
	
	//提供搜索接口
	public Hits search(File indexFilePath,String queryStr)throws IOException,ParseException ;
}

 

 

   2)文本文件的搜索一个具体实现

   /**

 * 
 */
package com.goodwitkey.seargine.src;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Date;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

/**
 * @author Owner
 * 
 */
public class ItxtfilesearchgineImp implements Ifileseargine {
	 
	
    //测试
	public static void main(String[] args)throws Exception{
		Ifileseargine itxtsearchgine=new ItxtfilesearchgineImp();   
		File dataFilePath = new File("C:\\data");
		File indexFilePath= new File("C:\\indexfiles");
		int indexedfileNum=itxtsearchgine.index(dataFilePath, indexFilePath);
		System.out.println("已索引文件个数:"+indexedfileNum);
		itxtsearchgine.search(indexFilePath, "希拉");
			
	}
	
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.goodwitkey.seargine.src.fileseargine#index(java.io.File,
	 *      java.io.File)
	 */
	public int index(File dataFilesPath, File indexFilePath) throws IOException {
		// TODO Auto-generated method stub
		if (!dataFilesPath.exists() || !dataFilesPath.isDirectory()) {
			throw new IOException(dataFilesPath
					+ "don't exists or is not a directory");
		}
		// lucene 2.4.0已将此构造函数deprecated
		System.out.println("****************" + indexFilePath);
		IndexWriter indexwr = new IndexWriter(indexFilePath,
				new StandardAnalyzer(), true);
		// 设置为true时,一定要主意建立索引文件夹不能有其它的重要文件,否则不小心会全删除掉的。
		// boolean create - true to create the index or overwrite the existing
		// one; false to append to the existing index
		indexwr.setUseCompoundFile(false);
		indexwr.mergeFactor = 2;

		// 建立索引
		indexDirectory(indexwr, dataFilesPath);
		System.out.println("****************" + indexFilePath);
		int indexedNum = indexwr.docCount();
		indexwr.optimize();
		indexwr.close();

		return indexedNum;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.goodwitkey.seargine.src.fileseargine#indexDirectory()
	 */
	public void indexDirectory(IndexWriter indexwr, File dataFilesPath)
			throws IOException {
		// TODO Auto-generated method stub
		File[] files = dataFilesPath.listFiles();
		for (int i = 0; i < files.length; i++) {
			File f = files[i];
			System.out.println(f.getName());
			if (f.isDirectory()) {
				indexDirectory(indexwr, f);// recurse
			} else if (f.getName().endsWith(".txt")) {

				indexFile(indexwr, f);
			}

		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.goodwitkey.seargine.src.fileseargine#indexfile()
	 */
	public void indexFile(IndexWriter writer, File f) throws IOException {
		// TODO Auto-generated method stub
		if (!f.exists() || !f.canRead()) {
			return;
		}

		System.out.println("it gets the file now");

		Document doc = new Document();
		doc.add(Field.Text("contents", new FileReader(f)));
		doc.add(Field.Keyword("filename", f.getCanonicalPath()));
		writer.addDocument(doc);
		System.out.println(f.toString());

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.goodwitkey.seargine.src.fileseargine#search(java.io.File,
	 *      java.lang.String)
	 */
	public Hits search(File indexFilePath, String queryStr)throws IOException,
	            ParseException  {
		// TODO Auto-generated method stub
		Directory fsDir = FSDirectory.getDirectory(indexFilePath, false);
		IndexSearcher is = new IndexSearcher(fsDir);
		Query query = QueryParser.parse(queryStr, "contents", new StandardAnalyzer());
		long starttime = new Date().getTime();
		Hits hits = is.search(query);

		long endtime = new Date().getTime();
		System.out.println("Search the key word has elapsed "
				+ (endtime - starttime) + "ms");
		for (int i = 0; i < hits.length(); i++) {
			Document doc = hits.doc(i);
			System.out.println(doc.get("filename"));
			System.out.println(doc.toString());
		}

		return hits;
	}

}

 代码运行时还要符合DIP原则见具体实现类中的main()函数,word,pdf文件的索引可以再具体实现相应的类,任务与目标也就达到了。

 

 

分享到:
评论

相关推荐

    lucene实例lucene实例

    本篇文章将详细探讨Lucene的实例应用,以及如何通过实例来理解和掌握这一技术。 一、Lucene的基本概念 1. 文档(Document):在Lucene中,文档是信息的基本单位,可以理解为数据库中的一条记录,包含多个字段...

    lucene全文搜素实例 java lucene 实例

    **Lucene 全文搜索引擎实例:Java Lucene 实例** Lucene 是 Apache 软件基金会的一个开源项目,它提供了一个高性能、可扩展的信息检索库。这个实例将深入讲解如何在 Java 中使用 Lucene 来创建索引并执行各种搜索...

    经典的lucene实例代码及详细解析以及lucene结构流程介绍

    经典的Lucene实例代码及详细解析以及Lucene结构流程介绍 Lucene是一个功能强大且灵活的开源搜索引擎库,它提供了一个简单易用的API,允许开发者快速构建搜索应用程序。下面将对Lucene的实例代码和结构流程进行详细...

    lucene实例(包括分页、中文检索等)

    lucene实例是一个比较详细的例子,包括lucene的入门到高级实例,代码里有比较详细的实例,所有的实例都是通过junit来测试的。实例包括各种搜索:如通配符查询、模糊查询、查询结果的分页、中文分词器、自定义分词器...

    lucene实例

    Lucene是中国大百科全书,它是Java开发的全文搜索引擎库,为开发者提供了强大的文本搜索功能。...如果你对压缩包内的文件进行解压并运行,你将看到具体的操作步骤和示例代码,帮助你更好地理解和掌握Lucene。

    Java搜索工具——Lucene实例总结(一)

    Java搜索工具——Lucene实例总结(一) 在Java开发中,搜索引擎已经成为不可或缺的一部分,而Apache Lucene正是一个强大的全文搜索引擎库。这篇博文将带你深入理解Lucene的基本概念和使用方式,帮助你快速入门并掌握...

    java代码 结合 lucene 实现的 公交搜索系统

    java代码 结合 lucene 实现的 公交搜索系统 java代码 结合 lucene 实现的 公交搜索系统 java代码 结合 lucene 实现的 公交搜索系统 java代码 结合 lucene 实现的 公交搜索系统 java代码 结合 lucene 实现的 公交...

    Lucene索引器实例

    **Lucene索引器实例详解** Lucene是一个高性能、全文本搜索库,由Apache软件基金会开发,被广泛应用于各种搜索引擎的构建。它提供了一个高级的、灵活的、可扩展的接口,使得开发者能够轻松地在应用程序中实现全文...

    LUCENE实例

    CreateLucene.java 增量生成索引 CreateLuceneAll.java 全量生成索引 CreateLuceneInter.java 按照时间区间生成索引 里面为参考代码,使用lucene-core-2.9.0.jar

    lucene实例项目及其打包文件

    **Lucene实例项目及其打包文件详解** Lucene是一款强大的全文搜索引擎库,由Apache软件基金会开发,广泛应用于各种信息检索系统。这个实例项目是基于Lucene官网提供的,旨在帮助开发者更好地理解和使用Lucene进行...

    Lucene搜索实例

    **Lucene 搜索实例** Apache Lucene 是一个高性能、全文本搜索引擎库,它为开发者提供了在各种应用程序中实现全文检索的工具集。Lucene 并不是一个完整的应用,而是一个 Java 类库,可以被其他 Java 应用程序所使用...

    lucene3 例子

    【标题】:“Lucene3 实例解析” 在深入探讨Lucene3这个主题之前,我们先来了解一下Lucene是什么。Lucene是一个开源的全文检索库,由Apache软件基金会开发,广泛应用于各种信息检索系统中,包括搜索引擎、内容管理...

    一个关于lucene实例

    在这个"一个关于lucene实例"的压缩包中,很可能是为了帮助初学者理解并掌握Lucene的基本用法和核心概念。 Lucene的主要功能包括文档的索引和搜索。在索引过程中,它能够分析文本,将文本分词,然后将这些词语转换为...

    lucene检索实例代码,自己总结的非常详细

    文档中包含Lucene4.0.0版本jar包,中文分词器jar包,Lucene实例代码 1:建立索引 2:各种搜索方式方法 3:删除索引 4:检查索引文件 5:恢复删除的索引 6:强制删除 7:更新索引 8:合并索引 9:高亮回显 供大家参考...

    Lucene.NET 实例

    【Lucene.NET 实例】 Lucene.NET 是一个开源全文搜索引擎库,它是 Apache Lucene 的 .NET 版本,为 .NET 开发者提供了强大的搜索功能。Lucene.NET 提供了高性能、可扩展的文本搜索解决方案,广泛应用于各种应用程序...

    Lucene简单实例记录

    虽然原始内容中没有给出完整的搜索实例代码,但一般步骤包括: - 创建`IndexReader`和`IndexSearcher`对象,用于读取索引和执行查询。 - 构建查询,使用`QueryParser`或直接构建`Query`对象。 - 执行查询,获取结果...

    lucene部分常用代码

    Lucene常用代码详解 Lucene是一款功能强大且高效的搜索引擎,广泛应用于各种搜索应用程序中。为了更好地使用Lucene,需要了解其核心API和常用代码。本文将对Lucene部分常用代码进行详细解释,包括多字段搜索、多...

Global site tag (gtag.js) - Google Analytics