Create a search class that provides the search function (runable)
/*
* The main function of this code is for the files after the index is created,
* Create a search class that provides the search function.
**/
Package ch2.lucenedemo. process;
Import java. io. BufferedReader;
Import java. io. File;
Import java. io. FileReader;
Import java. io. IOException;
Import java. util. Date;
Import java. util. Iterator;
Import java. util. LinkedHashMap;
Import java. util. Map;
Import org. apache. lucene. index. Term;
Import org. apache. lucene. index. TermDocs;
Import org. apache. lucene. search. IndexSearcher;
Import org. apache. lucene. search. Query;
Import org. apache. lucene. search. TermQuery;
Public class Search {
// Define the directory generated by the index
Private String INDEX_STORE_PATH = "E: \ Lucene project \ index directory ";
// Search by Lucene
Public void indexSearch (String searchType, String searchKey ){
Try {
System. out. println ("++ using index search ++ ");
System. out. println ("----------------------------------");
// Create an IndexSearcher Based on the index location
IndexSearcher searcher = new IndexSearcher ("INDEX_STORE_PATH ");
// Term is the minimum unit for searching. It is similar to a Chinese character or word.
// Create a search unit. searchType indicates the Field to be searched, and searchKey indicates the keyword.
Term t = new Term (searchType, searchKey );
// A Query is generated by the Term.
Query q = new TermQuery (t );
// Start time of the search
Date beginTime = new Date ();
// Obtain TermDocs
TermDocs termDocs = searcher. getIndexReader (). termDocs (t );
While (termDocs. next ()){
// Number of times the keyword appears in the document is output
System. out. println ("find" + termDocs. freq () + "matches in ");
// Output the keyword search document
System.out.println(searcher.getIndexReader().document(termDocs.doc (). getField ("fileName"). stringValue ());
}
// Search completion time
Date endTime = new Date ();
// Search hard
Long timeofSearch = endTime. getTime ()-beginTime. getTime ();
System. out. println ("total search time using indexes:" + timeofSearch + "ms ");
} Catch (IOException e ){
E. printStackTrace ();
}
}
// Search by String
Public void stringSearch (String keyword, String searchDir ){
System. out. println ("++ use string matching to search ++ ");
System. out. println ("---------------------------------");
File filesDir = new File (searchDir );
// Returns the array of all files in the directory folder.
File [] files = filesDir. listFiles ();
// HashMap saves the Object Name and matches the number of times
Map rs = new LinkedHashMap ();
// Record the Search Start Time
Date beginTime = new Date ();
// Traverse all objects
For (int I = 0; I <files. length; I ++ ){
// Number of initialization matches
Int hits = 0;
Try {
// Read the file content
BufferedReader br = new BufferedReader (new FileReader (files [I]);
StringBuffer sb = new StringBuffer ();
String line = br. readLine ();
While (line! = Null ){
Sb. append (line );
Line = br. readLine ();
}
Br. close ();
// Convert Stringbuffer to String for search
String stringToSearch = sb. toString ();
// Initialize fromIndex
Int fromIndex =-keyword. length ();
// Match keywords one by one
While (fromIndex = stringToSearch. indexOf (keyword, fromIndex + keyword. length ()))! =-1 ){
Hits ++;
}
// Add the file name and matching times to hashMap
Rs. put (files [I]. getName (), new Integer (hits ));
} Catch (IOException e ){
E. printStackTrace ();
}
}
// Output query results
Iterator it = rs. keySet (). iterator ();
While (it. hasNext ()){
String fileName = (String) it. next ();
Integer hits = (Integer) rs. get (fileName );
System. out. println ("find" + hits. intValue () + "matches in" + fileName );
}
// Record End Time
Date endTime = new Date ();
// Get the search time-consuming
Long timeOfSearch = endTime. getTime ()-beginTime. getTime ();
System. out. println ("Total time consumed by string matching" + timeOfSearch + "ms ");
}
Public static void main (String [] args ){
Search search = new Search ();
// Use the index speed lock keyword
Search. indexSearch ("content", "Paul ");
// Insert a separate row
System. out. println ("======================= ");
System. out. println ("======================= ");
// Search for keywords using the String API
Search. stringSearch ("PAUL", "INDEX_STORE_PATH ");
}
}