首页 > 学院 > 开发设计 > 正文

lucene数据库查询

2019-11-08 01:24:11
字体:
来源:转载
供稿:网友

首先我们先把库下载了,我这里是lucene-6.3.0版本,所需jar包: http://download.csdn.net/detail/yabaj/9757979 在数据库中查询指定字段内容,将该条记录输出

public class TestLucene { PRivate static ResultSet recrd; private static IndexSearcher iSearcher; private static Directory ramDirectory = new RAMDirectory(); /** * @param args */ public static void main(String[] args) { // MMAnalyzer analyzer=new MMAnalyzer(); try { recrd = getConn(); // 获取数据库的记录集 // while (recrd.next()) { // System.out.println(recrd.getString(1)); // // } Date date1 = new Date(); IndexBuilder(); // 建立索引 Date date2 = new Date(); System.out.println("创建索引-----耗时:" + (date2.getTime() - date1.getTime()) + "ms/n"); BufferedReader bReader = new BufferedReader(new InputStreamReader( System.in)); String query = bReader.readLine(); // System.out.println(query); TopDocs hits = search(query); // 输入查询内容后,查询 ScoreDoc[] scoreDocs = hits.scoreDocs; for (int i = 0; i < scoreDocs.length; i++) { // 返回查询后结果 int docId = scoreDocs[i].doc; Document document = iSearcher.doc(docId); System.out.println(document.get("ID")); System.out.println(document.get("Name")); System.out.println(document.get("Pass")); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } public static TopDocs search(String quString) { // 搜索用户输入的字符 TopDocs hits = null; try { iSearcher = new IndexSearcher(DirectoryReader.open(ramDirectory)); QueryParser parser = new QueryParser("Name", getAnalyzer()); Query query = parser.parse(quString); // System.out.println(query.toString()); hits = iSearcher.search(query, 10); return hits; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public static void IndexBuilder() throws Exception { // 建立索引 while (recrd.next()) { IndexWriterConfig indexWriterConfig = new IndexWriterConfig( new StandardAnalyzer()); IndexWriter ramWriter = new IndexWriter(ramDirectory, indexWriterConfig); Document document = new Document(); String _id = recrd.getInt("id") + ""; System.out.println("id=" + _id); Field id = new TextField("ID", _id, Field.Store.YES); Field name = new TextField("Name", recrd.getString("Name"), Field.Store.YES); Field pass = new TextField("Pass", recrd.getString("pass"), Field.Store.YES); document.add(id); document.add(name); document.add(pass); ramWriter.addDocument(document); ramWriter.close(); // fWriter.addIndexes(new Directory[] { ramDirectory }); } } public static Analyzer getAnalyzer() { return new StandardAnalyzer(); } private IndexSearcher getIndexSearcher() throws IOException { return new IndexSearcher(DirectoryReader.open(new RAMDirectory())); } public static ResultSet getConn() { // 建立数据库连接,并返回结果 try { Class.forName("com.MySQL.jdbc.Driver"); // &autoReconnect=true&failOverReadOnly=false&maxReconnects=10 String connString = "jdbc:mysql://localhost:3306/hibernate?user=root&passWord=123456"; Connection conn = DriverManager.getConnection(connString); Statement stmt = conn.createStatement(); String sql = "select * from user"; ResultSet rs = stmt.executeQuery(sql); return rs; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表