我在学习Lucene的时候,得到的结果跟想象中的不一样,请各位给点意见
先插入一写数据到数据库 tb_pu_user 表中,程序如下: public void insertDB() throws ClassNotFoundException, SQLException{
Connection conn = new DBUtil().getConnection();
PreparedStatement pst = conn.prepareStatement("insert into TB_PU_USER(V_CODE,V_NAME,V_ADDR)values(?,?,?)");
PreparedStatement pst1 = conn.prepareStatement("insert into tb_pu_order(N_ORDERID,V_CODE,N_MONEY)values(?,?,?)");
long starttime = System.currentTimeMillis();
try{
//打开事物
conn.setAutoCommit(false);
//truncate table tb_pu_user
//truncate table tb_pu_order
for (int i=0; i<20000; i++ ){
pst.setString(1, "code"+i);
pst.setString(2, "name"+i);
pst.setString(3, "addr"+i);
pst.execute();
}
for( int j=0; j<20000; j++ ){
pst1.setLong(1, j);
pst1.setString(2, "code"+j);
pst1.setLong(3, j);
pst1.execute();
} conn.commit();
long endtime = System.currentTimeMillis();
System.out.println("花费的时间为: "+ new Time(endtime-starttime));
}catch(Exception ex){
ex.printStackTrace();
conn.rollback();
}
finally{
if( pst1 != null ){
pst1.close();
}
if( pst != null ){
pst.close();
}
if( conn != null ){
conn.close();
}
}
}
2:创建索引
public CreateIndex(){}
public void CreateIndexTest() throws IOException, ClassNotFoundException, SQLException{
FSDirectory fsd = FSDirectory.getDirectory("C:\\indexDir");
StandardAnalyzer analyzer = new StandardAnalyzer();
IndexWriter iWriter = new IndexWriter (fsd, analyzer, true);
Document document = new Document();
String query = " select * from tb_pu_user t where rownum < 2000 ";
Connection conn = new DBUtil().getConnection();
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(query); while(rs.next()){
document.add(new Field("VCODE",rs.getString("V_CODE"),Field.Store.YES,Field.Index.UN_TOKENIZED));
iWriter.addDocument(document);
}
iWriter.optimize();
iWriter.close();
System.out.println(" 创建索引完成... ");
}
3: 全文搜索 public void serach(String q) throws CorruptIndexException, IOException, ParseException{
long startdate =new Date().getTime();
IndexSearcher searcher = new IndexSearcher("C:\\indexDir");
QueryParser queryParser = new QueryParser("VCODE", new StandardAnalyzer());
Query query = queryParser.parse(q);
Hits hits = searcher.search(query); System.out.println("找到了"+hits.length()+"个结果");
for(int i=0; i<hits.length(); i++ ){
Document d = hits.doc(i);
String VCODE = d.get("VCODE");
System.out.println(i+"--->"+VCODE);
}
long enddate =new Date().getTime();
System.out.println("花费时间:"+(double)(enddate-startdate)/1000+"秒");
}
测试:
public class TestLucene {
public static void main(String[] argc) throws IOException, ClassNotFoundException, SQLException, ParseException{
//生成测试数据
InsertDBTest idb = new InsertDBTest();
idb.insertDB();
//创建索引
CreateIndex cIndex = new CreateIndex();
cIndex.CreateIndexTest();
//查询索引
SerachIndex sIndex = new SerachIndex();
//参数是要查找的数据
sIndex.serach("code0");
}
} 但是得到的结果大失望,如果查询 code0 它就打印出
查找的是: code0
找到了1999个结果
0--->codedddd
1--->codedddd
2--->codedddd
3--->codedddd
4--->codedddd
5--->codedddd
6--->codedddd
7--->codedddd
8--->codedddd
....
我想应该查询出一个才对啊,请问高手们,这个是为什么?
先插入一写数据到数据库 tb_pu_user 表中,程序如下: public void insertDB() throws ClassNotFoundException, SQLException{
Connection conn = new DBUtil().getConnection();
PreparedStatement pst = conn.prepareStatement("insert into TB_PU_USER(V_CODE,V_NAME,V_ADDR)values(?,?,?)");
PreparedStatement pst1 = conn.prepareStatement("insert into tb_pu_order(N_ORDERID,V_CODE,N_MONEY)values(?,?,?)");
long starttime = System.currentTimeMillis();
try{
//打开事物
conn.setAutoCommit(false);
//truncate table tb_pu_user
//truncate table tb_pu_order
for (int i=0; i<20000; i++ ){
pst.setString(1, "code"+i);
pst.setString(2, "name"+i);
pst.setString(3, "addr"+i);
pst.execute();
}
for( int j=0; j<20000; j++ ){
pst1.setLong(1, j);
pst1.setString(2, "code"+j);
pst1.setLong(3, j);
pst1.execute();
} conn.commit();
long endtime = System.currentTimeMillis();
System.out.println("花费的时间为: "+ new Time(endtime-starttime));
}catch(Exception ex){
ex.printStackTrace();
conn.rollback();
}
finally{
if( pst1 != null ){
pst1.close();
}
if( pst != null ){
pst.close();
}
if( conn != null ){
conn.close();
}
}
}
2:创建索引
public CreateIndex(){}
public void CreateIndexTest() throws IOException, ClassNotFoundException, SQLException{
FSDirectory fsd = FSDirectory.getDirectory("C:\\indexDir");
StandardAnalyzer analyzer = new StandardAnalyzer();
IndexWriter iWriter = new IndexWriter (fsd, analyzer, true);
Document document = new Document();
String query = " select * from tb_pu_user t where rownum < 2000 ";
Connection conn = new DBUtil().getConnection();
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(query); while(rs.next()){
document.add(new Field("VCODE",rs.getString("V_CODE"),Field.Store.YES,Field.Index.UN_TOKENIZED));
iWriter.addDocument(document);
}
iWriter.optimize();
iWriter.close();
System.out.println(" 创建索引完成... ");
}
3: 全文搜索 public void serach(String q) throws CorruptIndexException, IOException, ParseException{
long startdate =new Date().getTime();
IndexSearcher searcher = new IndexSearcher("C:\\indexDir");
QueryParser queryParser = new QueryParser("VCODE", new StandardAnalyzer());
Query query = queryParser.parse(q);
Hits hits = searcher.search(query); System.out.println("找到了"+hits.length()+"个结果");
for(int i=0; i<hits.length(); i++ ){
Document d = hits.doc(i);
String VCODE = d.get("VCODE");
System.out.println(i+"--->"+VCODE);
}
long enddate =new Date().getTime();
System.out.println("花费时间:"+(double)(enddate-startdate)/1000+"秒");
}
测试:
public class TestLucene {
public static void main(String[] argc) throws IOException, ClassNotFoundException, SQLException, ParseException{
//生成测试数据
InsertDBTest idb = new InsertDBTest();
idb.insertDB();
//创建索引
CreateIndex cIndex = new CreateIndex();
cIndex.CreateIndexTest();
//查询索引
SerachIndex sIndex = new SerachIndex();
//参数是要查找的数据
sIndex.serach("code0");
}
} 但是得到的结果大失望,如果查询 code0 它就打印出
查找的是: code0
找到了1999个结果
0--->codedddd
1--->codedddd
2--->codedddd
3--->codedddd
4--->codedddd
5--->codedddd
6--->codedddd
7--->codedddd
8--->codedddd
....
我想应该查询出一个才对啊,请问高手们,这个是为什么?
解决方案 »
- eclipse中搭建ssh项目一定要用ant打包部署吗?
- 有关在JSP中获取年月的问题```各位大大来帮忙看看
- 救急~~~~~~session 存储多个用户信息 怎么解决?
- 公司要做一个考核系统,但是要求考核项集是动态生成的。。
- 整理了一些 mysql 的电子书,大家看看还有没有补充?
- 欢迎加入Java开发群组32186076
- 谁知道jfreechart 折线图和柱状图横坐标过多了以后,横坐标为什么就显示成一堆点了???
- asp.NET 实现了前后台分开编程,JSP呢?难道JSP仍然采用 HTML+JSP混合写法??
- wap页面嵌入问题
- session对象能不能传数组?
- WEB开发中的小问题
- word文档 为了这个问题我头都大了,大大侠帮忙解决一下
是创建索引的时候写错了
只创建了一个document
Document document = new Document(); 这句话应该写在循环里面