lucene实现建立数据库索引并实现检索的问题,我很着急望各位指教
我从数据库的表中将数据放到指定的地方D:/index/,在对他搜索,但是hits.length()等于0,就是没搜到,我些的到底哪错了,望各位指教
package MSHOME;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Document;
public class TestLucene {
public TestLucene() throws ClassNotFoundException {
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@219.217.50.183:1521:MSHOME", "MS_HOME", "MSHOME");
Statement stmt = conn.createStatement();
String sql = "select * from 讨论区表";
ResultSet rs = stmt.executeQuery(sql);
IndexWriter writer = new IndexWriter("D:/index/", getAnalyzer(), true);
while(rs.next()){
Document doc=new Document();
doc.add(new Field("id",rs.getString("用户ID"),Field.Store.YES,Field.Index.UN_TOKENIZED));
doc.add(new Field("content",rs.getString("文章内容"),Field.Store.YES,Field.Index.TOKENIZED));
writer.addDocument(doc);
}
}
catch(IOException e){
System.out.println(e);
}
catch(SQLException e){
System.out.println(e);
}
}
public Analyzer getAnalyzer(){
return new StandardAnalyzer();
}
public Hits seacher(String queryString){
Hits hits=null;
try{
IndexSearcher is = new IndexSearcher("D:/index/");
QueryParser qp= new QueryParser("content",getAnalyzer());
Query query = qp.parse(queryString);
hits=is.search(query);
System.out.print(hits.length());
}
catch(Exception e){
System.out.print(e);
}
return hits;
}
}