- 浏览: 888100 次
- 性别:
- 来自: 武汉
文章分类
最新评论
-
小宇宙_WZY:
膜拜一下大神,解决了我一个大问题,非常感谢 orz
【解惑】深入jar包:从jar包中读取资源文件 -
JKL852qaz:
感谢,遇到相同的问题!
【解惑】深入jar包:从jar包中读取资源文件 -
lgh1992314:
为什么java中调用final方法是用invokevirtua ...
【解惑】Java动态绑定机制的内幕 -
鲁曼1991:
说的都有道理,protected只能被同一级包的类所调用
【解惑】真正理解了protected的作用范围 -
鲁曼1991:
...
【总结】String in Java
Lucene 的基本原理与《全文检索的基本原理 》是差不多的。
Lucene 的源码主要有7 个子包,每个包完成特定的功能:
包名 |
功能描述 |
org.apache.lucene.analysis |
语言分析器,主要用于的切词,支持中文主要是扩展此类 |
org.apache.lucene.document |
索引存储时的文档结构管理,类似于关系型数据库的表结构 |
org.apache.lucene.index |
索引管理,包括索引建立、删除等 |
org.apache.lucene.queryParser |
查询分析器,实现查询关键词间的运算,如与、或、非等 |
org.apache.lucene.search |
检索管理,根据查询条件,检索得到结果 |
org.apache.lucene.store |
数据存储管理,主要包括一些底层的 I/O 操作 |
org.apache.lucene.util |
一些公用类 |
另外:Lucene 3.0 还有一个org.apache.lucene.messages 包,这个包增加了本地语言支持NLS 和软件系统国际化。
上面的图可以很明显的看出Lucene 的两大主要的功能:建立索引( 红色箭头:Index), 检索索引( 蓝色箭头:Search) 。
- analysis 模块主要负责词法分析及语言处理而形成Term( 词) 。 具体参见文章《 Lucene分析器—Analyzer 》
- index 模块主要负责索引的创建,里面有IndexWriter 。
- store 模块主要负责索引的读写。
- queryParser 主要负责语法分析。
- search 模块主要负责对索引的搜索 ( 其中similarity 就是相关性打分) 。
讲到这里基本上对全文检索工具包Lucene的原理和结构已经有了大致的了解了,下面给出Lucene3.0.1建立索引和检索索引的基本代码,关于Lucene的细节探讨将在后续文章中展开。
import java.io.File; import java.io.FileReader; import java.io.IOException; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.DateTools; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.Version; public class IndexFiles { // 主要代码 索引docDir文件夹下文档,索引文件在INDEX_DIR文件夹中 public static void main(String[] args) { File indexDir=new File("e:\\实验\\index"); File docDir = new File("e:\\实验\\content"); try { //索引器 IndexWriter standardWriter = new IndexWriter(FSDirectory.open(indexDir), new StandardAnalyzer(Version.LUCENE_CURRENT), true, IndexWriter.MaxFieldLength.LIMITED); //不建立复合式索引文件,默认的情况下是复合式的索引文件 standardWriter.setUseCompoundFile(false); String[] files = docDir.list(); for (String fileStr : files) { File file = new File(docDir, fileStr); if (!file.isDirectory()) { Document doc = new Document(); //文件名称,可查询,不分词 String fileName=file.getName().substring(0,file.getName().indexOf(".")); doc.add(new Field("name",fileName, Field.Store.YES, Field.Index.NOT_ANALYZED)); //文件路径,可查询,不分词 String filePath=file.getPath(); doc.add(new Field("path", filePath, Field.Store.YES, Field.Index.NOT_ANALYZED)); //文件内容,需要检索 doc.add(new Field("content", new FileReader(file))); standardWriter.addDocument(doc); } } standardWriter.optimize(); //关闭索引器 standardWriter.close(); } catch (IOException e) { System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage()); } } }
import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.index.IndexReader; 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.Searcher; import org.apache.lucene.search.TopScoreDocCollector; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.Version; /** * 检索索引 */ public class SearchFiles { /** Simple command-line based search demo. */ public static void main(String[] args) throws Exception { String index = "E:\\实验\\index"; String field = "content"; String queries = null; boolean raw = false; // 要显示条数 int hitsPerPage = 10; // searching, so read-only=true IndexReader reader = IndexReader.open(FSDirectory.open(new File(index)), true); // only Searcher searcher = new IndexSearcher(reader); Analyzer standardAnalyzer = new StandardAnalyzer(Version.LUCENE_CURRENT); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); QueryParser parser = new QueryParser(Version.LUCENE_CURRENT,field, standardAnalyzer); while (true) { if (queries == null) // prompt the user System.out.println("Enter query: "); String line = in.readLine(); if (line == null || line.length() == -1) break; line = line.trim(); if (line.length() == 0) break; Query query = parser.parse(line); System.out.println("Searching for: " + query.toString(field)); doPagingSearch(in, searcher, query, hitsPerPage, raw, queries == null); } reader.close(); } public static void doPagingSearch(BufferedReader in, Searcher searcher, Query query, int hitsPerPage, boolean raw, boolean interactive) throws IOException { TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, false); searcher.search(query, collector); ScoreDoc[] hits = collector.topDocs().scoreDocs; int end, numTotalHits = collector.getTotalHits(); System.out.println(numTotalHits + " total matching documents"); int start = 0; end = Math.min(hits.length, start + hitsPerPage); for (int i = start; i < end; i++) { Document doc = searcher.doc(hits[i].doc); String path = doc.get("path"); if (path != null) { System.out.println((i + 1) + ". " + path); } else { System.out.println((i + 1) + ". " + "No path for this document"); } } } }
发表评论
-
【Lucene3.0 初窥】索引文件格式(2):文件结构总体框架
2010-05-02 16:44 4033Lucene使用文件扩展名标识不同的索引文件。如.fnm文件存 ... -
【Lucene3.0 初窥】索引文件格式(1):预备知识
2010-05-02 16:26 3950注意,本专题内容参见《http://lucene.apache ... -
【Lucene3.0 初窥】索引文件格式(5):posting数据[.frq/.prx]
2010-05-02 12:34 3807★ .frq 词语频 ... -
【Lucene3.0 初窥】索引文件格式(4):dictionary数据[.tii/.tis]
2010-04-30 10:57 3511Terms数据 磁盘文件存储细节 从这篇开始 ... -
【Lucene3.0 初窥】索引文件格式(3):Field数据[.fdx/.fdt/.fnm]
2010-04-23 15:12 5107注意:以下文章是参见h ... -
【Lucene3.0 初窥】索引创建(6):关闭IndexWriter
2010-04-23 15:09 42851.5 IndexWriter的关闭细节 In ... -
【Lucene3.0 初窥】索引创建(4):DocumentWriter 处理流程三
2010-04-15 15:36 3546上接《索引创建(3):DocumentWriter 处理流程二 ... -
【Lucene3.0 初窥】索引创建(5):索引数据池及内存数据细节
2010-04-13 13:50 3719上接《索引创建 (2):DocumentWriter处理流程 ... -
【Lucene3.0 初窥】索引创建(3):DocumentWriter 处理流程二
2010-04-10 10:27 4054上接《索引创建(2):DocumentWriter处理流 ... -
【Lucene3.0 初窥】索引创建(2):DocumentWriter 处理流程一
2010-04-08 21:55 3647上接《索引创建(1): IndexWriter索引器》 ... -
【Lucene3.0 初窥】索引创建(1):IndexWriter索引器
2010-04-07 19:11 4820《Lucene索引创建》系列文章将从源代码出发,详细揭示Luc ... -
【Lucene3.0 初窥】数据源内存组织结构—Document/Field
2010-04-07 16:45 3767在检索数据的时候,我们很希望可以检索出数据源的各种信息。就比如 ... -
【Lucene3.0 初窥】文本分析器Analyzer
2010-04-06 14:58 6401一个优秀的IR system要做好的第一件事就是利用自然语言处 ... -
《Introduce to IR》索引创建
2010-04-03 10:41 3369该系列文章是《An Introduce to Inform ... -
《Introduce to IR》布尔检索模型
2010-03-18 09:33 5485该系列文章是《An Introduce to Informat ... -
【Lucene3.0 初窥】全文检索的基本原理
2010-03-04 16:01 5612全文转载:http://blog.csdn.net/forfu ...
相关推荐
lucene3.0 lucene3.0 lucene3.0 lucene3.0 lucene3.0
Lucene3.0 使 用 教 程 Lucene3.0 使 用 教 程 Lucene3.0 使 用 教 程 Lucene3.0 使 用 教 程 Lucene3.0 使 用 教 程 Lucene3.0 使 用 教 程 Lucene3.0 使 用 教 程 Lucene3.0 使 用 教 程 Lucene3.0 使 用 教 程 ...
lucene 3.0 API中文帮助,学习的人懂得的
Lucene3.0特性Lucene3.0特性
Lucene3.0之查询处理(1):原理和查询类型 各种Query对象详解
lucene3.0 中文分词器, 庖丁解牛
Lucene 3.0 原理与代码分析完整版
lucene3.0 实例,在jdk1.5,lucene3.0下调式通过,可以直接运行。先运行生成索引文件的class,在运行搜索的class。
lucene3.0的核心jar包文件,lucene3.0的核心jar包文件,lucene3.0的核心jar包文件,lucene3.0的核心jar包文件。
传智播客Lucene3.0课程,Lucene3.0的入门教程.
lucene3.0-highlighter.jar lucene3.0的高亮jar包,从lucene3.0源码中导出来的
lucene升级了,分词也得升级哦! 在使用lucene3与paoding集成的时候可能会出现以下错误: Exception in thread "main" java.lang.AbstractMethodError: org.apache.lucene.analysis.TokenStream.incrementToken()Z ...
Lucene 3.0 原理 Lucene 3.0 原理 Lucene 3.0 原理 Lucene 3.0 原理
全面好用的lucene 2.0 api以及lucene 3.0 api帮助文档
Lucene3.0浅析Lucene3.0浅析Lucene3.0浅析Lucene3.0浅析
基于lucene3.0 书籍查询系统 基于lucene3.0 书籍查询系统
Lucene3.0分词系统.doc
lucene3.0 例子lucene3.0 例子 lucene3.0 例子 ,很好的学习,只有原代原,jar 包自己加上去就OK了
全文检索 lucene 3.0 叶涛 全文检索 lucene 3.0 叶涛 非常好用.上手极快!
Lucene3.0创建索引 读取目录下的所有txt文档格式的文件,然后生成一个索引文件到某目录下!