前几日多少熟悉了一下Jsp+servlet+javabean,这两天就想着更进一层,做一个简单的搜索引擎,采用了Lucene3.6.2.下面详细说一下。
前面那篇博客《Jsp+servlet+javabean》只是通过数据库检索进行的视频搜索,本篇使用了Lucene,Jsp文件基本上不变,servlet文件也基本上不变,javabean文件彻底换了:
package test;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
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.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
public class Luce36 {
//定义索引文件存放的路径
public String resu;
public String query;
public Luce36()
{
resu=new String();
query=null;
}
static File indexDir=new File( "F:/DBindex" );
public String getQuery()
{
return query;
}
public void setQuery(String queryString)
{
query=queryString;
}
public String sear(String queryString)throws Exception
{
String url="jdbc:mysql://localhost/try_java";
String user="root";
String pwd="123";
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//建立到MySQL的连接
Connection conn = DriverManager.getConnection(url,user, pwd);
//执行SQL语句
Statement stmt = conn.createStatement();//创建语句对象,用以执行sql语言
ResultSet rs = stmt.executeQuery("select * from cid117");
//建立一个标准分析器
Analyzer luceneAnalyzer=new StandardAnalyzer(Version.LUCENE_36);
//创建一个索引器
IndexWriterConfig writerConfig = new IndexWriterConfig(Version.LUCENE_36, luceneAnalyzer);
IndexWriter writer = new IndexWriter(FSDirectory.open(indexDir), writerConfig);
//处理结果集
while (rs.next())
{
Document doc=new Document();
doc.add(new Field("title", rs.getString("title"), Field.Store.YES,Field.Index.ANALYZED));
doc.add(new Field("desc", rs.getString("desc"), Field.Store.YES,Field.Index.ANALYZED));
doc.add(new Field("picPath", rs.getString("picPath"), Field.Store.YES,Field.Index.ANALYZED));
doc.add(new Field("videoURL", rs.getString("videoURL"), Field.Store.YES,Field.Index.ANALYZED));
//doc.add(new Field("directors", rs.getString("directors"), Field.Store.YES,Field.Index.ANALYZED));
//doc.add(new Field("actors", rs.getString("actors"), Field.Store.YES,Field.Index.ANALYZED));
writer.addDocument(doc);
}
writer.close();
//进行搜索
resu=Dosearch(queryString);
//关闭数据库
rs.close();
stmt.close();
conn.close();
return resu;
}
//搜索模块代码 // IOException,ParseException
public String Dosearch(String queryString) throws Exception{
Query query=null;
IndexReader reader = IndexReader.open(FSDirectory.open(indexDir));
IndexSearcher searcher= new IndexSearcher(reader);
Analyzer analyzer= new StandardAnalyzer(Version.LUCENE_36);
String docstr=new String();
//要查询的内容
//try
//{
QueryParser qp = new QueryParser( Version.LUCENE_36,"title" , analyzer);
query = qp.parse(queryString);
// }
//catch (ParseException e)
//{
//System.out.println("我擦,抛出异常");
///docstr="我擦,抛出异常!";
//}
if (searcher != null )
{
ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;
if (hits.length > 0 )
{
for(int num=0;num<hits.length;num++)
{
Document doc=searcher.doc(hits[num].doc);
docstr+=doc.get("title");
//System.out.println("结果:"+hits.doc(num));
//System.out.println("相关度(积分):"+hits.score(num));
//System.out.println("记录ID:"+hits.id(num));
//System.out.println("片名是:"+doc.get("title"));//输出的是文本内容
//System.out.println("描述是:"+doc.get("desc"));
}
}
else
{
//System.out.println("我擦,没找到视频资源!");
//docstr="我擦,没找到视频资源!";
docstr=null;
}
}
return docstr;
}
}
需要的jar文件如图:,至此完成了javabean的撰写,但是不知道什么原因,保存这个javabean文件为Luce36.java后没有产生相应的class文件,在网上找了很久也没有解决,逐自己在DOS下采用命令行编译,然后copy到classes目录下,重启tomcat后运行,会报错:java.lang.UnsupportedClassVersionError: Unsupported major.minor version 51.0的错误 , 通过设置compiler compliance level解决,本项目统一设置为1.6.