lucene3.6.1完成数据库的检索

package com.javabean;

import java.io.File;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
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.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.queryParser.QueryParser.Operator;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;

public class lucene
{
public void createIndexFile() {
IndexWriter indexWriter=null;
try {
// 需要的分词器
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
// 创建的是哪个版本的IndexWriterConfig
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(
Version.LUCENE_36, analyzer);
// 创建系统文件-----
Directory directory = new SimpleFSDirectory(new File("D:\\tomcat-6.0.35\\webapps\\jsp_flex\\index"));
indexWriter = new IndexWriter(directory,indexWriterConfig);
//访问数据库拿数据
DBConnect db = new DBConnect();
String sql = "select * from book";
ResultSet rs = db.select(sql);
while(rs.next())
{
//indexWriter添加索引
Document doc=new Document();
//文本中添加内容
doc.add(new Field("ISBN",rs.getString(1).toString(),Store.YES,Index.ANALYZED));
doc.add(new Field("bookname",rs.getString(2).toString(),Store.YES,Index.ANALYZED));
doc.add(new Field("author",rs.getString(3).toString(),Store.YES,Index.ANALYZED));
doc.add(new Field("leibie",rs.getString(6).toString(),Store.YES,Index.ANALYZED));
doc.add(new Field("jianjie",rs.getString(10).toString(),Store.YES,Index.ANALYZED));
//添加到索引中去
indexWriter.addDocument(doc);
}

} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(indexWriter!=null){
try {
indexWriter.commit();
indexWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

public void seacher(String queryString) throws IOException
{
FSDirectory fs;
try {
fs = FSDirectory.open(new File("D:\\tomcat-6.0.35\\webapps\\jsp_flex\\index"));
IndexReader ir = IndexReader.open(fs);
long start = System.currentTimeMillis();
IndexSearcher search = new IndexSearcher(ir);
String key = queryString;
String fieds[] = new String[]{"ISBN","bookname","author","leibie","jianjie"};
Analyzer ik = new StandardAnalyzer(Version.LUCENE_36);
MultiFieldQueryParser m = new MultiFieldQueryParser(Version.LUCENE_36, fieds, ik);
m.setDefaultOperator(Operator.AND);
Query query = m.parse(key) ;
ScoreDoc[] hits = search.search(query, null, 1000).scoreDocs;
System.out.println("共命中"+hits.length+"条记录");
for(ScoreDoc scoreDoc:hits)
{
Document doc= search.doc(scoreDoc.doc);
System.out.println(scoreDoc.score + "\t ISBN:"+doc.get("ISBN")+"\t bookname:"+doc.get("bookname")+"\t leibie:"+doc.get("leibie"));
}
System.out.println("执行时间:"+(System.currentTimeMillis()-start)+"毫秒");
}
catch (IOException e)
{
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}


}

public static void main(String[] args) throws IOException
{
lucene test = new lucene();
// test.createIndexFile();
test.seacher("中国");

}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值