设为首页 收藏本站
| 数控仿真 | 技术文章 | 公路造价 | 文档管理软件 |
| 幸运之门彩票网 | 彩票新闻 | 免费招聘 | 百科问吧 | 百姓族谱 | 小游戏网 |
奥运会会歌简介
文档管理 | 数据库技术 | 全文检索 | 中文分词 | 文件格式 | Lucene | FireBird | IIS | 免费小游戏 | 彩票论坛
Google
北京紫气东来网络公司 > 技术文章 > 全文检索 > 为自己的系统搞个全文搜索
全文检索 | Lucene |
为自己的系统搞个全文搜索

发表:北京紫气东来网络公司www.chianwiss.com,本文被阅读:1
在本文我又提到lucene了,在java业界,提到全文检索,几乎没有什么人不知道它。
用google搜索一下,满世界都是有关资料。具有代表性的就是车东的"基于Java的全文索引引擎Lucene简介",
我要写的也就只有最简单的三板斧,再加上支持中文的ChineseAnalyzer以及按照时间排序的搜索结果排序方法。
这些都可以在其他地方找到相关资料,我只是把他们提出来,作为lucence应用中经常遇到的麻烦解决办法。
去年MSN上面有个朋友跟我提到希望用lucene构建个网站的全文检索,我当时就觉得很简单,直说没问题没问题,
不过他提到一个要求就是搜索结果要安装时间排序,我查阅了些资料,发现lucene并不提供用户自定义排序方式,
而只能按照自己相关性算法排序。后来我在车东的weblucene项目找到了IndexOrderSearcher。
解决了结果排序常规需求。

IndexOrderSearcher跟一般IndexSearch使用差不多,仅仅在构建对象的时候多加一个参数IndexOrderSearcher.ORDER_BY_DOCID_DESC
IndexOrderSearcher indexsearcher = new IndexOrderSearcher("/home/lucenetest/index",IndexOrderSearcher.ORDER_BY_DOCID_DESC);
新版本的lucene还提供了一个MultiFieldQueryParser,可以同时检索多个字段,以前QueryParser比较麻烦。

private static ChineseAnalyzer chineseAnalyzer = new ChineseAnalyzer();
public Hits search(String queryText){
if (queryText == null){
return null;
}
Query query;
try{
query = MultiFieldQueryParser.parse(queryText, new String[]{"title"},chineseAnalyzer);
return indexsearcher.search(query);
}catch(Exception e){
return null;
}
}
下面是构建索引,定时从数据库取出数据索引,做完记录完成时间,我是把时间写入一个txt文件。
package com.test.search;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.cn.*;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.*;
import org.apache.lucene.index.*;

import java.io.*;
import java.sql.*;
import java.util.Date;

import com.test.db.*;
import com.test.utility.*;

/**
* Title: SearchIndexer
* Description: 全文索引
* Copyright: Copyright (c) 2001
* Company: test
* @author Sean
* @version 1.0
*/
public class SearchIndexer {
private String indexPath = null;
protected Analyzer analyzer = new ChineseAnalyzer();

public SearchIndexer(String s) {
this.indexPath = s;
}
/**
* 索引某日期以前的所有文档
* @param fromdate
* @return
*/
public final void updateIndex(String fromdate) {
Connection conn = DbUtil.getCon();
IndexWriter indexWriter = null;
try {
indexWriter = getWriter(false);
//索引发布系统内部文件
PreparedStatement pstm = conn.prepareStatement(
"select title,body,creationtime from document where creationtime > "" + fromdate +
"" order by creationtime");
ResultSet rs = pstm.executeQuery();
while (rs.next()) {
String creationtime = rs.getString("creationtime");
String title = rs.getString("title");
String body = rs.getString("body");


if (title == null || body == null) {
continue;
}
try {
addDocsToIndex(title,body, creationtime,indexWriter);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
indexWriter.optimize();
}
catch (Exception ex) {
ex.printStackTrace();
}
finally {
try {
indexWriter.close();
conn.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 检查索引文件是否存在
* @param s
* @return 索引是否存在
*/
private boolean indexExists(String s) {
File file = new File(s + File.separator + "segments");
return file.exists();
}
/**
* 增加一组索引
* @param title
* @param body
* @param creationtime
* @param indexwriter
* @return
*/
private final void addNewsToIndex(String docid, String url,String title, String body,
String ptime, IndexWriter indexwriter) throws
IOException {
if (indexwriter == null) {
return;
}
else {
try {
Document document = new Document();
document.add(Field.Text("title", title));
document.add(Field.Text("body", body));
document.add(new Field("creationtime", creationtime, true, true, false));
indexwriter.addDocument(document);
}
catch (Exception ex) {
ex.printStackTrace();
}
return;
}
}
/**
* 取得IndexWriter
* @param flag 是否新建索引
* @return IndexWriter
*/
private IndexWriter getWriter(boolean flag) throws IOException {
String s = indexPath;
if (s == null) {
throw new IOException("索引文件路径设置错误.");
}
indexPath = s + File.separator + "search";
IndexWriter indexwriter = null;
if (flag) {
try {
indexwriter = new IndexWriter(indexPath, analyzer, true);
}
catch (Exception exception) {
System.err.println("ERROR: Failed to create a new index writer.");
exception.printStackTrace();
}
}
else {
if (indexExists(indexPath)) {
try {
indexwriter = new IndexWriter(indexPath, analyzer, false);
}
catch (Exception exception1) {
System.err.println("ERROR: Failed to open an index writer.");
exception1.printStackTrace();
}
}
else {
try {
indexwriter = new IndexWriter(indexPath, analyzer, true);
}
catch (Exception exception2) {
System.err.println("ERROR: Failed to create a new index writer.");
exception2.printStackTrace();
}
}
}
return indexwriter;
}

public static void main(String[] args) {
String lastUpdate = "/home/lucenetest/lastUpdate.txt";
SearchIndexer searchIndexer = new SearchIndexer("/home/lucenetest/index");
//取出上次更新时间
String str = Util.readTxtFile(lastUpdate);
if(str==null || str.length()==0){
str = new java.util.Date().toString();
}
searchIndexer.updateIndex(str);
//写入当前时间
Util.writeTxtFile(lastUpdate,new java.util.Date(),false);
}
}

写个cmd或者sh在相应操作系统下面定时执行SearchIndexer就可以了。

wiss文档协同系统三分钟解决一切文档管理的烦恼,帮您完成公司文档到资产的转变!!!
更多功能欢迎访问 http://www.chinawiss.com
 北京紫气东来网络公司 > 技术文章 > 全文检索
·搜索引擎分类 (3)
·刑侦角度看搜索 (6)
·搜索引擎ANTISPAM系统设计指南 (9)
·全文检索概念 (8)
·垂直搜索引擎的选型 (7)
·搜索引擎分类 (3)
·搜索引擎的索引和搜索 (7)
·中文分词和搜索引擎 (6)
·中文分词和搜索引擎 (7)
·一个例子学懂搜索引擎(LUCENE) (7)
·用LUCENE为数据库搜索建立增量索引 (7)
·中文搜索引擎技术揭密:中文分词 (8)
·刑侦角度看搜索 (6)
·搜索引擎ANTISPAM系统设计指南 (9)
 最新文章
·为自己的系统搞个全文搜索
·基于JAVA的全文索引引擎LUCENE简介
·搜索引擎分类
·向LUCENE增加中文分词功能
·统计语言模型在中文处理中的一个应用
·文档管理防止企业内部泄密
·浅析企业文档管理排序问题及方法
·数据库的要求与特性
·数据库的基本结构
·数据库的定义
·什么叫关键字密度?
·搜索引擎的索引和搜索
·BAIDU分词算法分析之一
·中文分词和搜索引擎
·中文分词算法精髓上
·中文分词的应用
·分词中的难题
·中文分词技术
·中文分词和搜索引擎
·什么是中文分词
·一个例子学懂搜索引擎(LUCENE)
·用LUCENE为数据库搜索建立增量索引
·LUCENE 索引数据库
·一个关于FIREBIRD数据库的SQL查询方法
·MSSQL数据库转到FIREBIRD数据库的问题
·FIREBIRD的备份工具(NBACKUP)介绍
·FIREBIRD常用SQL
·FIREBIRD/INTERBASE内置函数使用说明
·用了下FIREBIRD,发现真的不错哦
·FIREBIRD数据库的修复
·数据库中LOW CACHE RBA和ON DISK RBA的区
·使用DB2DART降低管理表空间的高水位标记
·快速了解数据仓库及数据建模的常用新术语
·国外数据中心流行“液冷门”
·IT业成为压力最大行业之一
·软件测试工程师笔试题中数据库试题集锦
·数据库开发程序员在开发过程中的注意事项
·带你深入了解高效的内存数据库系统FASTD
·使用链接服务器在异构数据库中查询数据
·数据库审计系统核心指标浅析
·设计应用程序时避免阻塞的八个准则
·ORACLE新手经常碰到的六个错误及解决方案
·嵌入式数据库的现状和未来
·分区表、分区索引和全局索引
·开源数据库SHARDING技术
·甲骨文数据库涨价18% IBM降价抢客户
·数据库的三重境界
·浅谈数据库管理系统在近些年内的发展趋势
·数据库技术的发展趋势
·WISS“三不”原则 引发内容管理新变革
 热门文章
·PDF转成WORD TXT EXCEL RTF软件
·JAVA抽取WORD和PDF格式文件的四种武器
·对PDF文件的文本抽取(PDFBOX处理中文PDF
·PDF与WORD之间提取和转换经验浅谈
·用C#制作PDF文件全攻略
·希望PDFLIB TET― 理想的文本提取软件包
·LUCENE索引文件格式分析
·从HTML文件中抽取正文的简单方案
·全文检索概念
·天灾还是人祸,让你知道最爱是谁?
·LUCENE系统结构分析
·C#抽取WORD文档内容
·LUCENE的应用、特点及优势
·中文搜索引擎技术揭密:网络蜘蛛
·DELPHI自动化控制EXCEL
·DELPHI与WORD之间的融合技术
·VC实现类似EXCEL文件夹式样的标签控制
·用DELPHI编制WINDOWS95下的钩子函数
·网页搜索引擎竞争分析
·刑侦角度看搜索
·论数据库技术的发展史
·信息检索的核心支撑技术
·真正搜索高手
·搜索引擎优化
·JDBC HIBERNATE 连接数据库连接字符串大
·搜索引擎ANTISPAM系统设计指南
·一个关于FIREBIRD数据库的SQL查询方法
·用DELPHI编制WINDOWS95下的钩子函数
·全文检索概念
·文档资产,"固定"还是"流动"?
免费小游戏
宠物连连看

真人美女换装

美女脱衣服

美女胴体猜猜看

调戏床上美女

黄金矿工
世界上最高的雕塑是?
| 2008-09-05 | 首页 | 功能介绍 | 免费下载 | 产品购买 | 在线服务 | 典型应用 | 技术文章 | 联系我们 |