Ackage COM. zxf. demo; import Java. io. bufferedreader; import Java. io. file; import Java. io. fileinputstream; import Java. io. inputstreamreader; import Org. apache. lucene. analysis. analyzer; import Org. apache. lucene. analysis. standard. standardanalyzer; import org.apache.e.doc ument. document; import org.apache.e.doc ument. field; import org.apache.e.doc ument. stringfield; import org.apache.w.e.doc ument. textfield; import Org. apache. lucene. index. directoryreader; 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; public class implements eforudemo {static final string indexpath = system. getproperty ("user. dir ") +" \ Index "; static final string datapath = system. getproperty ("user. dir ") +" \ data "; public static void main (string [] ARGs) {try {policeforudemo. indexdirectory (); luceneforudemo. search ();} catch (exception e) {e. printstacktrace () ;}/ *** create Index * @ throws exception */public static void indexdirectory () throws exception {file indexdir = new file (effeceforudemo. indexpath); file datadir = new file (effeceforudemo. datapath); analyzer = new standardanalyzer (version. required e_44); indexwriterconfig IWC = new indexwriterconfig (version. export e_44, analyzer); directory dir = fsdirectory. open (indexdir); indexwriter = new indexwriter (Dir, IWC); file [] datafiles = datadir. listfiles (); For (File file: datafiles) {fileinputstream instream = new fileinputstream (File); document DOC = new document (); field fullfilename = new stringfield ("fullfilename ", file. getCanonicalPath (), field. store. yes); Doc. add (fullfilename); Doc. add (New textfield ("contents", new bufferedreader (New inputstreamreader (instream, "UTF-8"); indexwriter. adddocument (DOC);} indexwriter. close ();}/* search */public static void search () throws exception {string field = "contents"; string querystr = "test "; // search for the string file indexdir = new file (effeceforudemo. indexpath); indexreader reader = directoryreader. open (fsdirectory. open (indexdir); indexsearcher searcher = new indexsearcher (Reader); analyzer = new standardanalyzer (version. required e_44); queryparser parser = new queryparser (version. paie_44, field, analyzer); query = parser. parse (querystr); system. out. println ("Searching for:" + query. tostring (field); topdocs Results = searcher. search (query, 50); scoredoc [] hits = results. scoredocs; int numtotalhits = results. totalhits; system. out. println (numtotalhits + "Total matching in documents"); For (scoredoc SD: HITS) {document DOC = searcher.doc(sd.doc); system. out. println (Doc. get ("fullfilename "));}}}
Lucene 4.4 demo