lucene中搜索例子实践总结二

 

        今天下午按lucene in action调试了建立文本文件索引的例子,晚上又来调试一下基于索引的搜索例子过程(体会一下空间换取时间的感觉,呵呵),先回顾一下建立索引的过程:

     1)利用IndexWriter对象及StandarAnalyzer关联输出的索引文件夹;

     2)对IndexWriter对象进行相关设置,利用递归遍历被索引的文件夹所有文本文件并建立filed ,document为结构的索引。

      3)IndexWriter对象调用close方法,正式将建立的索引文件存入磁盘索引文件夹中。

 

下面在索引的基础上来写搜索例子,照书抄了一下,呵呵。代码如下:

     /*******************************************************************************

 * 该类主要功能及特点:use lucene's Query Class and Indexsearcher Class to search keywords
 * from index dictionary of text files
 * 
 * @see(与该类相关的类:)
 * 
 * 开发公司或单位:XX软件有限公司研发中心 版权:本软件版权归属XX公司研发中心
 * 
 * @author (作者):张XX
 * @since (该文件所支持的jdk版本):jdk1.5或以上
 * @version (版本):1.0
 * 
 * @date ():2009-2-22 最后更改日期:2009-2-22
 * 
 * 修改人:张XX
 * 
 * 复审人:张三,李四,王五
 * 
 */
// *************************************************************************************
package com.goodwitkey.searcher;

import java.io.File;
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.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;

public class Searcher {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		if (args.length != 2) {
			throw new Exception("java: " + Searcher.class.getName()
					+ "<indexdir indir>,<query>");
		}
		File indexdir = new File(args[0]);
		String qr = args[1];
		if ((!indexdir.exists()) || (!indexdir.isDirectory())) {
			throw new Exception(indexdir.toString()
					+ "is not exist or is not a directory");
		}

		search(indexdir, qr);
	}

	/**
	 * @param indexdir
	 * @param query
	 * @throws IOException
	 * @throws ParseException
	 */
	public static void search(File indexdir, String qr) throws IOException,
			ParseException {
		Directory fsDir = FSDirectory.getDirectory(indexdir, false);
		IndexSearcher is = new IndexSearcher(fsDir);
		Query query = QueryParser.parse(qr, "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"));
		}
	}

}

 

 

 这就没有遇到什么问题,比较顺利,运行结果如下:

    Search the key word has elapsed 16ms

C:\data\赖昌星.txt
Document<Keyword<filename:C:\data\赖昌星.txt>>

 总结一下流程:

     1)用SearcherWriter对象来指向索引目录(用FSDirectory类完成);

     2)用Query的具体对象来解析要搜索的关键字或词;

      3) 执行搜索并将得到结果放到hits容器中。

你可能感兴趣的:(apache,jdk,C++,c,Lucene)