package jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.UUID; public class ConnectionUtil { private static String driver="com.mysql.cj.jdbc.Driver"; private static String url="jdbc:mysql://localhost:3306/solrdb?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true"; private static String user="root"; private static String password="123456"; public static Connection getConn() { try { Class.forName(driver); Connection conn=DriverManager.getConnection(url, user, password); return conn; } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public void CreateData() { Connection conn=getConn(); try { PreparedStatement ps=conn.prepareStatement ("insert into paper (id,name,title,author,content) values (?,?,?,?,?)"); for(int i=0;i<100;i++) { ps.setString(1,UUID.randomUUID().toString()); ps.setString(2,"專家233"+i); ps.setString(3,"杭州九次人大會議"+i); ps.setString(4,"記者"); ps.setString(5, "北京時間2019-2-13日,杭州市舉行了人大代表會議,會議決定現在...."+i); ps.executeUpdate(); } System.out.println("執行完畢"); } catch (SQLException e) { e.printStackTrace(); } } public static void main(String[] args) { ConnectionUtil util=new ConnectionUtil(); util.CreateData(); } }
//本類只為造數據
package jdbc;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
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.FieldType;
import org.apache.lucene.index.IndexOptions;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
/**
* Lucene 創建索引
* @author moonxy
*
*/
public class CreateIndexForMysql {
public static List<Paper> CreateData() {
Connection conn=ConnectionUtil.getConn();
List<Paper>list=new ArrayList<Paper>();
try {
PreparedStatement ps=conn.prepareStatement
("select * from paper ");
ResultSet rs=ps.executeQuery();
//4.處理數據庫的返回結果(使用ResultSet類)
while(rs.next()){
Paper paper=new Paper(rs.getString("id"),
rs.getString("name"), rs.getString("title"),
rs.getString("author"),rs.getString("content"));
list.add(paper);
}
System.out.println("執行完畢");
return list;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
// 創建3個News對象
// 開始時間
Date start = new Date();
System.out.println("**********開始創建索引**********");
// 創建IK分詞器
Analyzer analyzer = new StandardAnalyzer();
IndexWriterConfig icw = new IndexWriterConfig(analyzer);
// CREATE 表示先清空索引再重新創建
icw.setOpenMode(OpenMode.CREATE);
Directory dir = null;
IndexWriter inWriter = null;
// 存儲索引的目錄
Path indexPath = Paths.get("indexdir");
try {
if (!Files.isReadable(indexPath)) {
System.out.println("索引目錄 '" + indexPath.toAbsolutePath() + "' 不存在或者不可讀,請檢查");
System.exit(1);
}
dir = FSDirectory.open(indexPath);
inWriter = new IndexWriter(dir, icw);
//自己設置字段索引類型
// 設置ID索引並存儲
FieldType idType = new FieldType();
idType.setIndexOptions(IndexOptions.DOCS);
idType.setStored(true);
// 設置標題索引文檔、詞項頻率、位移信息和偏移量,存儲並詞條化
FieldType titleType = new FieldType();
titleType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
titleType.setStored(true);
titleType.setTokenized(true);
FieldType contentType = new FieldType();
contentType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
contentType.setStored(true);
contentType.setTokenized(true);
contentType.setStoreTermVectors(true);
contentType.setStoreTermVectorPositions(true);
contentType.setStoreTermVectorOffsets(true);
List<Paper>list=CreateData();
for(int i=0;i<list.size();i++) {
Document doc = new Document();
doc.add(new Field("id", list.get(i).getId(), idType));
doc.add(new Field("title", list.get(i).getTitle(), titleType));
inWriter.addDocument(doc);
}
//
// doc1.add(new Field("id", String.valueOf(news1.getId()), idType));
// doc1.add(new Field("title", news1.getTitle(), titleType));
// doc1.add(new Field("content", news1.getContent(), contentType));
// doc1.add(new IntPoint("reply", news1.getReply()));
// doc1.add(new StoredField("reply_display", news1.getReply()));
//
// Document doc2 = new Document();
// doc2.add(new Field("id", String.valueOf(news2.getId()), idType));
// doc2.add(new Field("title", news2.getTitle(), titleType));
// doc2.add(new Field("content", news2.getContent(), contentType));
// doc2.add(new IntPoint("reply", news2.getReply()));
// doc2.add(new StoredField("reply_display", news2.getReply()));
//
// Document doc3 = new Document();
// doc3.add(new Field("id", String.valueOf(news3.getId()), idType));
// doc3.add(new Field("title", news3.getTitle(), titleType));
// doc3.add(new Field("content", news3.getContent(), contentType));
// doc3.add(new IntPoint("reply", news3.getReply()));
// doc3.add(new StoredField("reply_display", news3.getReply()));
// inWriter.addDocument(doc1);
// inWriter.addDocument(doc2);
// inWriter.addDocument(doc3);
inWriter.commit();
inWriter.close();
dir.close();
} catch (IOException e) {
e.printStackTrace();
}
Date end = new Date();
System.out.println("索引文檔用時:" + (end.getTime() - start.getTime()) + " milliseconds");
System.out.println("**********索引創建完成**********");
}
}
//以上為構建索引代碼
///查詢代碼
package query;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.queryparser.classic.QueryParser.Operator;
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;
/**
* 單域搜索
* @author moonxy
*
*/
public class QueryParseTest {
public static void main(String[] args) throws ParseException, IOException {
// 搜索單個字段
String field = "title";
// 搜索多個字段時使用數組
//String[] fields = { "title", "content" };
Path indexPath = Paths.get("indexdir");
Directory dir = FSDirectory.open(indexPath);
IndexReader reader = DirectoryReader.open(dir);
IndexSearcher searcher = new IndexSearcher(reader);
Analyzer analyzer = new StandardAnalyzer();//最細粒度分詞
QueryParser parser = new QueryParser(field, analyzer);
// 多域搜索
//MultiFieldQueryParser multiParser = new MultiFieldQueryParser(fields, analyzer);
// 關鍵字同時成立使用 AND, 默認是 OR
parser.setDefaultOperator(Operator.AND);
// 查詢語句
// Query query = parser.parse("農村學生");//查詢關鍵詞
Query query = parser.parse("次人");//查詢關鍵詞
System.out.println("Query:" + query.toString());
// 返回前10條
TopDocs tds = searcher.search(query, 100);
for (ScoreDoc sd : tds.scoreDocs) {
// Explanation explanation = searcher.explain(query, sd.doc);
// System.out.println("explain:" + explanation + "\n");
Document doc = searcher.doc(sd.doc);
// System.out.println("DocID:" + sd.doc);
// System.out.println("id:" + doc.get("id"));
System.out.println("title:" + doc.get("title"));
// System.out.println("content:" + doc.get("content"));
// System.out.println("文檔評分:" + sd.score);
}
dir.close();
reader.close();
}
}
注意我用的是MySQL 8.0 其他版本的自行更改對應的driverClass url
