There are not many classes to be searched:
Package Org. apache. lucene. searcher; import Java. io. file; import Java. io. ioexception; import Org. apache. lucene. analysis. standard. standardanalyzer; import org.apache.e.doc ument. document; import Org. apache. lucene. index. indexreader; import Org. apache. lucene. indexer. indexer; import Org. apache. lucene. queryparser. parseexception; import Org. apache. lucene. queryparser. 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;/*** used to search the content of the index file * @ author administrator **/public class searcher {public static void main (string [] ARGs) throws exception {If (ARGs. length! = 3) {Throw new exception ("Usage: Java" + indexer. class. getname () + "<index dir> <string> <query>") ;}// the index storage directory string indexdir = ARGs [0]; // query part, for example, to query in the content, query string S = ARGs [1] in the directory; // to query string query = ARGs [2]; search (indexdir, S, query );} /*** search the required condition based on the entered string * @ Param indexdir: directory where the index is stored * @ Param s which part of the index is queried * @ Param Query * @ throws ioexception * @ throws parseexception */Private Static void search (string indexdir, string S, string query) throws ioexception, parseexception {// open the directory that stores the index dir = fsdirectory. open (new file (indexdir); indexreader reader = indexreader. open (DIR); indexsearcher is = new indexsearcher (Reader); // The parameter is the Lucene version. For the part to be queried, the standard word divider queryparser parser = new queryparser (version. lucene_35, S, new standardanalyzer (version. required e_35); query q = parser. parse (query); long start = system. currenttimemillis (); // obtain the query result. Each page displays 10 topdocs hits = is. search (Q, 10); system. out. println ("found" + hits. totalhits); long end = system. currenttimemillis (); system. out. println ("Search use" + (end-Start) + "milliseconds"); For (INT I = 0; I
In a project, if reader is a Singleton, You need to modify the code:
IndexSearch is = getSearcher();is.close();
/*** Set reader as a singleton. Generally, there is only one reader in the project cycle. Multiple writer */public IndexSearcher getSearcher () can be used () {try {if (reader = null) {reader = IndexReader. open (directory);} else {IndexReader tr = IndexReader. openIfChanged (reader); if (tr! = Null) {reader. close (); reader = tr;} return new IndexSearcher (reader);} catch (CorruptIndexException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} return null ;}
Experiment results:
Found3search use 14 millisecondsD:\abc\lucene\abc.txtD:\abc\lucene\hello.txtD:\abc\lucene\car.txt
Some Classes used in the above classes are described as follows:
IndexSearcher is used to search for indexes created by IndexWriter. As a class, an index is opened in read-only mode.
A Term is composed of a series of string elements.
Query is a general abstract parent class that contains some tool methods. One of the methods is setBoost.
(Float) Make sure that the Lucene search results match the user's search keywords.
TermQuery is a subclass of Query. It is used to match documents containing special values.
TopDocs is the result returned after the search
This class shows N sorting results before