最近要做一个站内的全文检索功能,主要是针对 clob 字段的,于是去网上找了点 lucene 的资料,现在新版本的是 2.0.0 ,网上的例子多是 1.4.3 的,有些方法已经废弃了,搞了 n 久终于把 2.0.0 的功能实现了,呵呵,下面把实现的代码贴出来,实现了索引的创建、检索和删除功能,并可以从检索结果去查询数据库 ~
<o:p></o:p>
// 创建索引 <o:p></o:p>
public void indexFiles() { <o:p></o:p>
// 创建索引文件存放路径 <o:p></o:p>
File indexDir = new File("E:\\lucene_Learning\\lucene-<st1:chsdate isrocdate="False" islunardate="False" day="30" month="12" year="1899" w:st="on">2.0.0</st1:chsdate>src\\src\\demo\\index"); <o:p></o:p>
<o:p></o:p>
try { <o:p></o:p>
Date start = new Date(); <o:p></o:p>
// 创建分析器 , 主要用于从文本中抽取那些需要建立索引的内容 , 把不需要参与建索引的文本内容去掉 . <o:p></o:p>
// 比如去掉一些 a the 之类的常用词 , 还有决定是否大小写敏感 . <o:p></o:p>
StandardAnalyzer standardAnalyzer = new StandardAnalyzer(); <o:p></o:p>
// 参数 true 用于确定是否覆盖原有索引的 <o:p></o:p>
IndexWriter indexWriter = new IndexWriter(indexDir, standardAnalyzer, true); <o:p></o:p>
indexWriter.setMergeFactor(100); <o:p></o:p>
indexWriter.setMaxBufferedDocs(100); <o:p></o:p>
// 只索引这个 Field 的前 5000 个字,默认为 10000 <o:p></o:p>
indexWriter.setMaxFieldLength(5000); <o:p></o:p>
// 从数据库取出所有纪录 <o:p></o:p>
List articleList = articleManager.getArticles(null); <o:p></o:p>
for (int i = 0; i < articleList.size(); i++) { <o:p></o:p>
Article article = (Article) articleList.get(i); <o:p></o:p>
// 在 Document 方法是创建索引的具体代码 <o:p></o:p>
Document doc = Document(article); <o:p></o:p>
indexWriter.addDocument(doc); <o:p></o:p>
} <o:p></o:p>
// Optimize 的过程就是要减少剩下的 Segment 的数量 , 尽量让它们处于一个文件中 . <o:p></o:p>
indexWriter.optimize(); <o:p></o:p>
indexWriter.close(); <o:p></o:p>
Date end = new Date(); <o:p></o:p>
System.out.println("create index: " + (end.getTime() - start.getTime()) + " total milliseconds"); <o:p></o:p>
} catch (IOException e) { <o:p></o:p>
System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage()); <o:p></o:p>
} <o:p></o:p>
} <o:p></o:p>
public static Document Document(Article article) <o:p></o:p>
throws java.io.IOException { <o:p></o:p>
Document doc = new Document(); <o:p></o:p>
// 为 article 表的主健创建索引,关于 Field 的几个参数下面有详细解释 <o:p></o:p>
Field fieldId = new Field("uid", article.getArticleId(), Field.Store.YES, Field.Index.UN_TOKENIZED, Field.TermVector.YES); <o:p></o:p>
// 为 detail 字段创建索引, detail 在 DB 中是 clob 字段,内容为 html 文本 <o:p></o:p>
String contentHtml = article.getDetail(); <o:p></o:p>
Reader read = new StringReader(contentHtml); <o:p></o:p>
// 用 HTMLParser 把 detail 字段中的 HTML 分析成文本在索引 <o:p></o:p>
// HTMLParser 这个类可以在 lucene 的 demo 中找到 <o:p></o:p>
HTMLParser htmlParser = new HTMLParser(read); <o:p></o:p>
BufferedReader breader = new BufferedReader(htmlParser.getReader()); <o:p></o:p>
String htmlContent =""; <o:p></o:p>
String tempContent = breader.readLine(); <o:p></o:p>
while (tempContent != null && tempContent.length() > 0) { <o:p></o:p>
htmlContent = htmlContent + tempContent; <o:p></o:p>
tempContent = breader.readLine(); <o:p></o:p>
} <o:p></o:p>
Field fieldContents = new Field("content", htmlContent, <o:p></o:p>
Field.Store.COMPRESS, Field.Index.TOKENIZED,Field.TermVector.YES); <o:p></o:p>
// db 中的每条纪录对应一个 doc ,每个字段对应一个 field <o:p></o:p>
doc.add(fieldId); <o:p></o:p>
doc.add(fieldContents); <o:p></o:p>
return doc; <o:p></o:p>
} <o:p></o:p>
// 搜索文件, keyword 是你在页面上输入的查找关键字,这里查找的是 detail 字段 <o:p></o:p>
public List searchFiles(String keyword){ <o:p></o:p>
String index = "E:\\lucene_Learning\\lucene-<st1:chsdate isrocdate="False" islunardate="False" day="30" month="12" year="1899" w:st="on">2.0.0</st1:chsdate>src\\src\\demo\\index"; <o:p></o:p>
// hitsList 用来保存 db 的纪录,这些纪录可以通过查询结果取到 <o:p></o:p>
List hitsList = new ArrayList(); <o:p></o:p>
try { <o:p></o:p>
Date start = new Date(); <o:p></o:p>
IndexReader reader = IndexReader.open(index); <o:p></o:p>
Searcher searcher = new IndexSearcher(reader); <o:p></o:p>
Analyzer analyzer = new StandardAnalyzer(); <o:p></o:p>
QueryParser parser = new QueryParser("content", analyzer); <o:p></o:p>
// 解析查询关键字,比如输入的是以空格等分开的多个查询关键字,这里解析后,可以多条件查询 <o:p></o:p>
Query query = parser.parse(keyword); <o:p></o:p>
// hits 用来保存查询结果,这里的 hits 相当于 sql 中的 result <o:p></o:p>
Hits hits = searcher.search(query); <o:p></o:p>
for (int i = 0; i < hits.length(); i++) { <o:p></o:p>
Document doc = hits.doc(i); <o:p></o:p>
// 获得 article 表的主健 <o:p></o:p>
String id = doc.get("uid"); <o:p></o:p>
<sp>
相关推荐
开发自己的搜索引擎《lucene2.0+heritrix》一书对应的源码资料,总共有30M,只上传了几个例子. ch2-lucene入门小例子 myReserch-可用的网络搜索引擎
全面好用的lucene 2.0 api以及lucene 3.0 api帮助文档
本软件是开发自己的搜索引擎《lucene2.0+heritrix》一书对应的源码资料,不过由于代码很大,只发布其中最全的部分,各章的源码就不发了。
lucene java 搜索引擎 比较经典的全文搜索引擎,最近发现在官方网站上找不到了,放在这里给大家,方便点,多谢谢支持!
基于Java的全文索引引擎.doc lucene测试代码.txt lucene为数据库搜索建立增量索引.txt lucene数据库索引.txt 新闻系统全文检索的思绪.txt ... 关于lucene2.0的创建、检索和删除功能的完整实现.doc weblucene.txt
去下载这个吧。(完整版的) http://d.download.csdn.net/down/1756566/caizhongda Lucene2.0+Heritrix搜索引擎(完整光盘).part2.rar
Lucene.2.0的API文档合集 是从html装成chm的
lucene-2.0开发专用
自己学习Lucenes 2.0 和Heritrix之后的总结
Lucene.net 2.0 API,Lucene.net 2.0 dll,Lucene.net学习探索着必备。
lucene.net 2.0的全文检索源代码
Lucene实现全文检索
Lucene.Net 2.0 源码+文档
这是Lucene 2.0+Heriterx书源代码里ch7文件夹下的lib文件,里面都是ch7项目里jar包,Lucene 2.0+Heriterx书源代码-ch7lib
lucene 2.0虽然是比较老的技术,它对实时搜索支持交差,不过很多普通的搜索引擎对实时性要求并不是很高,并且对于分词上没有太多的个性化专业要求,那么这个选择将是非常好的,提供各类分词接口、存储接口、索引接口...
Lucene2.0+Heritrix搜索引擎 随书光盘
lucene2.0+heritrix的随书光盘,有很多好用的lucene实例,还包括一个完整的垂直搜索引擎的设计
NULL 博文链接:https://fishhappy365.iteye.com/blog/456739
【别下这个哈,,】 【完整版的在】《开发自己的搜索引擎-Lucene 2.0 Heritrix》 http://download.csdn.net/source/1756566 文件大小:24.60 MB
Lucene2.0+Heritrix(ch3源代码)