- 浏览: 156090 次
- 性别:
- 来自: 无锡
文章分类
最新评论
-
lgh1992314:
1.7public <T> List<T&g ...
Commons DbUtils 源码阅读四 -
lgh1992314:
it was too slow to call the met ...
Commons DbUtils 源码阅读四 -
lgh1992314:
toBeanList的存在位置有点变态
Commons DbUtils 源码阅读二 -
lgh1992314:
toBeanList的存在位置有点变态
Commons DbUtils 源码阅读二 -
hwwjason:
<target name="jar" ...
Apache Ant 实例介绍
咱这搞技术活儿的吧,得会用Google/Baidu!
这哥俩儿在我们的成长过程中,起着举足轻重的作用,马大队长(马士兵)时刻提醒我们有不懂的首先问问Google/Baidu。输入我们感兴趣的话题直接回车就可以获取到互联网上所有的信息,那么多的信息量,却在毫秒级的时间里返回,不无感慨一下它的强大。当然,搜索之属于产生正是由于互联网越来越庞大的信息量。
一应用系统,随着时间的推移,数据量越来越大,系统性能是否经受得住考验?!这是个我们必须考虑的问题。我们知道,通过SQL的like模糊查询数据匹配时是巨慢的,我想你肯定有收到过用户为系统的低效而抱怨的信息,你优化SQL还是达不到预想的速度,基于此,我们有必要研究一下搜索引擎。
lucene是一个高性能的、可扩展的信息检索工具库。我们可以把它融入到我们所开发的应用程序中来增加索引和搜索功能。
说说索引和搜索的关系吧:
索引操作是所有搜索引擎的核心概念:为了进行快速查找,就需要把数据处理成一种高效的、可交叉引用的查找表格式。
我们知道,为了提高数据库的数据检索性能,我们通常会对相应的列做索引,然后通过搜索找到相应的索引,再通过索引找到相对应的数据,这样查询速度会提高很多。实际上,我们仔细思考这一种策略,确实是有一定的道理的。
搜索是一个在索引中查找关键字的过程。你原来需要在巨多的数据中搜索你想要的数据,现在只要在你建立的少量索引文件中检索,这高效不言而喻。
越来越有点意思了对吧,所以,Lucene应用实例,Come on(以下代码示例均来自Lucene in action):
创建索引文件(对扩展名为txt的文件进行索引),示例代码如下:
private IndexWriter writer; /** * 指定索引目录的构造函数 * */ public Indexer(String indexDir) throws IOException{ Directory dir = FSDirectory.open(new File(indexDir)); writer = new IndexWriter(dir,new StandardAnalyzer(Version.LUCENE_30), true, IndexWriter.MaxFieldLength.UNLIMITED);//1) } //关闭IndexWriter public void close() throws CorruptIndexException, IOException{ if(null!=writer){ writer.close(); } } /** * 索引 * @param dataDir 指定需要索引的数据路径 * @param filter 进行索引的文件类型 * @return int 索引的文件数 * */ public int index(String dataDir,FileFilter filter) throws IOException{ File[] files = new File(dataDir).listFiles(); for(File f:files){ if(f.isDirectory()){ index(f.getAbsolutePath(),filter); }else if(!f.isHidden()&& f.exists()&& f.canRead()&&(filter==null||filter.accept(f))){ indexFile(f); } } return writer.numRamDocs();//Return number of documents indexed } /** * 索引指定的文件 * */ private void indexFile(File f) throws IOException{ System.out.println("Indexing "+f.getCanonicalPath()); Document doc = getDocument(f); writer.addDocument(doc);//Add document to Lucene index } /** * 指定可索引的文件类型(只索引后缀名为.txt文件) * */ private static class TextFilesFilter implements FileFilter{ public boolean accept(File path){ return path.getName().toLowerCase().endsWith(".txt");//Index .txt files only,using FileFilter } } /** * 返回代表索引集合的文档 * */ protected Document getDocument(File f) throws IOException{ Document doc = new Document();//2) doc.add(new Field("contents",new FileReader(f)));//Index file content(not store) doc.add(new Field("filename",f.getName(), Field.Store.YES,Field.Index.NOT_ANALYZED));//Index filename doc.add(new Field("fullpath",f.getCanonicalPath(), Field.Store.YES,Field.Index.NOT_ANALYZED));//Index file full path return doc; }
现对上段代码的主要部分进行简单的说明如下:
1)初始化IndexWriter。IndexWriter是索引过程中的核心组件,该类用于创建一个新的索引或者打开一个已经存在的,并增加、删除或更新文档中的索引。因为你要有存储索引的地方啊,所以参数需要一个存储索引的目录。该参数目录通过
Directory dir = FSDirectory.open(new File(indexDir));
返回。org.apache.lucene.store.Directory有两个直接子类:FSDirectory和RAMDirectory。分别代表硬盘目录以及内存目录,这两个具体的含义以后再说,因为我们需要索引的文件存储在硬盘上,所以使用FSDirectory返回代表目录的实例。
2)实例化一个Document对象,该对象代表了一些域(Field)的集合。每个域都对应一段可能在搜索过程中被查询的数据。Lucene只能处理java.lang.String、java.io.Reader以及一些基本数据类型(如int或者float)。如果要处理其它诸如Microsoft的word、excel或者html、PDF等文件类型时,则需要使用到一些其它的分析器来进行索引操作,而不只是我们上文提到的最基础的StandardAnalyzer。
索引搜索,代码示例如下:
public static void search(String indexDir,String q) throws IOException, ParseException{ Directory dir = FSDirectory.open(new File(indexDir));//Open index IndexSearcher is = new IndexSearcher(dir); QueryParser parser = new QueryParser(Version.LUCENE_30,//Parse query "contents", new StandardAnalyzer( Version.LUCENE_30)); Query query = parser.parse(q);//Search index long start = System.currentTimeMillis(); TopDocs hits = is.search(query,10); long end = System.currentTimeMillis(); System.err.println("Found "+hits.totalHits+//Write search stats " document(s) (in "+(end-start)+ " milliseconds) that matched query '"+ q+"':"); // Note that the TopDocs object contains only references to the underlying documents. // In other words, instead of being loaded immediately upon search, matches are loaded // from the index in a lazy fashion—only when requested with the Index- // Searcher.doc(int) call. // 换言之,搜索并不立即加载,当调用IndexSearcher.doc(int)时返回。 // That call returns a Document object from which we can then // retrieve individual field values. for(ScoreDoc scoreDoc:hits.scoreDocs){ Document doc = is.doc(scoreDoc.doc);//Retrieve matching document System.out.println(doc.get("fullpath"));//Display filename System.out.println(doc.get("filename")); System.out.println(doc.get("contents")); } is.close();//Close IndexerSearcher }
在搜索索引之前,我们必须得知道索引在哪儿吧,所以通过
Directory dir = FSDirectory.open(new File(indexDir));//Open index
打开索引。然后实例化IndexSearcher对象,通过QueryParser对象解析查询字符并返回一个Query对象。再调用
TopDocs hits = is.search(query,10);
查询前10的文件集合。最后循环获取,非常的简单易懂。
具体的输出结果我就不再浪费时间打字了,源码可参见附件。
当然了,这只是对所有的文本文件做了一个Lucene的简单入门介绍,希望对有兴趣学习lucene的朋友提供一个入门介绍!如有不足的地方,望指出大家共同学习和进步!
- luceneDemo.rar (1 MB)
- 下载次数: 17
发表评论
-
Spring SpringMVC MyBatis MVN
2011-11-23 22:29 0突然想起用maven了,虽然早在今年上半年就有买了本书在看 ... -
Commons Logging 源码解析二
2011-05-18 22:52 0我们说过,Commons Loggi ... -
Commons Logging 源码解析一
2011-05-15 22:43 2052最近一段日子可真是够忙的,项目上线终于开始有回报了!一直要 ... -
Commons FileUpload 源码解析二
2011-04-14 23:03 0FileItemFactory: /** * ... -
Commons FileUpload 源码解析一
2011-04-14 20:39 0Commons FileUpload?! 哇噢~可以这样说 ... -
JExcelAPI(jxl)读写Excel应用
2011-04-04 23:51 10210这篇博文在我的草 ... -
Apache Axis 服务创建
2011-03-11 23:38 2259在Apache Axis了解一文中,已经对Apache A ... -
Apache Axis了解
2011-03-04 22:16 1454最近项目到了最最关键的时刻了,客户的试用期也快结束了,客户 ... -
Commons DbUtils源码阅读之实例及测试应用
2011-02-23 22:39 1906去年整了一Commons DbUtils的源码阅读,现如今 ... -
Apache Ant常用标签介绍
2011-02-20 22:24 4774在上一篇博文中简单介绍了一下Ant是如何编译和发布Java ... -
Apache Ant 实例介绍
2011-02-17 22:55 4431开开心心的过了个年,快快乐乐的过了个情人节!So,继续我技 ... -
Commons DbUtils 源码阅读八
2011-01-19 21:39 1814距离DbUtils组件源码七 ... -
Commons DbUtils 源码阅读七
2011-01-03 13:36 2053实际上,我们也知道官网上对ResultSet的解析示例: ... -
Commons DbUtils 源码阅读六
2010-12-24 23:27 1673DbUtils组件的核心部分已经解析完了,实际上呢,DbUt ... -
Commons DbUtils 源码阅读五
2010-12-13 21:31 5818关于DbUtils,我们深入剖析了对ResultSet解析 ... -
Commons DbUtils 源码阅读四
2010-12-03 19:51 2445在读BasicRowProcessor ... -
Commons DbUtils 源码阅读三
2010-11-29 22:26 3043前两天着实被javaeye关闭着急了一下,还好,总算开放了 ... -
Commons DbUtils 源码阅读二
2010-11-19 21:17 1866DbUtils组件的整个代码结构做了一个简单的疏导,现在来 ... -
Commons DbUtils 源码阅读一
2010-11-17 22:51 2930想起读源码了! 主要是出于这几个方面的考虑: ...
相关推荐
lucene 3.0 入门实例项目,迅速教会你怎么使用3.0API
lucene3.0 lucene3.0 lucene3.0 lucene3.0 lucene3.0
Lucene 创建索引查询的入门实例,可以直接导入工具
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使用介绍及实例,从不同的使用方面介绍了lucene的使用及相应的实例。
Lucene3.0和Lucene2.0API有多处改动,该实例用Luence3.0实现
Lucene 3.0完成入门 轻松入门,内有七个文档帮助你了解并实现简单的搜索
lucene3.0 中文分词器, 庖丁解牛
Lucene 3.0 原理与代码分析完整版
Exception in thread "main" java.lang.AbstractMethodError: org.apache.lucene.analysis.TokenStream.incrementToken()Z at org.apache.lucene.index.DocInverterPerField.processFields(DocInverterPerField....
传智播客Lucene3.0课程,Lucene3.0的入门教程.
lucene3.0 入门 这个资源对于入门搜索引擎开发的人来说是非常好的
lucene3.0的核心jar包文件,lucene3.0的核心jar包文件,lucene3.0的核心jar包文件,lucene3.0的核心jar包文件。
NULL 博文链接:https://zhuliang1984723.iteye.com/blog/1190886
实例是一个java实例,可直接导入到MyEclipse中使用。 其中是lucene3.0整合了庖丁解牛分词法,添加了...因为lucene3.0无法整合paoding-analysis.jar 所以我已经把paoding-analysis中的源码整合进来了避免无法整合问题