Lucene 搜索引擎框架 基础实践

上传人:飞*** 文档编号:28277518 上传时间:2018-01-16 格式:DOC 页数:11 大小:175.50KB
返回 下载 相关 举报
Lucene 搜索引擎框架 基础实践_第1页
第1页 / 共11页
Lucene 搜索引擎框架 基础实践_第2页
第2页 / 共11页
Lucene 搜索引擎框架 基础实践_第3页
第3页 / 共11页
Lucene 搜索引擎框架 基础实践_第4页
第4页 / 共11页
Lucene 搜索引擎框架 基础实践_第5页
第5页 / 共11页
点击查看更多>>
资源描述

《Lucene 搜索引擎框架 基础实践》由会员分享,可在线阅读,更多相关《Lucene 搜索引擎框架 基础实践(11页珍藏版)》请在金锄头文库上搜索。

1、Lucene 搜索引擎1、整体结构说明索引和搜索过程图:API 使用示例图:1. 索引过程:1) 有一系列被索引文件2) 被索引文件经过语法分析和语言处理形成一系列词(Term) 。3) 经过索引创建形成词典和反向索引表。4) 通过索引存储将索引写入硬盘。2. 搜索过程:a) 用户输入查询语句。b) 对查询语句经过语法分析和语言分析得到一系列词(Term)。c) 通过语法分析得到一个查询树。d) 通过索引存储将索引读入到内存。e) 利用查询树搜索索引,从而得到每个词(Term)的 文档链表,对文档链表进行交,差,并得到结果文档。f) 将搜索到的结果文档对查询的相关性进行排序。g) 返回查询结果

2、给用户。2、基本使用说明lucene 索引的单元对象是 Document,它可以是一个文本、一封 email 或者一个网页等,其中 Document 包含多个 Field,一个 Field 就是它的一个属性,比如 “文件路径”、“作者”,“文件内容”等都是 Field1)索引用类:/* 对目录进行Lucene索引* author roy*/public class Indexer private static String INDEX_DIR = G:ROY的各种笔记索引; /索引结果存放目录 private static String DATA_DIR = G:ROY的各种笔记; /被索引文

3、件存放的目录/* 测试主函数* param args* throws Exception*/public static void main(String args) throws Exceptionlong 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 + (en

4、d - start) + milliseconds); /* 索引dataDir下的.txt文件,并储存在indexDir下,返回索引的文件数量* param indexDir* param dataDir* return* throws IOException*/private static int index(File indexDir, File dataDir) throws Exceptionif(!dataDir.exists() | !dataDir.isDirectory()throw new Exception(被索引的文件不存在!);if(!indexDir.exists(

5、) | !indexDir.isDirectory()indexDir.mkdirs();IndexWriter writer = new IndexWriter(FSDirectory.open(indexDir),new StandardAnalyzer(Version.LUCENE_30),true,IndexWriter.MaxFieldLength.UNLIMITED);/按照目录进行递归索引indexDirectory(writer,dataDir);int numIndexed = writer.numDocs();/对索引后的结果加以优化writer.optimize();wr

6、iter.close();return numIndexed;/* 循环遍历目录下的所有.doc文件并进行索引 * 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 = 或 或 =id=53 or 60=id=57:*/Term lowerTerm1 = new Term(id,53);Term

7、upperTerm1 = new Term(id,55);RangeQuery rq1 = new RangeQuery(lowerTerm1,upperTerm1,true);Term lowerTerm2 = new Term(id,57);Term upperTerm2 = new Term(id,60);RangeQuery rq2 = new RangeQuery(lowerTerm2,upperTerm2,true);BooleanQuery bq = new BooleanQuery();bq.add(rq1,BooleanClause.Occur.SHOULD);bq.add(

8、rq2,BooleanClause.Occur.SHOULD);Hits hits = searcher.search(bq);5、查询结果的高亮显示将 lucene-3.0.1contribhighlighterlucene-highlighter-3.0.1.jar 加入类路径中。1、高亮显示工具public static SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter(, );2、高亮指示器Highlighter highlighter = new Highlighter(simpleHTMLFormat

9、ter,new QueryScorer(query);3、封装实现/* 为 doc 里面的属性添加高亮显示* param doc* param fieldName* param highlighter* return* throws Exception*/private static Document getHighLight(Document doc,String fieldName,Highlighter highlighter) throws ExceptionString fieldValue = doc.get(fieldName);highlighter.setTextFragmenter(new SimpleFragmenter(150); TokenStream tokenStream = analyzer.tokenStream(fieldName,new StringReader(fieldValue);String highLightText = highlighter.getBestFragment(tokenStream,fieldValue);if(highLightText!=null)doc.getField(fieldName).setValue(highLightText);return doc;

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 商业/管理/HR > 其它文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号