lucene為數據庫數據創建索引並查詢索引庫


1.

  1 package com.home.utils;
  2 
  3 import java.io.IOException;
  4 import java.sql.Connection;
  5 import java.sql.DriverManager;
  6 import java.sql.ResultSet;
  7 import java.sql.SQLException;
  8 import java.sql.Statement;
  9 import java.util.ArrayList;
 10 import java.util.List;
 11 
 12 import org.apache.lucene.analysis.Analyzer;
 13 import org.apache.lucene.analysis.standard.StandardAnalyzer;
 14 import org.apache.lucene.document.Document;
 15 import org.apache.lucene.document.Field;
 16 import org.apache.lucene.document.Field.Store;
 17 import org.apache.lucene.index.IndexWriter;
 18 import org.apache.lucene.index.IndexWriter.MaxFieldLength;
 19 import org.apache.lucene.queryParser.QueryParser;
 20 import org.apache.lucene.search.IndexSearcher;
 21 import org.apache.lucene.search.Query;
 22 import org.apache.lucene.search.ScoreDoc;
 23 import org.apache.lucene.search.TopDocs;
 24 import org.apache.lucene.util.Version;
 25 
 26 public class TestLucene {
 27 
 28     static {
 29         try {
 30             // 加載MySql的驅動類
 31             Class.forName("com.mysql.jdbc.Driver");
 32         } catch (ClassNotFoundException e) {
 33             System.out.println("找不到驅動程序類 ,加載驅動失敗!");
 34             e.printStackTrace();
 35         }
 36     }
 37 
 38     public static Connection createConnection() {
 39 
 40         String url = "jdbc:mysql://localhost:3306/cool_files";
 41         String username = "root";
 42         String password = "123456";
 43         Connection con = null;
 44         try {
 45             con = DriverManager.getConnection(url, username, password);
 46         } catch (SQLException se) {
 47             System.out.println("數據庫連接失敗!");
 48             se.printStackTrace();
 49         }
 50 
 51         return con;
 52     }
 53 
 54     public static ResultSet getResult(String sql, Connection conn) {
 55         try {
 56             Statement stmt = conn.createStatement();
 57             ResultSet rs = stmt.executeQuery(sql);
 58             return rs;
 59         } catch (SQLException e) {
 60             System.out.println(e);
 61         }
 62         return null;
 63     }
 64 
 65     public void close(ResultSet rs, Statement stmt, Connection conn) {
 66 
 67         if (rs != null) { // 關閉記錄集
 68             try {
 69                 rs.close();
 70             } catch (SQLException e) {
 71                 e.printStackTrace();
 72             }
 73         }
 74         if (stmt != null) { // 關閉聲明
 75             try {
 76                 stmt.close();
 77             } catch (SQLException e) {
 78                 e.printStackTrace();
 79             }
 80         }
 81         if (conn != null) { // 關閉連接對象
 82             try {
 83                 conn.close();
 84             } catch (SQLException e) {
 85                 e.printStackTrace();
 86             }
 87         }
 88     }
 89 
 90     public static void Index(ResultSet rs) {
 91         try {
 92             IndexWriter indexWriter = new IndexWriter(LuceneUtils.directory,LuceneUtils.analyzer,MaxFieldLength.LIMITED);
 93             while (rs.next()) {
 94                 Document doc = new Document();
 95                 doc.add(new Field("Au_id", rs.getString(1), Store.YES,
 96                         org.apache.lucene.document.Field.Index.ANALYZED));
 97                 doc.add(new Field("Au_name", rs.getString(2), Store.YES,
 98                         org.apache.lucene.document.Field.Index.ANALYZED));
 99                 doc.add(new Field("Phone", rs.getString(3), Store.YES,
100                         org.apache.lucene.document.Field.Index.ANALYZED));
101                 doc.add(new Field("Address", rs.getString(4), Store.YES,
102                         org.apache.lucene.document.Field.Index.ANALYZED));
103                 doc.add(new Field("City", rs.getString(5), Store.YES,
104                         org.apache.lucene.document.Field.Index.ANALYZED));
105                 doc.add(new Field("State", rs.getString(6), Store.YES,
106                         org.apache.lucene.document.Field.Index.ANALYZED));
107                 doc.add(new Field("Zip", rs.getString(7), Store.YES,
108                         org.apache.lucene.document.Field.Index.ANALYZED));
109                 doc.add(new Field("contract", rs.getString(8), Store.YES,
110                         org.apache.lucene.document.Field.Index.ANALYZED));
111                 indexWriter.addDocument(doc);
112             }
113             indexWriter.optimize();
114             indexWriter.close();
115         } catch (IOException e) {
116             System.out.println(e);
117         } catch (SQLException e) {
118             System.out.println(e);
119         }
120     }
121 
122     public static Analyzer getAnalyzer() {
123         return new StandardAnalyzer(Version.LUCENE_30);
124     }
125 
126     public static List<Authors> seacher(String queryString) {
127         List<Authors> authorsList = null;
128         try {
129             IndexSearcher is = new IndexSearcher(LuceneUtils.directory);
130             QueryParser parser = new QueryParser(Version.LUCENE_30, "Au_name",
131                     LuceneUtils.analyzer);
132             Query query = parser.parse(queryString);
133             TopDocs docs = is.search(query, 10);
134             ScoreDoc[] scoreDocs = docs.scoreDocs;
135             authorsList = new ArrayList<Authors>();
136 
137             for (ScoreDoc scoreDoc : scoreDocs) {
138                 int num = scoreDoc.doc;
139                 Document document = is.doc(num);
140                 Authors article = DocumentUtils.document2Authors(document);
141                 authorsList.add(article);
142             }
143 
144         } catch (Exception e) {
145             System.out.print(e);
146         }
147         return authorsList;
148     }
149 }

 

 

2.

 1 package com.home.controller;
 2 
 3 import java.sql.Connection;
 4 import java.sql.ResultSet;
 5 import java.util.List;
 6 
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 
10 import org.apache.lucene.analysis.Analyzer;
11 import org.springframework.stereotype.Controller;
12 import org.springframework.web.bind.annotation.RequestMapping;
13 import org.springframework.web.bind.annotation.RequestParam;
14 import org.springframework.web.bind.annotation.ResponseBody;
15 
16 import com.home.utils.Authors;
17 import com.home.utils.TestLucene;
18 
19 @Controller
20 @RequestMapping("/lucene")
21 public class LuceneController {
22     
23     @ResponseBody
24     @RequestMapping("/search")
25     public List<Authors> getAuthors(HttpServletRequest request,HttpServletResponse response,@RequestParam String name){
26         
27         Connection conn = TestLucene.createConnection();
28         Analyzer analyzer = TestLucene.getAnalyzer();
29         System.out.println(name);
30         ResultSet rs = TestLucene.getResult("select * from Authors where 1=1 and Au_name='"+name+"'", conn);
31         TestLucene.Index(rs);
32         List<Authors> authorsList = TestLucene.seacher(name);
33         
34         return authorsList;
35     }
36     
37     @ResponseBody
38     @RequestMapping("/searchOne")
39     public List<Authors> getAuthorsOne(HttpServletRequest request,HttpServletResponse response,@RequestParam String name){
40         
41         List<Authors> authorsList = TestLucene.seacher(name);
42         
43         return authorsList;
44     }
45 }

 

3、

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <%@ page language="java" contentType="text/html; charset=UTF-8"
 3     pageEncoding="UTF-8"%>
 4 <html xmlns="http://www.w3.org/1999/xhtml">
 5 <%
 6     String path = request.getContextPath();
 7     String basePath = request.getScheme() + "://"
 8             + request.getServerName() + ":" + request.getServerPort()
 9             + path + "/";
10 %>
11 <head>
12 </head>
13 <body>
14     <form name="form2" action="lucene/searchOne" method="post">
15             <input type="text" name="name" /> <input type="submit"
16                 value="搜索" />
17     </form>
18 </body>
19 </html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM