- 浏览: 163712 次
- 来自: ...
文章分类
最新评论
-
hellostory:
不知所云………………
如何使用反射访问类的private域和方法 -
xusong_zidingyi:
如果在linux下面的话就没有办法运行了,因为linux没有办 ...
使用tesseract破解checkCode -
javajava22:
Errors accessing files. There m ...
使用tesseract破解checkCode -
suncathay:
qljobs 写道cmd.add("E:/Works ...
使用tesseract破解checkCode -
zuoxu128:
TIFFImageWriteParam ,这个类是哪个包的啊
使用tesseract破解checkCode
lucene3.0已于2009-11-25发布啦,但网上的入门实例都是针对lucene3.0以前的,相对于以前的版本,貌似改动不小。
本人从头开始学习lucene,现在用的是《lucene in action中文版》,结合lucene3.0文档写了个入门实例,可供像我一样直接从lucene3.0开始学习的新手参考!
入门实例:
1.预处理:先把网上下载的一个《三国演义》电子书“三国演义.txt”(可用其他代替,呵呵)切割成多个小文件。
/** * @author ht * 预处理 * */ public class FilePreprocess { public static void main(String[] arg){ String outputpath = "D:\\test\\small\\";//小文件存放路径 String filename = "D:\\test\\三国演义.txt";//原文件存放路径 if(!new File(outputpath).exists()){ new File(outputpath).mkdirs(); } splitToSmallFiles(new File(filename), outputpath); } /**大文件切割为小的 * @param file * @param outputpath */ public static void splitToSmallFiles(File file ,String outputpath){ int filePointer = 0; int MAX_SIZE = 10240; String filename = "output"; BufferedWriter writer = null; try { BufferedReader reader = new BufferedReader(new FileReader(file)); StringBuffer buffer = new StringBuffer(); String line = reader.readLine(); while(line != null){ buffer.append(line).append("\r\n"); if(buffer.toString().getBytes().length>=MAX_SIZE){ writer = new BufferedWriter(new FileWriter(outputpath+filename+filePointer+".txt")); writer.write(buffer.toString()); writer.close(); filePointer++; buffer=new StringBuffer(); } line = reader.readLine(); } writer = new BufferedWriter(new FileWriter(outputpath+filename+filePointer+".txt")); writer.write(buffer.toString()); writer.close(); System.out.println("The file hava splited to small files !"); } catch (FileNotFoundException e) { System.out.println("file not found !"); e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
2.用lucene3.0生成索引类:用lencene3.0对生成的多个小文件进行索引,中文分词用的是lucene3.0自带的StandardAnalyzer.
/** * @author ht * 索引生成 * */ public class Indexer { private static String INDEX_DIR = "D:\\test\\index";//索引存放目录 private static String DATA_DIR = "D:\\test\\small\\";//小文件存放的目录 public static void main(String[] args) throws Exception { long start = new Date().getTime(); int numIndexed = index(new File(INDEX_DIR), new File(DATA_DIR));//调用index方法 long end = new Date().getTime(); System.out.println("Indexing " + numIndexed + " files took " + (end - start) + " milliseconds"); } /**索引dataDir下的.txt文件,并储存在indexDir下,返回索引的文件数量 * @param indexDir * @param dataDir * @return int * @throws IOException */ public static int index(File indexDir, File dataDir) throws IOException { if (!dataDir.exists() || !dataDir.isDirectory()) { throw new IOException(dataDir + " does not exist or is not a directory"); } IndexWriter writer = new IndexWriter(FSDirectory.open(indexDir), new StandardAnalyzer(Version.LUCENE_CURRENT), true, IndexWriter.MaxFieldLength.LIMITED);//有变化的地方 indexDirectory(writer, dataDir); int numIndexed = writer.numDocs(); writer.optimize(); writer.close(); return numIndexed; } /**循环遍历目录下的所有.txt文件并进行索引 * @param writer * @param dir * @throws IOException */ 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); // recurse } else if (f.getName().endsWith(".txt")) { indexFile(writer, f); } } } /**对单个txt文件进行索引 * @param writer * @param f * @throws IOException */ private 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("contents",new FileReader(f)));//有变化的地方 doc.add(new Field("filename",f.getCanonicalPath(),Field.Store.YES, Field.Index.ANALYZED));//有变化的地方 writer.addDocument(doc); } }
3.查询类:查询“玄德”!
/** * @author ht * 查询 * */ public class Searcher { private static String INDEX_DIR = "D:\\test\\index\\";//索引所在的路径 private static String KEYWORD = "玄德";//关键词 private static int TOP_NUM = 100;//显示前100条结果 public static void main(String[] args) throws Exception { File indexDir = new File(INDEX_DIR); if (!indexDir.exists() || !indexDir.isDirectory()) { throw new Exception(indexDir + " does not exist or is not a directory."); } search(indexDir, KEYWORD);//调用search方法进行查询 } /**查询 * @param indexDir * @param q * @throws Exception */ public static void search(File indexDir, String q) throws Exception { IndexSearcher is = new IndexSearcher(FSDirectory.open(indexDir),true);//read-only String field = "contents"; QueryParser parser = new QueryParser(Version.LUCENE_CURRENT, field, new StandardAnalyzer(Version.LUCENE_CURRENT));//有变化的地方 Query query = parser.parse(q); TopScoreDocCollector collector = TopScoreDocCollector.create(TOP_NUM , false);//有变化的地方 long start = new Date().getTime();// start time is.search(query, collector); ScoreDoc[] hits = collector.topDocs().scoreDocs; System.out.println(hits.length); for (int i = 0; i < hits.length; i++) { Document doc = is.doc(hits[i].doc);//new method is.doc() System.out.println(doc.getField("filename")+" "+hits[i].toString()+" "); } long end = new Date().getTime();//end time System.out.println("Found " + collector.getTotalHits() + " document(s) (in " + (end - start) + " milliseconds) that matched query '" + q + "':"); } }
4.结果就不贴啦,反正能运行就是啦
转载自:http://cumtfirefly.iteye.com/blog/543664
- lucene3.0入门实例源码.rar (3 KB)
- 下载次数: 55
发表评论
-
一些随想
2012-06-01 22:05 0需求 idea 方案 执 ... -
Redis的安装与使用
2011-10-24 20:04 1234今天看了下redis相关的东东,做个备忘。 Re ... -
Redis的安装与使用
2011-10-24 20:00 0Redis 服务端: http://cocos.iteye. ... -
GenericDAO
2011-07-31 14:49 0GenericDAO -
loadfromproperties
2011-07-31 14:48 0loadfromproperties -
resultcode
2011-07-31 14:47 0resultcode -
berkeleydb的使用
2011-10-24 20:05 1802在napoli的代码中发现这个berkeleydb,版本为3. ... -
几种简单的Load Balance方法
2011-07-31 13:44 0以下代码出自napoli (1)定义Selec ... -
参数断言工具
2011-07-31 12:58 907见代码: public final class Argume ... -
如何检测线上代码的运行情况---BTrace使用分享
2011-04-10 17:36 2035(1)你是否有时发现线上的代码运行效率不高,但却无法定位到底具 ... -
napoli和JMS,mq的一些知识
2010-11-26 21:01 01 jms和mq (1)jms activeMQ 应用 h ... -
Taglib基础知识
2010-11-26 12:41 0浅论taglib设计 : http://wenku.bai ... -
java编码问题的一些文章
2010-11-11 17:30 0http://www.iteye.com/topic/8047 ... -
URL在转码的过程中数据丢失
2010-10-25 14:52 0示例代码 import java.io.Unsuppo ... -
遍历大容量map的正确方法
2010-10-08 13:20 11072首先,遍历map有以下方法: import java.uti ... -
B2B-Code Review规范
2010-08-31 10:15 01.目标 凡事知其然还要知其所以然,我们首先需要知道什么 ... -
多线程书写及code review规范
2010-08-31 10:09 14101. 不允许将Calendar用于类的静态成员 ... -
域名白名单检查
2010-08-24 21:05 0首先准备一份白名单名为safe-domain.xml ,如下: ... -
如何使用反射访问类的private域和方法
2010-08-17 21:44 5087如何使用反射访问类的private域和方法?非常简单,由 ... -
FindBugs插件的安装与使用
2010-04-21 10:54 18671 什么是FindBugs FindBugs 是一个静态分析 ...
相关推荐
lucene 3.0 入门实例项目,迅速教会你怎么使用3.0API
Lucene 创建索引查询的入门实例,可以直接导入工具
lucene3.0 lucene3.0 lucene3.0 lucene3.0 lucene3.0
NULL 博文链接:https://cuics-100.iteye.com/blog/1057266
Lucene3.0 使 用 教 程 Lucene3.0 使 用 教 程 Lucene3.0 使 用 教 程 Lucene3.0 使 用 教 程 Lucene3.0 使 用 教 程 Lucene3.0 使 用 教 程 Lucene3.0 使 用 教 程 Lucene3.0 使 用 教 程 Lucene3.0 使 用 教 程 ...
lucene 3.0 API中文帮助,学习的人懂得的
lucene3.0 实例,在jdk1.5,lucene3.0下调式通过,可以直接运行。先运行生成索引文件的class,在运行搜索的class。
Lucene3.0特性Lucene3.0特性
Lucene3.0之查询处理(1):原理和查询类型 各种Query对象详解
Lucene3.0和Lucene2.0API有多处改动,该实例用Luence3.0实现
Lucene 3.0完成入门 轻松入门,内有七个文档帮助你了解并实现简单的搜索
Lucene 3.0 原理与代码分析完整版
lucene3.0 中文分词器, 庖丁解牛
传智播客Lucene3.0课程,Lucene3.0的入门教程.
lucene3.0 入门 这个资源对于入门搜索引擎开发的人来说是非常好的
lucene3.0的核心jar包文件,lucene3.0的核心jar包文件,lucene3.0的核心jar包文件,lucene3.0的核心jar包文件。
lucene3.0使用介绍及实例,从不同的使用方面介绍了lucene的使用及相应的实例。
NULL 博文链接:https://zhuliang1984723.iteye.com/blog/1190886
lucene3.0-highlighter.jar lucene3.0的高亮jar包,从lucene3.0源码中导出来的
实例是一个java实例,可直接导入到MyEclipse中使用。 其中是lucene3.0整合了庖丁解牛分词法,添加了...因为lucene3.0无法整合paoding-analysis.jar 所以我已经把paoding-analysis中的源码整合进来了避免无法整合问题