`
42087743
  • 浏览: 235749 次
  • 性别: Icon_minigender_1
  • 来自: 合肥&上海
社区版块
存档分类
最新评论

lucene-1.4入门例子的详细注释

阅读更多
[size=small]才开始学习lucene,所以从网上找了个不错的入门例子来看,并且加了很详细的注释,假如有不对的地方,还请大家指正:
Indexer.java
// 索引生成类
public class Indexer {
	// 传入2个参数,一个是索引文件路径,一个是数据文件路径
	public static void main(String[] args) throws Exception {
		// 如果没有传入2个文件,则抛出异常
		if (args.length != 2) {
			throw new Exception("Usage: java " + Indexer.class.getName()
					+ " <index dir> <data dir>");
		}
		// 读取索引文件
		File indexDir = new File(args[0]);
		// 读取数据文件
		File dataDir = new File(args[1]);
		long start = new Date().getTime();
		// 调用index方法生成索引
		int numIndexed = index(indexDir, dataDir);
		long end = new Date().getTime();
		// 打印文件数目和花费时间
		System.out.println("Indexing " + numIndexed + " files took "
				+ (end - start) + " milliseconds");
	}

	// open an index and start file directory traversal
	public static int index(File indexDir, File dataDir) throws IOException {
		// 如果2个文件有一个找不到,则抛出异常
		if (!dataDir.exists() || !dataDir.isDirectory()) {
			throw new IOException(dataDir
					+ " does not exist or is not a directory");
		}
		// IndexWriter: 创建和维护索引的类
		// StandardAnalyzer: 分析器,筛选出无用的词语
		// true: 是否重建索引. false: 扩展索引
		IndexWriter writer = new IndexWriter(indexDir, new StandardAnalyzer(),
				true);
		// true:在创建索引库时,会合并多个 Segments 文件到一个 .cfs 中。此方式有助于减少索引文件数量,减少同时打开的文件数量。
		writer.setUseCompoundFile(false);
		// 读取数据文件
		indexDirectory(writer, dataDir);
		// 得到当前索引文件中的文件数量
		int numIndexed = writer.docCount();
		/*
		 * IndexWriter为了减少大量的io维护操作,在每得到一定量的索引后建立新的小索引文件(笔者测试索引批量的最小单位为10),
		 * 然后再定期将它们整合到一个索引文件中,因此在索引结束时必须进行 writer.optimize(),以便将所有索引合并优化。
		 */
		writer.optimize();
		// 关闭对象
		writer.close();
		// 返回索引文件的文件数量
		return numIndexed;
	}

	// recursive method that calls itself when it finds a directory
	// 读取数据文件,假如是目录则遍历
	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];
			// 假如读入的是目录,那么递归调用自己
			if (f.isDirectory()) {
				indexDirectory(writer, f);
				// 假如读入的是txt文件,读取它
			} else if (f.getName().endsWith(".txt")) {
				indexFile(writer, f);
			}
		}
	}

	// method to actually index file using Lucene
	// 将一个文件读取到IndexWriter对象中去
	private static void indexFile(IndexWriter writer, File f)
			throws IOException {
		// 假如准备读取的数据文件为隐藏,不存在,不能读,则返回不进行操作
		if (f.isHidden() || !f.exists() || !f.canRead()) {
			return;
		}
		// 打印出数据文件的路径
		System.out.println("Indexing " + f.getCanonicalPath());
		// Document:类似一条数据库中的一条数据
		// Field:类似字段
		Document doc = new Document();
		// Text(String,Reader):本方法适用于简短的文本,如title,subject,String可以被检索,但是不可以从检索结果中得到
		doc.add(Field.Text("contents", new FileReader(f)));
		// Keyword(String,String):本方法适用在date和url上,String可以被检索,也可以从检索结果中得到
		doc.add(Field.Keyword("filename", f.getCanonicalPath()));
		// 索引添加一个文档
		writer.addDocument(doc);
	}
}


Searcher.java
// 检索类
public class Searcher {
	// 传入2个参数,目录文件名和检索关键字
	public static void main(String[] args) throws Exception {
		if (args.length != 2) {
			throw new Exception("Usage: java " + Searcher.class.getName()
					+ " <index dir> <auery>");
		}
		File indexDir = new File(args[0]);
		String q = args[1];
		if (!indexDir.exists() || !indexDir.isDirectory()) {
			throw new Exception(indexDir
					+ " does not exist or is not a directory.");
		}
		search(indexDir, q);
	}

	public static void search(File indexDir, String q) throws Exception {
		// 将目录文件的目录封装成对象
		Directory fsDir = FSDirectory.getDirectory(indexDir, false);
		// 创建查询器,在提供的目录上查找索引文件
		IndexSearcher is = new IndexSearcher(fsDir);
		// QueryParser.parse():1.检索的关键字;2.检索的范围,常用的是subject,text,contents,title等等;3.分析器
		Query query = QueryParser.parse(q, "contents", new StandardAnalyzer());
		long start = new Date().getTime();
		// 使用查询器进行查询,Hit是结果集
		Hits hits = is.search(query);
		long end = new Date().getTime();
		System.err.println("Found " + hits.length() + " document(s) (in "
				+ (end - start) + " milliseconds) that matched query ‘" + q
				+ "’:");
		// 遍历hit结果集
		for (int i = 0; i < hits.length(); i++) {
			// 返回第i个的文档的所有字段
			Document doc = hits.doc(i);
			System.out.println(doc.get("filename"));
		}
	}
}


[/size]
分享到:
评论
5 楼 42087743 2009-07-02  
xsjleilei 写道
你的代码可以运行,嘿嘿。又到了lucene2.4?

lucene的最新版本2.4
4 楼 xsjleilei 2009-06-28  
你的代码可以运行,嘿嘿。又到了lucene2.4?
3 楼 xsjleilei 2009-06-28  
谢谢楼主,又到了lucene2.4,继续探索
2 楼 42087743 2009-06-23  
直接跑main方法就可以了,推荐学lucene2.4,现在学习,1.4已经不适宜了
1 楼 xsjleilei 2009-06-22  
怎么run?看不懂,

相关推荐

    lucene-1.4-final

    在"lucene-1.4-final"压缩包中,包含了Lucene这个版本的所有核心组件和相关资源,便于开发者研究和应用。 1. **索引过程**:Lucene的索引过程主要包括分析、文档存储和倒排索引的构建。在这个版本中,用户可以...

    lucene-analyzers-smartcn-7.7.0-API文档-中文版.zip

    赠送jar包:lucene-analyzers-smartcn-7.7.0.jar; 赠送原API文档:lucene-analyzers-smartcn-7.7.0-javadoc.jar; 赠送源代码:lucene-analyzers-smartcn-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-...

    lucene-core-7.7.0-API文档-中文版.zip

    赠送jar包:lucene-core-7.7.0.jar; 赠送原API文档:lucene-core-7.7.0-javadoc.jar; 赠送源代码:lucene-core-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-core-7.7.0.pom; 包含翻译后的API文档:lucene...

    最新全文检索 lucene-5.2.1 入门经典实例

    《最新全文检索 Lucene-5.2.1 入门经典实例》 Lucene是一个开源的全文检索库,由Apache软件基金会开发,广泛应用于各种信息检索系统。在5.2.1版本中,Lucene提供了更为高效和强大的搜索功能,为开发者提供了构建...

    lucene-analyzers-common-6.6.0-API文档-中文版.zip

    赠送jar包:lucene-analyzers-common-6.6.0.jar; 赠送原API文档:lucene-analyzers-common-6.6.0-javadoc.jar; 赠送源代码:lucene-analyzers-common-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-...

    lucene-suggest-6.6.0-API文档-中文版.zip

    赠送jar包:lucene-suggest-6.6.0.jar; 赠送原API文档:lucene-suggest-6.6.0-javadoc.jar; 赠送源代码:lucene-suggest-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-suggest-6.6.0.pom; 包含翻译后的API...

    lucene-core-7.2.1-API文档-中文版.zip

    赠送jar包:lucene-core-7.2.1.jar; 赠送原API文档:lucene-core-7.2.1-javadoc.jar; 赠送源代码:lucene-core-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-core-7.2.1.pom; 包含翻译后的API文档:lucene...

    lucene-backward-codecs-7.3.1-API文档-中英对照版.zip

    赠送jar包:lucene-backward-codecs-7.3.1.jar; 赠送原API文档:lucene-backward-codecs-7.3.1-javadoc.jar; 赠送源代码:lucene-backward-codecs-7.3.1-sources.jar; 赠送Maven依赖信息文件:lucene-backward-...

    lucene-core-6.6.0-API文档-中文版.zip

    赠送jar包:lucene-core-6.6.0.jar; 赠送原API文档:lucene-core-6.6.0-javadoc.jar; 赠送源代码:lucene-core-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-core-6.6.0.pom; 包含翻译后的API文档:lucene...

    lucene-4.7.0全套jar包

    - **Contrib模块**:包含社区贡献的扩展功能,可能包括特殊分词器、搜索建议等,如`lucene-join-4.7.0.jar`、`lucene-suggest-4.7.0.jar`等。 使用这些JAR包时,开发者需要根据具体需求选择合适的模块,并确保它们...

    lucene-spatial-extras-7.3.1-API文档-中英对照版.zip

    赠送jar包:lucene-spatial-extras-7.3.1.jar; 赠送原API文档:lucene-spatial-extras-7.3.1-javadoc.jar; 赠送源代码:lucene-spatial-extras-7.3.1-sources.jar; 赠送Maven依赖信息文件:lucene-spatial-extras...

    lucene-analyzers-smartcn-7.7.0-API文档-中英对照版.zip

    赠送jar包:lucene-analyzers-smartcn-7.7.0.jar; 赠送原API文档:lucene-analyzers-smartcn-7.7.0-javadoc.jar; 赠送源代码:lucene-analyzers-smartcn-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-...

    lucene-memory-6.6.0-API文档-中文版.zip

    赠送jar包:lucene-memory-6.6.0.jar; 赠送原API文档:lucene-memory-6.6.0-javadoc.jar; 赠送源代码:lucene-memory-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-memory-6.6.0.pom; 包含翻译后的API文档...

    lucene-suggest-7.7.0-API文档-中文版.zip

    赠送jar包:lucene-suggest-7.7.0.jar; 赠送原API文档:lucene-suggest-7.7.0-javadoc.jar; 赠送源代码:lucene-suggest-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-suggest-7.7.0.pom; 包含翻译后的API...

    lucene-spatial-extras-7.2.1-API文档-中英对照版.zip

    赠送jar包:lucene-spatial-extras-7.2.1.jar; 赠送原API文档:lucene-spatial-extras-7.2.1-javadoc.jar; 赠送源代码:lucene-spatial-extras-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-spatial-extras...

    lucene-sandbox-7.2.1-API文档-中文版.zip

    赠送jar包:lucene-sandbox-7.2.1.jar; 赠送原API文档:lucene-sandbox-7.2.1-javadoc.jar; 赠送源代码:lucene-sandbox-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-sandbox-7.2.1.pom; 包含翻译后的API...

    lucene-spatial-6.6.0-API文档-中文版.zip

    赠送jar包:lucene-spatial-6.6.0.jar; 赠送原API文档:lucene-spatial-6.6.0-javadoc.jar; 赠送源代码:lucene-spatial-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-spatial-6.6.0.pom; 包含翻译后的API...

    lucene-misc-6.6.0-API文档-中文版.zip

    赠送jar包:lucene-misc-6.6.0.jar; 赠送原API文档:lucene-misc-6.6.0-javadoc.jar; 赠送源代码:lucene-misc-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-misc-6.6.0.pom; 包含翻译后的API文档:lucene...

    lucene-spatial-extras-6.6.0-API文档-中英对照版.zip

    赠送jar包:lucene-spatial-extras-6.6.0.jar; 赠送原API文档:lucene-spatial-extras-6.6.0-javadoc.jar; 赠送源代码:lucene-spatial-extras-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-spatial-extras...

    lucene-backward-codecs-6.6.0-API文档-中文版.zip

    赠送jar包:lucene-backward-codecs-6.6.0.jar; 赠送原API文档:lucene-backward-codecs-6.6.0-javadoc.jar; 赠送源代码:lucene-backward-codecs-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-backward-...

Global site tag (gtag.js) - Google Analytics