Let's take a look at Lucene in action as an example of getting started.
Before using Lucene for text content search, you must index the files in the specified directory. The Code is as follows:
Import Java. io. file; import Java. io. filefilter; import Java. io. filereader; import Java. io. ioexception; import Org. apache. lucene. analysis. standard. standardanalyzer; import org.apache.e.doc ument. document; import org.apache.e.doc ument. field; import Org. apache. lucene. index. indexwriter; import Org. apache. lucene. index. indexwriterconfig; import Org. apache. lucene. store. directory; import Org. apache. lucene. s Tore. fsdirectory; import org. Apache. Lucene. util. version; public class indexer {public static void main (string [] ARGs) {If (ARGs. length! = 2) {Throw new illegalargumentexception ("Usage: Java" + indexer. class. getname () + "<index dir> <data dir>");} string indexdir = ARGs [0]; string datadir = ARGs [1]; long start = system. currenttimemillis (); indexer = NULL; try {indexer = new indexer (indexdir);} catch (ioexception e) {e. printstacktrace ();} int numindexed = 0; try {numindexed = indexer. index (datadir, new textfilesfilter ();} catch (Exception e) {e. printstacktrace ();} finally {try {indexer. close ();} catch (ioexception e) {e. printstacktrace () ;}long end = system. currenttimemillis (); system. out. println ("Indexing" + numindexed + "files took" + (end-Start) + "mi");} private indexwriter writer; Public indexer (string indexdir) throws ioexception {directory dir = fsdirectory. open (new file (indexdir); writer = new indexwriter (Dir, n EW indexwriterconfig (version. inclue_30, new standardanalyzer (version. required e_30);} public void close () throws ioexception {writer. close ();} public int index (string datadir, filefilter filter) throws exception {file [] files = new file (datadir ). listfiles (); For (File file: Files) {If (! File. isdirectory ()&&! File. ishidden () & file. exists () & file. canread () & (filter = NULL | filter. accept (File) {indexfile (File) ;}} return writer. numdocs ();} Private Static class textfilesfilter implements filefilter {@ overridepublic Boolean accept (file path) {return path. getname (). tolowercase (). endswith (". TXT ") ;}}/*** declares the query fields of the three indexes, one contents, one filename, A fullpath * @ Param f * @ return * @ throws exception */protected document getdocument (file F) throws exception {document DOC = new document (); Doc. add (new field ("contents", new filereader (F); Doc. add (new field ("FILENAME", F. getname (), field. store. yes, field. index. not_analyzed); Doc. add (new field ("fullpath", F. getCanonicalPath (), field. store. yes, field. index. not_analyzed); Return Doc;} private void indexfile (file F) throws exception {system. out. println ("Indexing" + F. getCanonicalPath (); document DOC = getdocument (f); writer. adddocument (DOC );}}
The file generated in the input directory after the command is executed: 650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/46/C2/wKioL1P0TH-AvMgZAAD4q83vAHc299.jpg "Title =" .png "alt =" wKioL1P0TH-AvMgZAAD4q83vAHc299.jpg "/>
After the index is generated, you can perform a basic search. The search code is as follows:
Import Java. io. file; import Java. io. ioexception; import Java. util. arrays; import org.apache.w.e.doc ument. document; import Org. apache. lucene. index. directoryreader; import Org. apache. lucene. index. term; import Org. apache. lucene. queryparser. classic. parseexception; import Org. apache. lucene. search. indexsearcher; import Org. apache. lucene. search. query; import Org. apache. lucene. search. scoredoc; import Org. apache. l Ucene. search. termquery; import Org. apache. lucene. search. topdocs; import Org. apache. lucene. store. directory; import Org. apache. lucene. store. fsdirectory; public class searcher {public static void main (string [] ARGs) throws ioexception, parseexception {If (ARGs. length! = 2) {Throw new illegalargumentexception ("Usage: Java" + searcher. class. getname () + "<index dir> <query>");} string indexdir = ARGs [0]; string q = ARGs [1]; search (indexdir, q );} private Static void search (string indexdir, string q) throws ioexception, parseexception {directory dir = fsdirectory. open (new file (indexdir); directoryreader reader = directoryreader. open (DIR); indexsearcher is = new indexsearcher (Reader); // each term corresponds to a field query = new termquery (new term ("contents", q )); long start = system. currenttimemillis (); topdocs hits = is. search (query, 10); long end = system. currenttimemillis (); system. err. println ("found" + hits. totalhits + "document (s) (in" + (end-Start) + "mi) that matched query '" + q + "':"); For (scoredoc: hits. scoredocs) {document DOC = is.doc(scoredoc.doc); system. out. println (Doc. get ("fullpath"); system. out. println (arrays. tostring (Doc. getvalues ("FILENAME"); system. out. println (Doc. get ("contents");} query Qu = new termquery (new term ("FILENAME", "1.txt"); topdocs hits1 = is. search (qu, 10); For (scoredoc score: hits1.scoredocs) using system.out.println(is.doc(score.doc ). get ("fullpath");} reader. close (); Dir. close ();}}
Run the following command to search for "hi" Words in the directory:
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/46/C3/wKioL1P0TYPyRhDJAACbSm7uX30193.jpg "Title =" .png "alt =" wkiol1p0typyrhdjaacbsm7ux30193.jpg "/>
This article from the "cainiao learning" blog, please be sure to keep this source http://biyusheng.blog.51cto.com/2626140/1542547