建立索引的java代码:package com.delphibbs.search;import java.sql.*;
import org.apache.lucene.index.*;
import org.apache.lucene.document.*;public class indexmysql {
public indexmysql() {
} public static void main(String[] args) {
try {
long start = System.currentTimeMillis(); org.apache.lucene.index.IndexWriter writer = new org.apache.lucene.index.
IndexWriter("index",
// new org.apache.lucene.analysis.cjk.CJKAnalyzer(), true);
new org.apache.lucene.analysis.cn.ChineseAnalyzer(), true);
indexDocs(writer);
writer.optimize();
writer.close();
System.out.print(System.currentTimeMillis() - start);
System.out.println(" total milliseconds"); }
catch (Exception e) {
System.out.println(" 出错了 " + e.getClass() +
"\n 错误信息为: " + e.getMessage());
}
} public static void indexDocs(IndexWriter writer) throws Exception {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=I:\\delphibbs\\DelphiBBS20030801-1031.mdb";
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery(
"select ID,CONTENT from letters where ID<2077061");
//最初我是select all,上了次厕所回来之后,程序都还没有执行能够完,只好强行终止了。
////后来我只取了2000条记录出来,不过也还是花了两分多钟才把索引建立好。
while (rs.next()) { writer.addDocument(com.delphibbs.search.mysqldocument.Document(
rs.getString("ID"), rs.getString("CONTENT")));
///只对id和帖子内容进行了建立索引
}
rs.close();
stmt.close();
conn.close();
}
catch (Exception ex) {
System.out.println("数据库操作有问题");
System.out.println(ex.getMessage());
ex.printStackTrace();
}
System.out.println("索引创建完毕");
}}/////////上面用到的mysqldocument.javapackage com.delphibbs.search;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.DateField;public class mysqldocument {
public static Document Document(String id,String content){
Document doc = new Document();
doc.add(Field.Keyword("ID", id));
doc.add(Field.Text("CONTENT", content));
return doc;
}
public mysqldocument() {
}
}
查询帖子的java文件:searchmysql.javapackage com.delphibbs.search;import org.apache.lucene.document.Document;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Hits;
import org.apache.lucene.analysis.cn.ChineseAnalyzer;
import org.apache.lucene.queryParser.QueryParser;public class searchmysql {
public searchmysql() {
} public static void main(String[] args) {
try {
Searcher searcher = new IndexSearcher("index");
ChineseAnalyzer analyzer = new ChineseAnalyzer();
//org.apache.lucene.analysis.cjk.CJKAnalyzer analyzer=new org.apache.lucene.analysis.cjk.CJKAnalyzer();
Query query = QueryParser.parse("游戏网站", "content", analyzer);
///上面的“游戏网站”即是需要查询的关键字
System.out.println("正在查找: " + query.toString("content"));
Hits hits = searcher.search(query);
System.out.println(hits.length() + " total matching documents");
java.text.NumberFormat format = java.text.NumberFormat.getNumberInstance();
for (int i = 0; i < hits.length(); i++) {
//开始输出查询结果
Document doc = hits.doc(i);
System.out.println(doc.get("ID"));
System.out.println("准确度为:" + format.format(hits.score(i) * 100.0) + "%");
System.out.println(doc.get("CONTENT"));
}
}
catch (Exception e) {
System.out.println(" 出错了 " + e.getClass() +
"\n 错误信息为: " + e.getMessage());
} }}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货