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

学习lucene in action 的例子学习总结

阅读更多
  刚开始学习lucene,很多不熟,先买了本lucene原理分析与应用书,很适合自己看这类书,原理及各个类之间的关系讲得很好,但自己写起代码来不咋好使,后来下载lucene in action这本书,慢慢学习并动手搞一下例子。

   现在将学习点滴记录下来,便于日后查看
  1、环境:MyEclipse6.0+JDK 1.5+lucene 1.43(现在的lucene版本是2.4.0,为了配合lucene in action 中的例子,去下载了个lucene1.4.3 http://download.csdn.net/source/746086)

  2、先学一下indexer这个过程
     源代码照书抄了遍,哈哈,如下:

/*******************************************************************************
 * 该类主要功能及特点:use lucene's indexer Class to index text files
 * 
 * @see(与该类相关的类:)
 * 
 * 开发公司或单位:XX软件有限公司研发中心 版权:本软件版权归属XX公司研发中心
 * 
 * @author (作者):张XX
 * @since (该文件所支持的jdk版本):jdk1.5或以上
 * @version (版本):1.0
 * 
 * @date ():2009-2-20 最后更改日期:2009-2-20
 * 
 * 修改人:张XX
 * 
 * 复审人:张三,李四,王五
 * 
 */
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;
public class indexer {
	public static void main(String[] args) throws Exception {

		if (args.length != 2) {
			throw new Exception("usage:java " + indexer.class.getName()
					+ "<indexfilepath,datefilespath>");
		}
		// the path of original files
		File dataFilesPath = new File(args[0]);
		File indexFilesPath = new File(args[1]);
		System.out.println("indexFilesPath=" + indexFilesPath);// 由lucene建立产生的索引文件的输出路径
		System.out.println("dataFilesPath=" + dataFilesPath);// 这是将被索引的所有文本文件所在目录
		Long startTime = new Date().getTime();
		int filesNum = index(dataFilesPath, indexFilesPath);
		Long endTime = new Date().getTime();
		System.out.println("indexing " + filesNum + " files has consumed "
				+ (endTime - startTime) + " ms");
	}

	public static int index(File dataFilesPath, File indexFilePath)
			throws IOException {
		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);
		indexwr.setUseCompoundFile(false);
		indexwr.mergeFactor = 2;

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

		return indexedNum;
	}

	// recursive method that calls itself when it find a directorys;

	private static void indexDirectory(IndexWriter writer, File dir)
			throws IOException {
		File[] files = dir.listFiles();
		for (int i = 0; i < files.length; i++) {
			File f = files[i];
			System.out.println(f.getName());
			if (f.isDirectory()) {
				indexDirectory(writer, f);// recurse
			} else if (f.getName().endsWith(".txt")) {

				indexFile(writer, f);
			}

		}

	}

	private static void indexFile(IndexWriter writer, File f)
			throws IOException {

		/*
		 * if (!f.isHidden() || !f.exists() || !f.canRead()) { return; }
		 */

		System.out.println("it get 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.getName());

	}
}

具体含义lucene in action已讲得很清楚,问题总结:
   1)在eclipse中设置argments(本程序是控制台程序测试),Run>Open Run Dialog,在argments中加入c:\data c:\indexfile参数
   2)刚开始参数弄反了,运行后程序把data目录下的文件给全删除了(我辛苦下载的软件安装包也全删除,好险啊,没有C盘整个目录),郁闷吧 ,呵呵,第一次用这个玩意儿。
new IndexWriter(indexFilePath,new StandardAnalyzer(), rue)
;设置为true,会把原索引目录下的文件先全删除再重新建立索引,设置为false则按增量添加索引,要求索引目录至少建立一次相应索引,否则会报找不到相应索引文件异常。
   3)
if (!f.isHidden() || !f.exists() || !f.canRead()) { return; }
运行过不了,所以干脆注释掉,主要原因是前面一个条件不对 !f.isHidden(),算了,今天就写到这儿吧
分享到:
评论

相关推荐

    lucene in action 书中例子源码

    《Lucene in Action》是一本深受开发者喜爱的书籍,它深入浅出地介绍了Apache Lucene这个全文搜索引擎库的使用...如果需要进一步学习,建议配合阅读《Lucene in Action》的相应章节,以便更全面地掌握这些概念和技术。

    Lucene in action 2nd edition

    《Lucene in Action》第二版是一本不可多得的Lucene学习宝典,它不仅内容丰富、实用性强,而且涵盖了从入门到精通的各个阶段。无论是作为个人学习资料还是作为专业开发者的参考书,本书都能够提供有价值的指导和支持...

    LuceneInAction(第2版)_中文版.rar

    压缩包内的“LuceneInAction(第2版)_中文版.pdf”是该书的完整电子版,适合在各种设备上阅读。而“拼吾爱 - 最新编程资源的分享下载站.url”可能是一个链接,指向更多的编程资源网站,这为读者提供了进一步学习和...

    Lucene In Action second edition

    ### Lucene in Action 第二版:深入探索 Java 搜索技术 #### 一、引言 《Lucene in Action》第二版是一本专为希望利用 Apache Lucene 进行高效搜索功能开发的技术人员编写的权威指南。本书由 Michael McCandless、...

    Lucene in action second edition

    《Lucene in Action 第二版》是一本专为IT专业人士深度解析Apache Lucene搜索引擎库的权威著作。在十月份发布的新版本中,作者们对原有的内容进行了全面的更新和升级,以适应现代搜索技术的发展。这本书是Lucene...

    Lucene In Action

    《Lucene In Action》是一本深入探讨Apache Lucene搜索引擎库的专业书籍,主要针对英文读者。这本书详尽地介绍了如何在实际应用中利用Lucene进行全文检索、索引建设和搜索优化。Lucene是一个高性能、可扩展的全文...

    lucene in Action

    《Lucene in Action》是一本深入探讨开源全文检索引擎Lucene的专业书籍,它全面而详尽地介绍了如何在Java环境中构建高效、可扩展的搜索应用。Lucene是Apache软件基金会的一个项目,它为开发者提供了强大的文本分析和...

    Lucene in Action 2nd Edition

    当Lucene的第一次打五年前的场景,这是令人惊叹的。通过使用这个开源的,高度可扩展,超快速的...和第二版,字迹清晰,可重复使用的例子,和最佳做法,在行动的Lucene所无法比拟的意见,仍然是用Lucene开发的权威指南。

    Lucene_in_ Action

    《Lucene in Action》是一本专门介绍Apache Lucene搜索引擎库的详细指南,由Erik Hatcher、Otis Gospodnetic和Manning出版社合作出版。这本书深入浅出地讲解了如何安装、配置以及利用Lucene进行实际的开发工作。...

    Lucene.in.Action.2nd.Edition.pdf

    通过对《Lucene in Action 第2版》的深入学习,读者不仅可以掌握Lucene的使用方法,还能了解到搜索引擎领域的前沿技术和趋势。无论你是初学者还是经验丰富的开发者,这本书都能提供宝贵的知识和启示,帮助你在信息...

    基于lucene和nutch的开源搜索引擎资料集合

    JAVA_Lucene_in_Action教程完整版.doc Java开源搜索引擎.doc Linux下Nutch分布式配置和使用.pdf Lucene+Nutch源码.rar Lucene学习笔记.doc nutch_tutorial.pdf nutch二次开发总结.txt nutch入门.pdf nutch入门学习....

    Solr In Action 原版

    最后,《Solr In Action》书籍中的例子和最佳实践将引导读者完成一系列实际项目,从建立索引到执行搜索查询,再到后续的搜索结果展示,体现了从理论到实践的全方位应用,旨在帮助读者能够独立且高效地利用Solr进行...

    lucenceInAction 代码例子

    《LuceneInAction 代码例子》是针对Apache Lucene这一著名全文搜索引擎库的一份实践教程。Lucene是一个高性能、全文本搜索库,它为开发者提供了强大的文本搜索功能,可以用于构建复杂的检索应用。这个压缩包文件包含...

    Java例题源码第二章-solr-in-action:本书的示例代码、数据和配置

    《Java例题源码第二章-solr-in-action》是一个包含本书示例代码、数据和配置的资源包,主要用于帮助读者深入理解Java编程中的Solr搜索引擎应用。Solr是Apache Lucene项目的一个子项目,是一个高性能、全文本搜索...

    ZendFramework中文文档

    Available options (for this frontend in Zend_Cache factory) 4.3.6.3. Examples 4.4. Zend_Cache后端 4.4.1. Zend_Cache_Backend_File 4.4.2. Zend_Cache_Backend_Sqlite 4.4.3. Zend_Cache_Backend_...

Global site tag (gtag.js) - Google Analytics