用lucene替代mysql读库的尝试

时间:2024-01-09 21:28:02

采用lucene对mysql中的表建索引,并替代全文检索操作。

备注:代码临时梳理很粗糙,后续修改。

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date; 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.document.StringField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version; public class App { private static Directory dir = null; /**
* @param args
*/
public static void main(String[] args) { try {
initIndex();
searchBylucene();
searchByMysql();
} catch (IOException | ParseException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} //创建索引
private static void initIndex() throws IOException{ dir = FSDirectory.open(new File("E:\\index")); Analyzer analyzer = new StandardAnalyzer();
IndexWriterConfig iwc = new IndexWriterConfig(
Version.LUCENE_4_10_2, analyzer);
iwc.setOpenMode(OpenMode.CREATE);
IndexWriter writer = new IndexWriter(dir, iwc); Connection conn = null; conn = DbUtil.getcon(); Statement stmt; try {
stmt = conn.createStatement();
String sql = "select * from t_content";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) { Document doc = new Document();
doc.add(new StringField("id", rs.getString(1), Field.Store.YES));
doc.add(new StringField("content",
rs.getString(2),
Field.Store.YES)); writer.addDocument(doc);
}
} catch (SQLException e) {
e.printStackTrace();
DbUtil.Close(conn);
}finally {
DbUtil.Close(conn);
writer.close();
}
} private static void searchByMysql() throws SQLException{
Connection conn = null; Date date1 = new Date();
conn = DbUtil.getcon();
Statement stmt = conn.createStatement(); String sql = "select * from t_content where content like '%内马尔%'";
ResultSet rs = stmt.executeQuery(sql); Date date2 = new Date(); System.out.println("Mysql:" + String.valueOf(date2.getTime() - date1.getTime())); int i= 0; while (rs.next()) {
//System.out.println(rs.getString(2));
i++;
} System.out.println(i); } private static void searchBylucene() throws IOException, ParseException{
IndexReader reader = DirectoryReader.open(dir);
IndexSearcher searcher = new IndexSearcher(reader);
Date date1 = new Date();
QueryParser parser = new QueryParser("content", new StandardAnalyzer());
Query query = parser.parse("内马尔"); TopDocs rs = searcher.search(query, null, 10);
Date date2 = new Date(); System.out.println("lucene:" + String.valueOf(date2.getTime() - date1.getTime()));
System.out.println(rs.totalHits);
if(rs.totalHits != 0){
ScoreDoc[] hits = rs.scoreDocs;
//System.out.println(searcher.doc(rs.scoreDocs[0].doc));
}
//System.out.println("end");
}
}