example

http://www.java3z.com/cwbwebhome/article/article5/51021.html?id=2284

 

http://hi.baidu.com/yuanaishun/blog/item/fb9b624cb15836fdd72afca4.html

 

Lucene 其实很简单的,它最主要就是做两件事:建立索引和进行搜索
来看一些在lucene中使用的术语,这里并不打算作详细的介绍,只是点一下而已----因为这一个世界有一种好东西,叫搜索。

IndexWriter:lucene中最重要的的类之一,它主要是用来将文档加入索引,同时控制索引过程中的一些参数使用。

Analyzer:分析器,主要用于分析搜索引擎遇到的各种文本。常用的有StandardAnalyzer分析器,StopAnalyzer分析器,WhitespaceAnalyzer分析器等。

Directory:索引存放的位置;lucene提供了两种索引存放的位置,一种是磁盘,一种是内存。一般情况将索引放在磁盘上;相应地lucene提供了FSDirectory和RAMDirectory两个类。

Document:文档;Document相当于一个要进行索引的单元,任何可以想要被索引的文件都必须转化为Document对象才能进行索引。

Field:字段。

IndexSearcher:是lucene中最基本的检索工具,所有的检索都会用到IndexSearcher工具;

Query:查询,lucene中支持模糊查询,语义查询,短语查询,组合查询等等,如有TermQuery,BooleanQuery,RangeQuery,WildcardQuery等一些类。

QueryParser: 是一个解析用户输入的工具,可以通过扫描用户输入的字符串,生成Query对象。

Hits:在搜索完成之后,需要把搜索结果返回并显示给用户,只有这样才算是完成搜索的目的。在lucene中,搜索的结果的集合是用Hits类的实例来表示的。

 

 

 

最近要做一个站内的全文检索功能,主要是针对 clob 字段的,于是去网上找了点 lucene 的资料,现在新版本的是 2.0.0 ,网上的例子多是 1.4.3 的,有些方法已经废弃了,搞了 n 久终于把 2.0.0 的功能实现了,呵呵,下面把实现的代码贴出来,实现了索引的创建、检索和删除功能,并可以从检索结果去查询数据库 ~ 

// 创建索引 

public void indexFiles() { 

// 创建索引文件存放路径 

File indexDir = new File("E://lucene_Learning//lucene-2.0.0src//src//demo//index"); 



try { 

Date start = new Date(); 

// 创建分析器 , 主要用于从文本中抽取那些需要建立索引的内容 , 把不需要参与建索引的文本内容去掉 . 

// 比如去掉一些 a the 之类的常用词 , 还有决定是否大小写敏感 . 

StandardAnalyzer standardAnalyzer = new StandardAnalyzer(); 

// 参数 true 用于确定是否覆盖原有索引的 

IndexWriter indexWriter = new IndexWriter(indexDir, standardAnalyzer, true); 

indexWriter.setMergeFactor(100); 

indexWriter.setMaxBufferedDocs(100); 

// 只索引这个 Field 的前 5000 个字,默认为 10000 

indexWriter.setMaxFieldLength(5000); 

// 从数据库取出所有纪录 

List articleList = articleManager.getArticles(null); 

for (int i = 0; i < articleList.size(); i++) { 

Article article = (Article) articleList.get(i); 

// 在 Document 方法是创建索引的具体代码 

Document doc = Document(article); 

indexWriter.addDocument(doc); 

} 

// Optimize 的过程就是要减少剩下的 Segment 的数量 , 尽量让它们处于一个文件中 . 

indexWriter.optimize(); 

indexWriter.close(); 

Date end = new Date(); 

System.out.println("create index: " + (end.getTime() - start.getTime()) + " total milliseconds"); 

} catch (IOException e) { 

System.out.println(" caught a " + e.getClass() + "/n with message: " + e.getMessage()); 

} 

} 

public static Document Document(Article article) 

throws java.io.IOException { 

Document doc = new Document(); 

// 为 article 表的主健创建索引,关于 Field 的几个参数下面有详细解释 

Field fieldId = new Field("uid", article.getArticleId(), Field.Store.YES, Field.Index.UN_TOKENIZED, Field.TermVector.YES); 

// 为 detail 字段创建索引, detail 在 DB 中是 clob 字段,内容为 html 文本 

String contentHtml = article.getDetail(); 

Reader read = new StringReader(contentHtml); 

// 用 HTMLParser 把 detail 字段中的 HTML 分析成文本在索引 

// HTMLParser 这个类可以在 lucene 的 demo 中找到 

HTMLParser htmlParser = new HTMLParser(read); 

BufferedReader breader = new BufferedReader(htmlParser.getReader()); 

String htmlContent =""; 

String tempContent = breader.readLine(); 

while (tempContent != null && tempContent.length() > 0) { 

htmlContent = htmlContent + tempContent; 

tempContent = breader.readLine(); 

} 

Field fieldContents = new Field("content", htmlContent, 

Field.Store.COMPRESS, Field.Index.TOKENIZED,Field.TermVector.YES); 

// db 中的每条纪录对应一个 doc ,每个字段对应一个 field 

doc.add(fieldId); 

doc.add(fieldContents); 

return doc; 

} 

// 搜索文件, keyword 是你在页面上输入的查找关键字,这里查找的是 detail 字段 

public List searchFiles(String keyword){ 

String index = "E://lucene_Learning//lucene-2.0.0src//src//demo//index"; 

// hitsList 用来保存 db 的纪录,这些纪录可以通过查询结果取到 

List hitsList = new ArrayList(); 

try { 

Date start = new Date(); 

IndexReader reader = IndexReader.open(index); 

Searcher searcher = new IndexSearcher(reader); 

Analyzer analyzer = new StandardAnalyzer(); 

QueryParser parser = new QueryParser("content", analyzer); 

// 解析查询关键字,比如输入的是以空格等分开的多个查询关键字,这里解析后,可以多条件查询 

Query query = parser.parse(keyword); 

// hits 用来保存查询结果,这里的 hits 相当于 sql 中的 result 

Hits hits = searcher.search(query); 

for (int i = 0; i < hits.length(); i++) { 

Document doc = hits.doc(i); 

// 获得 article 表的主健 

String id = doc.get("uid"); 

// 根据主健去 db 中取纪录,返回到 hitsList 中 

try { 

Article article = articleManager.getArticle(id); 

} catch (ObjectRetrievalFailureException e) { 

article = null; 

} 

// 如果没有找到该纪录,表示该纪录已经不存在,不必添加到 hitsList 中 

if(article!=null) hitsList.add(article); 

} 

searcher.close(); 

reader.close(); 

Date end = new Date(); 

System.out.println("search files: " + (end.getTime() - start.getTime()) + " total milliseconds"); 

} catch (IOException e) { 

System.out.println(" caught a " + e.getClass() + "/n with message: " + e.getMessage()); 

} catch (ParseException e) { 

System.out.println(" caught a " + e.getClass() + "/n with message: " + e.getMessage()); 

} 

return hitsList; 

} 

// 删除索引 

public void deleteIndex(){ 

String index = "E://lucene_Learning//lucene-2.0.0src//src//demo//index"; 

try { 

Date start = new Date(); 

IndexReader reader = IndexReader.open(index); 

int numFiles = reader.numDocs(); 

for (int i = 0; i < numFiles; i++) { 

// 这里的删除只是给文档做一个删除标记,你可以看到执行 deleteDocument 后会产生一个 del 后缀的文件, 

// 用来记录这些标记过的文件 

reader.deleteDocument(i); 

} 

reader.close(); 

Date end = new Date(); 

System.out.println("delete index: " + (end.getTime() - start.getTime()) + " total milliseconds"); 

} catch (IOException e) { 

System.out.println(" caught a " + e.getClass() + "/n with message: " + e.getMessage()); 

} 



} 

// 恢复已删除的索引 

public void unDeleteIndex(){ 

String index = "E://lucene_Learning//lucene-2.0.0src//src//demo//index"; 

try { 

IndexReader reader = IndexReader.open(index); 

reader.undeleteAll(); 

reader.close(); 

} catch (IOException e) { 

System.out.println(" caught a " + e.getClass() + "/n with message: " + e.getMessage()); 

} 



} 



Field 就像我们学过的数据库中的字段,简单的说,就是一个名值对。这个域有三种属性,分别是 

isStored - 是否被存储 
isIndexed - 是否被索引 
isTokenized - 是否分词 

这些属性的组合又构成了四种不同类型的 Field ,而且各有用途 


Stored 
Indexed 
Tokenized 

Keyword 
Y 
Y 
N 

UnIndexed 
Y 
N 
N 

UnStored 
N 
Y 
Y 

Text: String 
Y 
Y 
Y 

Text : Reader 
N 
Y 
Y 




关于 Field , 2.0.0 版本和 1.4.3 版本方法相比改动比较大,具体见下表 



1.4.3 版本中的下面方法都被 Field(String name, String value, Store store, Index index, TermVector termVector) 取代 

Keyword(String name, String value) // only version 1.4.3 
存储、索引、不分词,用于 URI (比如 MSN 聊天记录的日期域、比如 MP3 文件的文件全路径等等) 
Field(String name, String value, Field.Store.YES, Field.Index.UN_TOKENIZED) // version 2.0.0 

UnIndexed(String name, String value) // only version 1.4.3 
存储、不索引、不分词,比如文件的全路径 
Field(String name, String value,Field.Store.YES, Field.Index.NO)// version 2.0.0 

UnStored(String name, String value) // only version 1.4.3 
不存储、索引、分词,比如 HTML 的正文、 Word 的内容等等,这部分内容是要被索引的,但是由于具体内容通常很大,没有必要再进行存储,可以到时候根据 URI 再来挖取。所以,这部分只分词、索引,而不存储。 
Field(String name, String value,Field.Store.YES, Field.Index.TOKENIZED)// version 2.0.0 

Text(String name, String value) // only version 1.4.3 
存储、索引、分词,比如文件的各种属性,比如 MP3 文件的歌手、专辑等等。 Field.Store.YES, Field(String name, String value,Field.Index.TOKENIZED)// version 2.0.0 

Text(String name, Reader value) // only version 1.4.3 

Field(String name, Reader reader) // version 2.0.0 
不存储、索引、分词。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值