Use lucene-core-3.1.0.jar
1. Create an index
Public class indexer {<br/> Public static void main (string [] ARGs) throws ioexception {<br/> // where the index file is saved <br/> string indexdir = "F: // indexdir "; <br/> // Where the TXT file is to be searched <br/> string datedir = "F: // datedir"; <br/> indexwriter = NULL; <br/> // create a directory object <br/> directory dir = new simplefsdirectory (new file (indexdir); <br/> // create an indexwriter object, the first parameter is directory, the second parameter is the word divider, and the third parameter indicates whether it is created. If it is false, it is modified on this basis. The fourth parameter indicates the maximum value of word segmentation, for example, new <br/> // maxfieldlength (2) indicates one point of two words. Generally, indexwriter is used. maxfieldlength. limited <br/> indexwriter = new indexwriter (Dir, new standardanalyzer (<br/> version. required e_30), true, indexwriter. maxfieldlength. unlimited); <br/> file [] files = new file (datedir ). listfiles (); <br/> for (INT I = 0; I <files. length; I ++) {<br/> document DOC = new document (); <br/> // create a field object and put it in the doc object <br/> Doc. add (new field ("contents", new filereader (files [I]); <br/> Doc. add (new field ("FILENAME", files [I]. getname (), field. store. yes, <br/> field. index. not_analyzed); <br/> Doc. add (new field ("indexdate", datemedils. datetostring (new date (), <br/> datatels. resolution. day), field. store. yes, <br/> field. index. not_analyzed); <br/> // write indexwriter <br/> indexwriter. adddocument (DOC); <br/>}< br/> // view the number of indexes in indexwriter <br/> system. out. println ("numdocs/t" + indexwriter. numdocs (); <br/> indexwriter. close (); <br/>}< br/>}
2. index files
Public class seacher {<br/> Public static void main (string [] ARGs) throws ioexception, parseexception {<br/> // The place where the index file is saved <br/> string indexdir = "F: // indexdir "; <br/> directory dir = new simplefsdirectory (new file (indexdir); <br/> // create an indexsearcher object. Compared with the indexwriter object, this parameter requires an index directory. <br/> indexsearcher indexsearch = new indexsearcher (DIR); <br/> // create a queryparser object, the first parameter indicates the Lucene version, the second parameter indicates the field to be searched for, and the third parameter indicates the use of the word divider for search <br/> queryparser = new queryparser (version. inclue_30, <br/> "contents", new standardanalyzer (version. required e_30); <br/> // generate a query object <br/> query = queryparser. parse ("pause"); <br/> // the search result topdocs contains the scoredocs [] array, which stores the index value <br/> topdocs hits = indexsearch. search (query, 3); <br/> // hits. totalhits indicates the total number of records found <br/> system. out. println ("found" + hits. totalhits + ""); <br/> // cycle the hits.scoredocsdata, use the indexsearch.doc method to restore the document, and then extract the value of the corresponding field <br/> for (INT I = 0; I <pits. scoredocs. length; I ++) {<br/> scoredoc SDoC = hits. scoredocs [I]; <br/> document DOC = indexsearch.doc(sdoc.doc); <br/> system. out. println (Doc. get ("FILENAME") + "/t" <br/> + hits. scoredocs [I]. score); <br/>}< br/> indexsearch. close (); <br/>}< br/>