全文检索lucene学习笔记(一)

lucene: http://lucene.apache.org/java/docs/index.html

资料:

http://levi.bloghome.cn/posts/121531.html

http://www.javaeye.com/topic/165963

代码如下:

//生成索引:
package com.lucene.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;

public class Indexer {

	public static void main(String[] args) throws IOException {
		
		File indexDir = new File("C://test//index");
		File dataDir  = new File("C://test//data");
		
		int numIndexed = index(indexDir, dataDir);
		
		System.out.println(numIndexed); 
	}
	
	public static int index(File indexDir, File dataDir) throws IOException {
		
		if( !indexDir.exists() || !dataDir.isDirectory()) {
			throw new IOException();
		}
		
		IndexWriter writer = new IndexWriter(indexDir, new StandardAnalyzer(), true);
		writer.setUseCompoundFile(false);
		
		indexDirectory(writer, dataDir);
		
		int numIndexed = writer.docCount();
		writer.optimize();
		writer.close();
		return numIndexed;
	}
	
	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 if(f.getName().toLowerCase().endsWith(".txt")) {
				indexFile(writer, f);
			}
		}
	}
	
	public static void indexFile(IndexWriter writer, File f) throws IOException {
		
		if(f.isHidden() || !f.exists() || !f.canRead()) {
			return;
		}
		
		System.out.println("Indexing " + f.getCanonicalPath());
		
		Document doc = new Document();
		doc.add(new Field("filename", f.getCanonicalPath(), Field.Store.YES,Field.Index.UN_TOKENIZED));
		doc.add(new Field("contents", new FileReader(f)));
		writer.addDocument(doc);
	}
}

//查询代码:
package com.lucene.search;

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

import org.apache.lucene.analysis.standard.StandardAnalyzer;
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 {
	
	public static void main(String[] args) throws Exception {
		File indexDir = new File("C://test//index");
		String q = "ERROR";
		if (!indexDir.exists() || !indexDir.isDirectory()) {
			throw new IOException();
		}
		search(indexDir, q);
	}

	public static void search(File indexDir, String q) throws Exception {
		Directory fsDir = FSDirectory.getDirectory(indexDir);
		IndexSearcher searcher = new IndexSearcher(fsDir);
		QueryParser parser = new QueryParser("contents", new StandardAnalyzer());
		Query query = parser.parse(q);
		Hits hits = searcher.search(query);
		System.out.println("共有" + searcher.maxDoc() + "条索引,命中" + hits.length() + "条");
		for (int i = 0; i < hits.length(); i++) {
			int DocId = hits.id(i);
			String DocPath = hits.doc(i).get("filename");
			System.out.println(DocId + ":" + DocPath);
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值