`

Lucene 2.9 基础应用

阅读更多

在这里就不解释Lucene应用中的基本概念了,直接切入主题

 

一、添加索引过程

	public static void addDocument(String title,String content){
		Document doc = new Document();
		Field titleField = new Field("title",title,Store.YES, Field.Index.ANALYZED,Field.TermVector.WITH_POSITIONS_OFFSETS);
		Field contentField = new Field("content",content, Field.Store.NO, Field.Index.ANALYZED,Field.TermVector.WITH_POSITIONS_OFFSETS);
		Field content2Field = null;
		//CompressionTools 用于将 长文本 或二进制文件压缩存储
		content2Field = new Field("content",CompressionTools.compressString(content), Field.Store.YES);
		doc.add(titleField);
		doc.add(contentField);
		doc.add(content2Field);
		docmentList.add(doc);
	}
	
	public static void index(){
		try {
			//获取索引存放的路径
			FSDirectory indexDir = FSDirectory.open(new File(FilePathUtils.getIndexPath()));
			//实例化IKAnalyzer中文分词器
			Analyzer analyzer = new IKAnalyzer();
			IndexWriter indexWriter = new IndexWriter(indexDir, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED);
			for (Document doc : docmentList) {
				indexWriter.addDocument(doc);
			}
			indexWriter.optimize();
			indexWriter.close();
			docmentList = new ArrayList<Document>();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

 二、检索过程

	public static void search(String keyword){
		try {
			//获取索引存放的路径
			FSDirectory indexDir = FSDirectory.open(new File(FilePathUtils.getIndexPath()));
			//集成IKAnalyzer中文分词器
			Analyzer analyzer = new IKAnalyzer();
			// 实例化搜索器
			IndexSearcher searcher = new IndexSearcher(indexDir,true);
			// 在索引器中使用 IKSimilarity 相似度评估器
			searcher.setSimilarity(new IKSimilarity());
			// 使用 IKQueryParser 查询分析器构造 Query 对象
			Query query = IKQueryParser.parse("title", keyword);//指定检索的字段
			//构建高亮显示格式
			SimpleHTMLFormatter sHtmlF = new SimpleHTMLFormatter("<b><font color='red'>", "</font></b>");
			//高亮对象
			Highlighter highlighter = new Highlighter(sHtmlF,new QueryScorer(query));
			//设置高亮附近的字数
			highlighter.setTextFragmenter(new SimpleFragmenter(100));
			//分页索取结果,每页显示5条记录
		    TopScoreDocCollector collector = TopScoreDocCollector.create(5, false);
	        //执行检索
		    searcher.search(query, collector);
	        ScoreDoc[] hits = collector.topDocs().scoreDocs;
	        int numTotalHits = collector.getTotalHits();
	        System.out.println("共检索到"+numTotalHits+"条记录");
			// 输出结果
			for (int i = 0; i < hits.length; i++) {
				Document targetDoc = searcher.doc(hits[i].doc);
				try {
					//highlighter.getBestFragment()获取高亮显示结果
					System.out.println(" 标题: " +  highlighter.getBestFragment(analyzer,"title",targetDoc.get("title")));
					Field contentField = targetDoc.getField("content");
					//CompressionTools.decompressString() 解压
					System.out.println(" 内容: " + CompressionTools.decompressString(contentField.getBinaryValue()));
				} catch (InvalidTokenOffsetsException e) {
					e.printStackTrace();
				} catch (DataFormatException e) {
					e.printStackTrace();
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
 
1
0
分享到:
评论

相关推荐

    lucene2.9开发指南

    lucene2.9开发指南是为了帮助初级开发人员而编写整理的资料,包括了所有的核心技术,希望对大家有帮助

    本人的Lucene2.9学习笔记

    本人的Lucene2.9学习笔记 本人的Lucene2.9学习笔记 本人的Lucene2.9学习笔记 本人的Lucene2.9学习笔记本人的Lucene2.9学习笔记本人的Lucene2.9学习笔记 本人的Lucene2.9学习笔记

    Lucene 2.9 API CHM 官方API学习资料电子书

    lucene 2.9 API , lucene API,lucene 学习资料,lucene2.9 CHM

    Lucene2.9英文API

    最新的Lucene2.9英文API Lucene是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎

    lucene 2.9api

    lucene_2.9_api

    Lucene.NET2.9搜索引擎源代码(C#)

    Lucene.NET2.9,搜索引擎源代码(C#),分析较透彻,可以研究源代码来学习

    lucene 2.9 api

    解压后进入all文件夹,index.html。 选择all 链接,界面你就会很熟悉了 html 形式 内容很全面 最新

    lucene 2.9

    最新版lucene分词,为了方便各位下载 放到csdn上来 希望能帮到忙

    lucene.net2.9.4.2源码版

    lucene.net2.9.4.2源码版 dll版本是2.9.4.2 对2.9.4版的局部改进版

    lucene.net2.9.4.2编译版

    lucene.net2.9.4.2编译版 dll版本是2.9.4.2 对2.9.4版的局部改进版

    lucene.net 2.9.1.002

    检索 lucene C# lucene.net 2.9.1.002版本

    自己修改的基于lucene.net 2.9和highlighter插件的简单例子

    自己修改的基于lucene.net 2.9和highlighter插件的简单例子

    lucene facet查询示例

    lucene facet查询示例,区间查询多dim查询,key-field-value模型了解

    Lucene.Net2.9 Library

    What’s Lucene Lucene是一个信息检索的函数库(Library),利用它你可以为你的应用加上索引和搜索的功能. for VS2008

    lucene2.9.4

    lucene2.9.4官方版本 很好用的 我自己现在就在用 给大家分享一下吧

    Lucene 2.9.2 Jar包

    Version 2.9.2 本人倡导零分资源共享,欢迎大家按需下载。

    lucene.net基本应用(doc)

    lucene.net基本应用(doc) lucene.net基本应用(doc) lucene.net基本应用(doc)

    lucene 3.4基本应用

    简单的lucene应用,包括建立索引,更新索引,查询索引。基础的应用。代码方便易懂。声明该源码仅供个人学习研究使用。不得用于商业盈利目的,侵权必究。

    Lucene实战(第2版)源代码

    全书共14章,分为两大部分。第1部分Lucene的核心,着重于Lucene的核心API介绍,并按照把Lucene集成到程序...第2部分Lucene的应用,通过对Lucene内置工具的介绍,展示了Lucene技术的高级应用和在各种程序语言上的移植。

    Lucene.net2.9.2源码

    Lucene.net是我从https://svn.apache.org/repos/asf/incubator/lucene.net/tags/Lucene.Net_2_9_2/ 上下载下来的,如果你想自己下载可以按照http://apps.hi.baidu.com/share/detail/22856972上面说的方法来下载。...

Global site tag (gtag.js) - Google Analytics