Lucene初学备注

1,检索文件目录,创建index

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

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;

/**
 * @author Clay Zhong - Email: zjclay@gmail.com
 * @date Sep 7, 2008
 */
public class Indexer {
	public static int index(File indexDir, File dataDir) throws IOException {
		if (!dataDir.exists() || !dataDir.isDirectory()) {
			throw new IOException(dataDir + " does not exists or is not a directory");
		}

		IndexWriter writer = new IndexWriter(indexDir, new StandardAnalyzer(), true);
		writer.setUseCompoundFile(false);
		indexDirectory(writer, dataDir);

		int indexNbr = writer.docCount();
		writer.optimize();
		writer.close();

		return indexNbr;
	}

	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);
			}
			else {
				indexFile(writer, f);
			}
		}
	}

	private static void indexFile(IndexWriter writer, File file) throws IOException {
		if (!file.exists() || file.isHidden() || !file.canRead()) {
			return;
		}

		System.out.println("Index: " + file.getCanonicalPath());

		Document doc = new Document();
		doc.add(new Field("path", file.getCanonicalPath(), Field.Store.COMPRESS,
				Field.Index.UN_TOKENIZED));
		doc.add(new Field("content", new FileReader(file)));

		writer.addDocument(doc);
	}
}

 

2,检索index目录,查找文档对象

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.index.IndexReader;
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;

/**
 * @author Clay Zhong - Email: zjclay@gmail.com
 * @date Sep 7, 2008
 */
public class Searcher {
	public static void search(File indexDir, String queryString) throws IOException, ParseException {
		IndexReader reader = IndexReader.open(indexDir);
		org.apache.lucene.search.Searcher searcher = new IndexSearcher(reader);
		QueryParser parser = new QueryParser("content", new StandardAnalyzer());
		Query query = parser.parse(queryString);

		long start = new Date().getTime();
		Hits hits = searcher.search(query);
		long end = new Date().getTime();

		System.out.println("Found " + hits.length() + " document(s) (in " + (end - start)
				+ " milliseconds) that matched query: '" + queryString + "'");

		for (int i = 0; i < hits.length(); i++) {
			Document doc = hits.doc(i);
			System.out.println(hits.id(i) + " - " + hits.score(i) + " - " + doc.get("path"));
		}
	}
}

 

3,运行Demo,查看索引结果

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

/**
 * @author Clay Zhong - Email: zjclay@gmail.com
 * @date Sep 6, 2008
 */
public class Demo {
	public static void main(String[] args) {
		try {
			if (true) {
				long start = new Date().getTime();
				int indexingNbr = Indexer.index(new File("/home/clay/work/lucene/index"), new File(
						"/home/clay/work/lucene/data"));
				long end = new Date().getTime();

				System.out.println("Indexing:" + indexingNbr + " files, took " + (end - start)
						+ " milliseconds");
			}
			else {
				Searcher.search(new File("/home/clay/work/lucene/index"), "junit");
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值