/*
* The main function of this code is to post the index of the created file.
* Create a search function to search for classes.
* */
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 index-generated folders
Private String Index_store_path = "E:\\lucene item \ \ Index Folder";
Using Lucene's search
public void Indexsearch (string searchtype, String searchkey) {
try{
System.out.println ("+ + using indexed search + +");
System.out.println ("----------------------------------");
Establish Indexsearcher based on index location
Indexsearcher searcher = new Indexsearcher ("Index_store_path");
Term is the smallest unit of search, similar to a Chinese character or word
Create a search unit, SearchType represents the field to search, Searchkey represents keyword
Term T = new term (searchtype, searchkey);
Generate a query from the term
Query q = new Termquery (t);
The time the search started
Date BeginTime = new Date ();
Gets a <document, Frequency> enumeration Object Termdocs
Termdocs Termdocs = Searcher.getindexreader (). Termdocs (t);
while (Termdocs.next ()) {
The number of times the keyword output appears in the document
System.out.println ("Find" + termdocs.freq () + "matches in");
Output a document that searches for a keyword
System.out.println (Searcher.getindexreader (). Document (Termdocs.doc ()). GetField ("FileName"). StringValue ());
}
Search Complete Time
Date EndTime = new Date ();
The search has consumed a hard
Long Timeofsearch = Endtime.gettime ()-begintime.gettime ();
SYSTEM.OUT.PRINTLN ("Total time spent using indexed search:" + timeofsearch + "MS");
}catch (IOException e) {
E.printstacktrace ();
}
}
Search using string
public void Stringsearch (string keyword, string searchdir) {
System.out.println ("+ + uses string matching method to search + +");
System.out.println ("---------------------------------");
File Filesdir = new file (Searchdir);
Return folder folder All files array
file[] files = filesdir.listfiles ();
HashMap save file name and number of matches
Map rs = new Linkedhashmap ();
Record Search Start time
Date BeginTime = new Date ();
Traverse All Files
for (int i = 0; i < files.length; i++) {
Initialize number of Matches
int hits = 0;
try{
Read File contents
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 easy search
String Stringtosearch = sb.tostring ();
Initialize Fromindex
int fromIndex =-keyword.length ();
Match keyword individually
while ((FromIndex = stringtosearch.indexof (keyword, fromIndex + keyword.length ())) =-1) {
hits++;
}
Increase file name and number of matches 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 search Time consuming
Long Timeofsearch = Endtime.gettime ()-begintime.gettime ();
System.out.println ("Use string matching method total time consuming" + Timeofsearch + "MS");
}
public static void Main (string[] args) {
Search search = new search ();
Keyword by index speed lock
Search.indexsearch ("Content", "Paul");
Insert a sub-interlaced
System.out.println ("=========================");
System.out.println ("=========================");
Search for keywords via the string API
Search.stringsearch ("Paul", "Index_store_path");
}
}
Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.
Create a search function to search for a class (executable file)