The following program uses the Lucene-2.0.0 Database: MySQL
Function: Create an index for the database. Enter a query to obtain the corresponding content, which is similar to an SQL statement. This program is only used for exercises.
Database fields: ID name age: after entering a person's name, you can obtain the corresponding ID and age
The source code is as follows:
Package test;
Import java. AWT. List;
Import java. Io. bufferedinputstream;
Import java. Io. bufferedreader;
Import java. Io. file;
Import java. Io. fileinputstream;
Import java. Io. filenotfoundexception;
Import java. Io. ioexception;
Import java. Io. inputstreamreader;
Import java. Io. reader;
Import java. Io. stringreader;
Import java. SQL .*;
Import org. Apache. Lucene. Search. Hits;
Import org. Apache. Lucene. Search. query;
Import org. Apache. Lucene. analysis. tokenstream;
Import org. Apache. Lucene. analysis. Standard. standardanalyzer;
Import org.apache.e.doc ument .*;
Import org. Apache. Lucene. Index. indexreader;
Import org. Apache. Lucene. Index. indexwriter;
Import org. Apache. Lucene. Index. termpositionvector;
Import org. Apache. Lucene. queryparser. queryparser;
Import org. Apache. Lucene. Search. indexsearcher;
Import org. Apache. Lucene. Store. Directory;
Import org. Apache. Lucene. Store. fsdirectory;
Import org. Apache. Lucene. Store. ramdirectory;
Import jeasy. analysis. mmanalyzer;
Public class testlucene {
Private Static resultset recrd;
Static mmanalyzer analyzer = new mmanalyzer ();
/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
// Mmanalyzer analyzer = new mmanalyzer ();
Try {
File indexpath = new file ("F: // Luo yunfa's //");
Indexwriter writer = new indexwriter (indexpath,
Analyzer, true );
Recrd = getconn (); // obtain the database record set
// While (recrd. Next ()){
// System. Out. println (recrd. getstring (1 ));
//
//}
Indexbuilder (writer); // create an index
Bufferedreader breader = new bufferedreader (New inputstreamreader (system. In ));
String query = breader. Readline ();
// System. Out. println (query );
Hits hits = search (query); // After the query content is entered, the query
For (INT I = 0; I Document document=hits.doc (I );
System. Out. println (document. Get ("ID "));
System. Out. println (document. Get ("name "));
System. Out. println (document. Get ("Age "));
}
} Catch (exception e ){
// Todo: handle exception
}
}
Public static hits search (string qustring) {// search for characters entered by the user
Hits hits = NULL;
Try {
Indexsearcher isearcher = new indexsearcher ("F: // ro published //");
Queryparser parser = new queryparser ("name", analyzer );
Query query = parser. parse (qustring );
// System. Out. println (query. tostring ());
Hits = isearcher. Search (query );
Return hits;
} Catch (exception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Return NULL;
}
Public static void indexbuilder (indexwriter fwriter) throws exception {// create an index
While (recrd. Next ()){
Directory ramdirectory = new ramdirectory ();
Indexwriter ramwriter = new indexwriter (ramdirectory,
New standardanalyzer (), true );
Document document = new document ();
Field id = new field ("ID", recrd. getstring ("ID"), field. Store. Yes,
Field. Index. tokenized );
Field name = new field ("name", recrd. getstring ("name"), field. Store. Yes,
Field. Index. tokenized );
Field age = new field ("Age", recrd. getstring ("Age"), field. Store. Yes,
Field. Index. tokenized );
Document. Add (ID );
Document. Add (name );
Document. Add (AGE );
Ramwriter. adddocument (document );
Ramwriter. Close ();
Fwriter. addindexes (new directory [] {ramdirectory });
}
}
Public static resultset getconn () {// create a database connection and return the result
Try {
Class. forname ("com. MySQL. JDBC. Driver ");
String url = "JDBC: mysql: // localhost: 3306/test ";
Connection conn = drivermanager. getconnection (URL, "root", "Alen ");
Statement stmt = conn. createstatement (resultset. type_scroll_sensitive, resultset. concur_updatable );
String SQL = "select * from student ";
Resultset rs = stmt.exe cutequery (SQL );
Return Rs;
} Catch (exception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Return NULL;
}
}
For example, enter Alen.
Result: 1
Alen
18
Of course, you can control the result output in the following code:
For (INT I = 0; I Document document=hits.doc (I );
System. Out. println (document. Get ("ID "));
System. Out. println (document. Get ("name "));
System. Out. println (document. Get ("Age "));
}