`
okwangxing
  • 浏览: 28736 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Lucene之Helloworld

阅读更多
Lucene不是一个完整搜索引擎,不具备爬虫功能,管理界面之类的功能,可其部分之项目中实现了网站的搜索引擎,Nutch就是其中的一个,基于Lucene实现的搜索引擎应用.  .

本文记录下自己的学习点点滴滴,实现一个简单的程序,
Hello world 之实现文本搜索
这里没应用中文分词的东西,可以参照庖丁解牛的项目,svn中已经上传了代码,上面有针对lucene3.0的.感兴趣的可自行试验.
SVN地址
svn checkout http://paoding.googlecode.com/svn/trunk/ paoding-read-only

项目是利用Maven构建的,自从开始用Maven就是疯狂的爱上了她.个人推荐使用!
Maven pom.xml
<dependency>
	<groupId>log4j</groupId>
	<artifactId>log4j</artifactId>
	<version>1.2.15</version>
</dependency>
<dependency>
	<groupId>commons-logging</groupId>
	<artifactId>commons-logging</artifactId>
	<version>1.1.1</version>
</dependency>
<dependency>
	<groupId>org.apache.lucene</groupId>
	<artifactId>lucene-core</artifactId>
	<version>3.0.0</version>
</dependency>


对索引的提取,数据是自己造的.下面提供下载.
Index.java
import java.io.File;
import java.io.FileReader;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.analysis.tokenattributes.TermAttribute;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

/**
 * @author ruodao
 * @since 1.0 2010-2-23 下午09:39:10
 */
public class Index {
	public static void main(String[] args) throws Exception {
		String indexDir = "E:\\Temp\\index";
		String dataDir = "E:\\Temp\\data";

		long start = System.currentTimeMillis();
		Index indexer = new Index(indexDir);
		int numIndex = indexer.index(dataDir);

		indexer.close();

		long end = System.currentTimeMillis();

		System.out.println("Indexing " + numIndex + " files tooks "
				+ (end - start) + " millisenconds");
	}

	private IndexWriter writer;
	private Analyzer analyzer;
	
	private static final Log logger = LogFactory.getLog(Index.class);

	public Index(String indexDir) throws Exception {
		Directory dir = FSDirectory.open(new File(indexDir));
		analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
		writer = new IndexWriter(dir, analyzer, MaxFieldLength.UNLIMITED);
	}

	public void close() throws Exception {
		writer.close();
	}

	public int index(String dataDir) throws Exception {
		File[] files = new File(dataDir).listFiles();
		for (File f : files) {
			if (!f.isDirectory() && !f.isHidden() && f.exists() && f.canRead()
					&& acceptFile(f)) {
				indexFile(f);
			}
		}
		return writer.numDocs();
	}

	protected boolean acceptFile(File f) {
		return f.getName().endsWith(".txt");
	}

	protected Document getDocument(File f) throws Exception {
		Document doc = new Document();
		doc.add(new Field("contents", new FileReader(f)));
		doc.add(new Field("filename", f.getCanonicalPath(), Store.YES,
				org.apache.lucene.document.Field.Index.NOT_ANALYZED));
		return doc;
	}

	private void indexFile(File f) throws Exception {
		System.out.println("Index " + f.getCanonicalPath());
		
		Document doc = getDocument(f);
		if (doc != null) {
			writer.addDocument(doc);
		}
		
		
		//查看分词情况  可选代码
		TokenStream ts = analyzer.tokenStream("contents", new FileReader(doc
				.get("filename")));
		ts.addAttribute(TermAttribute.class);

		while (ts.incrementToken()) {
			TermAttribute ta = ts.getAttribute(TermAttribute.class);
			logger.debug("{" + ta.term() + "}");
		}
	}
}


数据准备好了,也该提供给别人使用吧,一个简单的搜索.
Searcher.java
import java.io.File;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

/**
 * @author ruodao
 * @since 1.0 2010-2-23 下午10:19:06
 */
public class Searcher {
	public static void main(String[] args) throws Exception {
		String indexDir = "E:\\Temp\\index";
		String q = "中";

		searc(indexDir, q);
	}

	private static void searc(String indexDir, String q) throws Exception {
		Directory dir = FSDirectory.open(new File(indexDir), null);
		Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
		IndexSearcher is = new IndexSearcher(dir);
		QueryParser parser = new QueryParser(Version.LUCENE_CURRENT,
				"contents", analyzer);
		Query query = parser.parse(q);
		long start = System.currentTimeMillis();

		TopDocs hits = is.search(query, 10);

		long end = System.currentTimeMillis();

		System.err.println("Found " + hits.totalHits + " Document(s) (in )"
				+ (end - start) + "milliseconds) that matched query '" + q
				+ "':");
		for (int j = 0; j < hits.scoreDocs.length; j++) {
			ScoreDoc scoreDoc = hits.scoreDocs[j];
			Document doc = is.doc(scoreDoc.doc);
			System.out.println(doc.get("filename"));
		}
		is.close();
	}
}

一个简单而完整的程序已经完成了.可以实验实验.

--EOF--
分享到:
评论
3 楼 java-xb 2012-10-19  
夜神月 写道
按照楼主的文章,建立项目,将代码跑起来,建立索引,而后查询,总是查找为0,求答案

中文的检索内容好像不应 试试英文的
2 楼 夜神月 2011-08-12  
按照楼主的文章,建立项目,将代码跑起来,建立索引,而后查询,总是查找为0,求答案
1 楼 ladybird2010 2010-02-25  
求Lucene结合Hibernate的配置实例 急。。
您若有Lucene的例子工程,帮忙发一个好吗?最好是可以分词。
Email: gao.guangpei@zte.com.cn 或者ggp123@126.com
非常感谢你!

相关推荐

    lucene:lucene构建索引,从HelloWorld到项目具体使用

    lucene构建索引,从HelloWorld到项目具体使用 ====================项目一:=================================== qianjun.lucene.first ====================项目二:=================================== qianjun....

    lucene-入门

    概述 Lucene简介 Lucene架构原理 Lucene应用示例(Hello World)

    Lucene演示

    Lucene代码演示,Helloworld,简单演示

    Struts Web设计与开发大全

    17章:Hello World类和Ant脚本及基本Web应用结构和Ant脚; 18章:Hello World类和测试类以及Struts测试工程; 19章:Struts与Hibernate结合应用; 20章:lucene1全文检索应用,直接复制到tomcat的webapps目录下...

    基于SSM框架构建积分系统和基本商品检索系统源码.zip

    【资源说明】 1、该资源包括项目的全部源码,下载可以直接使用!...#### (三)[ Redis系列(一)--安装、helloworld以及读懂配置文件](http://blog.csdn.net/jack__frost/article/details/67633975

    涵盖了90%以上的面试题

    如何在main方法执行前输出”hello world” java程序的初始化顺序 请说出作用域public,private,protected,以及不写时的区别 为什么java中有些接口没有任何方法 java中的clone方法有什么作用 面向对象有哪些特征 ...

    JAVA上百实例源码以及开源项目

     Java访问权限控制,为Java操作文件、写入文件分配合适的权限,定义写到文件的信息、定义文件,输出到c:/hello.txt、写信息到文件、关闭输出流。 Java绘制图片火焰效果 1个目标文件 摘要:Java源码,图形操作,火焰...

    JAVA上百实例源码以及开源项目源代码

     Java访问权限控制,为Java操作文件、写入文件分配合适的权限,定义写到文件的信息、定义文件,输出到c:/hello.txt、写信息到文件、关闭输出流。 Java绘制图片火焰效果 1个目标文件 摘要:Java源码,图形操作,火焰...

Global site tag (gtag.js) - Google Analytics