package com.laozizhu.article.util;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.sql.DataSource;
import net.paoding.analysis.analyzer.PaodingAnalyzer;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocCollector;
/**
* 基于庖丁解牛的Lucene 2.4的全文搜索代码。
*
* @author 老紫竹研究室(laozizhu.com)
*/
public class LucenePaoDing {
private static final String indexPath = "d:/indexpaoding/www.laozizhu.com";
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
rebuildAll();
String keyword = "Spring.jar";
LucenePaoDing l = new LucenePaoDing();
System.out.println("索引搜索\n------------------------------");
System.out.println(l.seacherIndex(keyword));
}
public static void rebuildAll() {
synchronized (indexPath) {
LucenePaoDing l = new LucenePaoDing();
DataSource ds = (DataSource) Factory.getBean("dataSource");
Connection con = null;
Statement stat = null;
ResultSet rs = null;
try {
con = ds.getConnection();
stat = con.createStatement();
rs = stat.executeQuery("select id,subject,content from t_article");
if (rs != null) {
l.Index(rs);
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (Exception ex) {}
}
if (stat != null) {
try {
stat.close();
} catch (Exception ex) {}
}
if (con != null) {
try {
con.close();
} catch (Exception ex) {}
}
}
}
}
public synchronized Analyzer getAnalyzer() {
return new PaodingAnalyzer();
}
private synchronized void Index(ResultSet rs) {// 通过结果集就可以获得数据源了
try {
IndexWriter writer = new IndexWriter(indexPath, getAnalyzer(), true, IndexWriter.MaxFieldLength.UNLIMITED);
writer.setMaxFieldLength(10000000);
Date start = new Date();
int index = 1;
while (rs.next()) {
Document doc = new Document();// 一个文档相当与表的一条记录
doc.add(new Field("id", rs.getString("id"), Field.Store.YES, Field.Index.NOT_ANALYZED));// 字段id放的是数据库表中的id,lucene的一条记录的一个字段下的数据可以放多个值,这点与数据库表不同
doc.add(new Field("subject", rs.getString("subject"), Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("content", rs.getString("content"), Field.Store.YES, Field.Index.ANALYZED));
writer.addDocument(doc);
if (index++ == 1000) {
writer.commit();
index = 0;
}
}
writer.commit();
writer.optimize();// 优化
writer.close();// 一定要关闭,否则不能把内存中的数据写到文件
Date end = new Date();
System.out.println("重建索引成功!!!!" + "用时" + (end.getTime() - start.getTime()) + "毫秒");
} catch (IOException e) {
System.out.println(e);
} catch (SQLException e) {
System.out.println(e);
}
}
public void IndexSingle(long id, String subject, String content) {// 通过结果集就可以获得数据源了
synchronized (indexPath) {
try {
IndexWriter writer = new IndexWriter(indexPath, getAnalyzer(), false, IndexWriter.MaxFieldLength.UNLIMITED);
writer.setMaxFieldLength(10000000);
Date start = new Date();
Document doc = new Document();// 一个文档相当与表的一条记录
doc.add(new Field("id", Long.toString(id), Field.Store.YES, Field.Index.NOT_ANALYZED));// 字段id放的是数据库表中的id,lucene的一条记录的一个字段下的数据可以放多个值,这点与数据库表不同
doc.add(new Field("subject", subject, Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("content", content, Field.Store.YES, Field.Index.ANALYZED));
writer.addDocument(doc);
// writer.optimize();// 优化
writer.close();// 一定要关闭,否则不能把内存中的数据写到文件
Date end = new Date();
System.out.println("索引建立成功!!!!" + "用时" + (end.getTime() - start.getTime()) + "毫秒");
} catch (IOException e) {
System.out.println(e);
}
}
}
/**
* 最主要的搜索方法。
*
* @param queryString
* @return
*/
public List<Long> seacherIndex(String queryString) {// 根据关键字搜索
try {
IndexSearcher isearcher = new IndexSearcher(indexPath);
/* 下面这个表示要同时搜索这两个域,而且只要一个域里面有满足我们搜索的内容就行 */
BooleanClause.Occur[] clauses = { BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD };
TopDocCollector collector = new TopDocCollector(10);
Query query = MultiFieldQueryParser.parse(queryString, new String[] { "subject", "content" }, clauses, getAnalyzer());
isearcher.search(query, collector);
ScoreDoc[] hits = collector.topDocs().scoreDocs;
List<Long> rtn = new ArrayList<Long>();
Long id;
int docId;
for (int i = 0; i < hits.length; i++) {
docId = hits[i].doc;
Document doc = isearcher.doc(docId);
id = Long.parseLong(doc.get("id").trim());
if (!rtn.contains(id)) {
rtn.add(id);
}
}
isearcher.close();
return rtn;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
分享到:
相关推荐
庖丁解牛 源码 for Lucene 2.4
Lucene 庖丁解牛分词法 , 能够使用它解决中文分词问题。
庖丁解牛(很好的分词效率) 在做站内全文检索时创建索引时比较快,而且感觉效果比JE要好一些。。
实例是一个java实例,可直接导入到MyEclipse中...其中是lucene3.0整合了庖丁解牛分词法,添加了高亮显示。因为lucene3.0无法整合paoding-analysis.jar 所以我已经把paoding-analysis中的源码整合进来了避免无法整合问题
Lucene2.4入门总结lucene2.4 人门 field 多条件搜索
支持中文的庖丁解牛,庖丁分词,找了好久才找到的希望对你有帮助。
java拼车网雏形(Ext2.0+SSH+oracle10g+lucene2.4)
庖丁解牛,Lucene分词器,很难得的资源。
lucene 2.4 jar lucene2.4版本的JAR包
Lucene加庖丁解牛测试类Lucene加庖丁解牛测试类
能够直接运行的Lucene2.4样例以及全面注释; + Lucene中文文档,能够帮助你最快掌握Lucene2.4
由于庖丁官方目前提供可下载尚不支持Lucene 3.0以上版本。因此作者对paoding进行重新编译,使其与最新Lucene 3.0.1版本适用。 Latest paoding 3.0.1 for lucene 3.0.1 使用说明: 先下载2.0.4的版本(h t t p : / ...
ictclas4j for lucene 2.4 任何人不得将此用于商业用途,仅限个人学习研究之用.该开源项目遵循Apache License 2.0
支持lucene3的庖丁解牛分词器和字典,可直接调用
最新庖丁解牛分词法的使用demo,支持Lucene3.3、3.4等3.0以上版本,庖丁解牛的分词包为自己编译生成的,之前的2.0的版本不能支持Lucene3.0以上版本,所以需要从svn下载最新的庖丁解牛源码,生成jar文件(我同样已...
一直找不到适合lucene-35以上的庖丁解牛jar包,搞了半天总于生成好了jar包,在lucene-35中运行没问题
lucene最新版本加庖丁解牛实现搜索引擎 内附chm说明文档,还有各种核心包
资源为庖丁解牛分词法的最新源码以及生成的jar包,支持最新的Lucene3.4以及Lucene3.0以上版本。Jar包为本地生成,大家也可以到SVN上检出自己生成,另外庖丁解牛分词法的使用Demo我会接下来上传一份,欢迎分享。
Luene2.4版本在以前的基础上进行了不少的改动,性能上也提升了不少,这里边学习边写了一个简单的例子,给初学者行个方便!
可以适用于lucene3.5的庖丁解牛分词器jar包