Lucene Index and retrieve database

Source: Internet
Author: User
Tags createindex sql error stmt

1.Lucene IntroductionLucene is an open-source full-Text Search engine toolkit that provides the complete indexing engine, query engine, and partial text analysis engine. Lucene provides software developers with a set of easy-to-use search engine development kits for full-text retrieval in the system, or a complete full-text search engine based on Lucene. how the full-text search engine works: Each record in the scan question and answer database is indexed, the index records the number and position of the word in each question and answer record, and when the user's question is received, the problem is also segmented, and all the answer records that contain these words are found in the index. Then we calculate the similarity between these question and answer records and the user's question separately, and find the one answer record with the highest similarity returned to the user. Lucene Benefits: The index file format is independent of the application platform, the efficient indexing engine, the powerful query engine, and easy to scale. 2.Lucene indexing and retrieval principlesLucene is able to index and retrieve any data, such as TXT, Word, PDF, database and other formats of data sources, we can use other tools or programmatically to read the data in these formats into text form of data, This allows the text data to be indexed and retrieved using Lucene. 3.Lucene indexing and retrieving databases    1. Create JDBC Get Connection tool class
Package Com.tgb.org;import Java.sql.connection;import Java.sql.drivermanager;import java.sql.sqlexception;/** *  JDBC Get Connection Tool class  * @author Quwenzhe * */public class Jdbcutil {private static Connection conn = Null;private static Final string URL = "Jdbc:mysql://localhost:3306/lucene";p rivate static final String jdbc_driver = "Com.mysql.jdbc.Driver ";p rivate static final String user_name =" root ";p rivate static final String PASSWORD =" root ";p ublic static Connection ge Tconnection () {try {class.forname (jdbc_driver); conn = Drivermanager.getconnection (URL, user_name, PASSWORD);} catch ( ClassNotFoundException e) {e.printstacktrace ();} catch (SQLException e) {e.printstacktrace ();} Return conn;}}
2. Create a JavaBean class
Package Com.tgb.org;public class UserInfo {private string Id;private string Username;public string GetId () {return ID;} public void SetId (String id) {this.id = ID;} Public String GetUserName () {return username;} public void Setusername (String username) {this.username = username;}}
3. Create an index on database information and retrieve the index
Package Com.tgb.org;import Java.io.file;import Java.sql.connection;import java.sql.resultset;import Java.sql.statement;import Java.util.arraylist;import Java.util.list;import Org.apache.lucene.analysis.Analyzer; Import Org.apache.lucene.document.document;import Org.apache.lucene.document.field;import Org.apache.lucene.document.textfield;import Org.apache.lucene.index.indexreader;import Org.apache.lucene.index.indexwriter;import Org.apache.lucene.index.indexwriterconfig;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;import Org.wltea.analyzer.lucene.ikanalyzer;public class SearchLogic {private static Connection conn = null;private static Statement stmt = null;private static ResultSet rs = null;//Index Save directory Private String Indexdir = "D:\\lucence\\index2";p rivate static Indexsearcher searcher = null;//Create a word breaker private static Analyzer Analyzer = new Ikanalyzer (true);/** * Get Database data * @param querystr keywords to retrieve * @return * @throws Exception */public L Ist<userinfo> GetResult (String querystr) throws Exception {list<userinfo> result = Null;conn = Jdbcutil.getconnection (); if (conn = = null) {throw new Exception ("Database connection failed! ");} String sql = "SELECT ID, username from t_user"; try {stmt = Conn.createstatement (); rs = stmt.executequery (sql);//Create indexes on database, Do this once, do not create the index every time you run//after the data has an update can be called in the background update index This.createindex (RS); Topdocs Topdocs = This.search (QUERYSTR); scoredoc[] Scoredocs = Topdocs.scoredocs;result = This.addhits2list (Scoredocs);} catch (Exception e) {e.printstacktrace (); throw new Exception ("Database query SQL Error! SQL: "+ sql);} Finally {if (rs! = null) rs.close (); if (stmt! = null) stmt.close (); if (conn! = null) Conn.close ();} return result;} /** * CREATE index for database retrieve data * @param access resultset returned by database * @throws Exception */private void CreateIndex (ResultSet rs) throws Exception {//Create or open index Directory directory = fsdirectory.open (new File (Indexdi R));//create Indexwriterindexwriterconfig conf = new Indexwriterconfig (version.lucene_46,analyzer); IndexWriter IndexWriter = new IndexWriter (directory, conf);//Traverse resultset CREATE INDEX while (Rs.next ()) {//Create document and add Fielddocument doc = n EW Document ();d Oc.add (new TextField ("id", rs.getstring ("id"), Field.Store.YES));d Oc.add (New TextField ("username", Rs.getstring ("username"), Field.Store.YES));//Add doc to the index indexwriter.adddocument (DOC);} Indexwriter.commit (); Indexwriter.close ();d irectory.close ();} /** * Retrieve index * @param querystr keyword to retrieve * @return * @throws Exception */private topdocs Search (String querystr) throws Excep tion {//Create or open an index directory directories directory = Fsdirectory.open (new File (Indexdir)); Indexreader reader = Indexreader.open ( directory), if (searcher = = null) {searcher = new indexsearcher (reader);} Use the query parser to create queryqueryparser parser = new Queryparser (version.lucene_46, "username",Analyzer); Query query = parser.parse (QUERYSTR);//search from index to get top 10 document Topdocs Topdocs = searcher.search (query); return topdocs;} /** * Add search results to list * @param scoredocs * @return * @throws Exception */private list<userinfo> addhits2list (scoredoc[ ] scoredocs) throws Exception {list<userinfo> Listbean = new arraylist<userinfo> (); UserInfo bean = null;for (int i = 0; i < scoredocs.length; i++) {int docId = scoredocs[i].doc;document doc = SEARCHER.D OC (docId), Bean = new UserInfo (), Bean.setid (Doc.get ("id")), Bean.setusername (Doc.get ("username")), Listbean.add (bean );} return Listbean;} public static void Main (string[] args) {searchlogic logic = new searchlogic (); try {Long startTime = System.currenttimemill Is (); list<userinfo> result = Logic.getresult ("Kuvenzhe"); int i = 0;for (UserInfo bean:result) {if (i = =) break; System.out.println ("Bean.name" + bean.getclass (). GetName () + ": bean.id" + bean.getid () + ": bean.username" + Bean.getu Sername ()); i++;} System.out.println ("SearchBean.result.size: "+ result.size ()); Long endTime = System.currenttimemillis (); System.out.println ("The time spent in the query is:" + (Endtime-starttime)/1000);} catch (Exception e) {e.printstacktrace (); System.out.println (E.getmessage ());}}}
Project-dependent jar:

After reading the above code, we can find that Lucene indexes the return value of the Access database resultset, and then retrieves the matching data by retrieving the index, thus improving the query efficiency of the database.

Lucene Index and retrieve database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.