一、信息检索和全文检索
信息检索就是从信息集合中找出与用户需求相关的信息。被检索的信息除了文本外,还有图像、音频、视频等多媒体信息。我们只关注文本的检索,把用户的查询请求和全文中的每一个词进行比较,不考虑查询请求与文本语义上的匹配,这叫做全文检索。在信息检索工具中,全文检索是最具通用性和实用性的。例如,使用百度从一大堆网页中搜出与“传智播客”相关的网页。
我们简单看一下检索技术的流程:
请求
上面可见索引数据库是十分重要的,简单的说。全文检索系统,将网络上的数据通过某种格式保存到索引库中。当用户发送查询请求时,实质上就是向索引库查询。全文检索引擎负责处理用户的请求用索引库的更新等。
小时候查的汉语字典、英语词典…我们是如何查询的?当然不是一页一页的翻了,靠的是字典的目录。Lucene的检索方式正是使用了此技术,lucene的索引库格式:
|
上面的表只是简单说明lucene索引库的存储格式。我们通过lucene提供的类可以向索引库添加、修改、查询、删除操作…。
关键字是通过分词器分析出来的,一般情况下各有语言有各字的分词器。分词器的强大提高了查询数据的接近性。
二、lucene操作
我们编写一个对Article对象向lucene索引库的添加、修改、查询、删除操作:
import java.util.ArrayList; import java.util.List; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.Term; import org.apache.lucene.index.IndexWriter.MaxFieldLength; import org.apache.lucene.queryParser.MultiFieldQueryParser; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.TopDocs; import cn.itcast.cc.lucene.helloword.Article; import cn.itcast.cc.lucene.helloword.utils.ArticleDocUtils;
publicclass IndexDao { // 索引目录 private String indexPath = "./index"; // 分词器 private Analyzer analyzer = new StandardAnalyzer();
/** *保存记录 * *@paramart */ publicvoid save(Article art) { // lucene的写出索引类 IndexWriter indexWriter = null; try { indexWriter = new IndexWriter(this.indexPath, this.analyzer, MaxFieldLength.LIMITED); // 添加到索引库 indexWriter.addDocument(ArticleDocUtils.Article2Doc(art)); } catch (Exception e) { e.printStackTrace(); } // 释放indexWriter if (indexWriter != null) { try { // 使用后一定要关闭 indexWriter.close(); } catch (Exception e) { e.printStackTrace(); } } }
/** *更新记录 * *@paramart */ publicvoid update(Article art) { IndexWriter indexWriter = null; try { indexWriter = new IndexWriter(this.indexPath, this.analyzer, MaxFieldLength.LIMITED); Term term = new Term("id", art.getId() + ""); // 更新 indexWriter.updateDocument(term, ArticleDocUtils.Article2Doc(art)); } catch (Exception e) { e.printStackTrace(); }
// 释放indexWriter if (indexWriter != null) { try { indexWriter.close(); } catch (Exception e) { e.printStackTrace(); } } }
/** *删除记录 * *@paramid */ publicvoid delete(int id) { IndexWriter indexWriter = null; try { indexWriter = new IndexWriter(this.indexPath, this.analyzer, MaxFieldLength.LIMITED); Term term = new Term("id", id + ""); // 删除 indexWriter.deleteDocuments(term); } catch (Exception e1) { e1.printStackTrace(); }
// 释放indexWriter if (indexWriter != null) { try { indexWriter.close(); } catch (Exception e) { e.printStackTrace(); } } }
/** *查询记录(具有分页功能) * *@paramqueryString *@paramstartIndex *@paramrecordCount *@return */ public List search(String queryString, int startIndex, int recordCount) { List result = new ArrayList(); IndexSearcher indexSearcher = null; try { indexSearcher = new IndexSearcher(this.indexPath); String[] fields = new String[] {"title","content"}; // 分析查询条件 QueryParser queryParser = new MultiFieldQueryParser(fields, this.analyzer); // 生成查询对象 Query query = queryParser.parse(queryString); int findTotalRecord = startIndex + recordCount; // 查询 TopDocs topDocs = indexSearcher.search(query, null, findTotalRecord); //获取分页数据 int endIndex = Math.min(startIndex+recordCount,topDocs.totalHits); for(int i=startIndex; i<endIndex;i++){ result.add(indexSearcher.doc(topDocs.scoreDocs[i].doc)); }
} catch (Exception e1) { e1.printStackTrace(); }
// 释放indexWriter if (indexSearcher != null) { try { indexSearcher.close(); } catch (Exception e) { e.printStackTrace(); } } return result; } } |
其中使用到的“ArticleDocUtils”如下:
需要导入的jar包:lucene-analyzers-2.4.0.jar、lucene-core-2.4.0.jar、lucene-highlighter-2.4.0.jar。