lucene3.6 mysql_lucene3.6多Field查询

创建索引文件

import java.io.File;

import java.io.IOException;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;

import org.apache.lucene.document.Document;

import org.apache.lucene.document.Field;

import org.apache.lucene.document.Fieldable;

import org.apache.lucene.index.IndexWriter;

import org.apache.lucene.index.IndexWriterConfig;

import org.apache.lucene.store.FSDirectory;

import org.apache.lucene.util.Version;

import org.wltea.analyzer.lucene.IKAnalyzer;

public class IndexCreateForTable {

/**

* @param args

*/

public static void main(String[] args) {

File f = new File("F:\\lucene\\table");

String url = "";

FSDirectory fs = null;

try {

fs = FSDirectory.open(f);

IKAnalyzer ik = new IKAnalyzer();

ik.setUseSmart(true);

IndexWriterConfig ifg = new IndexWriterConfig(Version.LUCENE_36, ik);

IndexWriter iw = new IndexWriter(fs, ifg);

Class.forName("com.mysql.jdbc.Driver");

Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/articles", "root", "root") ;

PreparedStatement ps = conn.prepareStatement("select s.pub_id,s.china_title,s.sponsor from bs_tb_publications s where s.status = '11A'") ;

ResultSet rs = ps.executeQuery();

while(rs.next()){

Document doc = new Document();

doc.add(new Field("id", rs.getString("pub_id"), Field.Store.YES, Field.Index.ANALYZED));

doc.add(new Field("title", rs.getString("china_title"), Field.Store.YES, Field.Index.ANALYZED));

doc.add(new Field("desc", rs.getString("sponsor")==null?"":rs.getString("sponsor"), Field.Store.YES, Field.Index.ANALYZED));

iw.addDocument(doc);

}

iw.commit();

iw.close();

} catch (IOException e) {

e.printStackTrace();

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

搜索程序:

/**

* 多字段搜索

*/

public void test2(){

FSDirectory fs;

try {

fs = FSDirectory.open(new File("F:\\lucene\\table"));

IndexReader ir = IndexReader.open(fs);

long start = System.currentTimeMillis();

IndexSearcher search = new IndexSearcher(ir);

String key = " 卫星 ";

String fieds[] = new String[]{"title","desc"};

IKAnalyzer ik = new IKAnalyzer();

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 id:"+doc.get("id")+"\ttitle:"+doc.get("title")+"\tdesc:"+doc.get("desc"));

}

System.out.println("执行时间:"+(System.currentTimeMillis()-start)+"毫秒");

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (ParseException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2012-07-31 17:14

浏览 1316

评论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值