apache lucene 一个最简单的实例
2009-05-21 14:10
549 查看
就像每个程序都有一个Hello World来让人体验它一样,lucene也可以很简单的提供一个实例。如下(来自lucene in action的例子)有两个类组成:
一个是建立索引
ok,这样就简单实现了,在搜索目录下所有txt,找出包括某一个字符串的txt文件名的功能。
下篇文章将介绍一下lucene的核心类
一个是建立索引
package my; import java.io.File; import java.io.FileReader; 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.document.Field; import org.apache.lucene.index.IndexWriter; package my; import java.io.File; import java.util.Date; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; 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 { 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); Query query = QueryParser.parse(q, "contents", new StandardAnalyzer()); long start = new Date().getTime(); 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 + "’:"); for (int i = 0; i < hits.length(); i++) { Document doc = hits.doc(i); System.out.println(doc.get("filename")); } } }
ok,这样就简单实现了,在搜索目录下所有txt,找出包括某一个字符串的txt文件名的功能。
下篇文章将介绍一下lucene的核心类
相关文章推荐
- apache lucene 一个最简单的实例
- apache lucene 一个最简单的实例
- 初学者,写了一个java下lucene全文检索简单实例,可直接运行
- 搜索引擎(建立一个简单的java Lucene实例)
- 章鱼哥出品_VB.NET 线程委托应用实例 一个简单的例子让你轻松掌握委托的用法
- 【转】jQuery+Ajax+PHP登录实例 一个简单的用Jquery中ajax方法登录的例子
- [WCF REST] 一个简单的REST服务实例
- 一个简单的tab选项卡实例
- [WCF REST] 一个简单的REST服务实例
- 一个简单的MySQL多实例环境搭建
- android一个简单的线程实例
- 一个处理用户登陆的servlet简单实例
- 一个从JSP到Servlet的简单小实例
- 用Lucene做一个简单的Java搜索工具
- 一个简单的JMS实例(使用ActiveMQ)
- 一个简单的遗传算法的应用实例(java代码)
- php和数据库结合的一个简单的web实例 代码分析 (php初学者)
- 一个简单的php MVC实例
- 一个非常的简单的图片copy实例,虽然简单却很实用
- 一个简单的WCF实例