By writing a connection example, you can get a general idea of Lucene's core classes and main API functions.
package Demo;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStreamReader;import java.util.Date;import org.apache.lucene.analysis.Analyzer;import org.apache.lucene.analysis.standard.StandardAnalyzer;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.document.LongField;import org.apache.lucene.document.StringField;import org.apache.lucene.document.TextField;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.IndexWriterConfig;import org.apache.lucene.index.IndexWriterConfig.OpenMode;import org.apache.lucene.index.Term;import org.apache.lucene.store.Directory;import org.apache.lucene.store.FSDirectory;import org.apache.lucene.util.Version;public class IndexFiles {private IndexFiles(){}/**Index all text files under a directory **//** * @param args */public static void main(String[] args) {// TODO Auto-generated method stub String usage = "java org.apache.lucene.demo.IndexFiles" + " [-index INDEX_PATH] [-docs DOCS_PATH] [-update]\n\n" + "This indexes the documents in DOCS_PATH, creating a Lucene index" + "in INDEX_PATH that can be searched with SearchFiles"; String indexPath = "index"; String docsPath = args[0]; boolean create = true; if(docsPath==null) { System.err.println("input the docsPath"); System.exit(1); } final File docDir = new File(docsPath); if(!docDir.exists()||!docDir.canRead()) { System.out.println("Document directory '" +docDir.getAbsolutePath()+ "' does not exist or is not readable, please check the path"); System.exit(1); } Date start = new Date(); try{// System.out.println("Indexing to directory '") Directory dir =FSDirectory.open(new File(indexPath)); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40); IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_40, analyzer); if(create) { iwc.setOpenMode(OpenMode.CREATE); } else { iwc.setOpenMode(OpenMode.CREATE_OR_APPEND); } IndexWriter writer = new IndexWriter(dir, iwc); indexDocs(writer, docDir); writer.close(); Date end = new Date(); System.out.println(end.getTime()-start.getTime() + "total milliseconds"); }catch(IOException e) { System.out.println("caught a "+ e.getClass()+ "\n with message:" + e.getMessage()); } }/** * do not try to index files that cannot be read * @throws IOException */static void indexDocs(IndexWriter writer,File file) throws IOException{if(file.canRead()){ if(file.isDirectory()) { String[] files = file.list(); if(files != null){ for(int i = 0;i < files.length;i++) { indexDocs(writer,new File(file,files[i])); } } } else{ FileInputStream fis; fis = new FileInputStream(file); try{ //make a new ,empty document Document doc = new Document(); Field pathField = new StringField("path", file.getPath(), Field.Store.YES); doc.add(pathField); doc.add(new LongField("modified", file.lastModified(), Field.Store.NO)); doc.add(new TextField("contents", new BufferedReader(new InputStreamReader(fis,"UTF-8")))); if(writer.getConfig().getOpenMode()==OpenMode.CREATE) { //new index so we just add the document (no old document can be there) System.out.println("adding " + file); writer.addDocument(doc); } else { //Existing index (an old copy of the document may have been indexed) // so we use updataDocument instead to replace the old one matching //the exact path,if present System.out.println("updating " + file); writer.updateDocument(new Term("path", file.getPath()), doc); } }finally{ fis.close(); } }} }}
Package demo; import Java. io. bufferedreader; import Java. io. file; import Java. io. fileinputstream; import Java. io. ioexception; import Java. io. inputstreamreader; import Java. NIO. buffer; import Java. util. date; import Org. apache. lucene. analysis. analyzer; import Org. apache. lucene. analysis. standard. standardanalyzer; import org.apache.e.doc ument. document; import Org. apache. lucene. index. directoryreader; import or G. apache. lucene. index. indexreader; import Org. apache. lucene. queryparser. classic. parseexception; 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. fsdirectory; import Org. apache. lucene. util. version; imp ORT Org. XML. sax. inputsource; public class searchfiles {/*** @ Param ARGs * @ throws ioexception * @ throws parseexception */public static void main (string [] ARGs) throws ioexception, parseexception {// todo auto-generated method stubstring Index = "Index"; string field = "contents"; string queries = NULL; int repeat = 0; Boolean raw = false; string querystring = NULL; int hitsperpage = 10; // open the inde folder where the index is located. Xreader reader = directoryreader. open (fsdirectory. open (new file (INDEX); // retrieves indexsearcher searcher = new indexsearcher (Reader) based on the index file opened by indexreader; analyzer = new standardanalyzer (version. required e_40); bufferedreader in = NULL; If (queries! = NULL) {In = new bufferedreader (New inputstreamreader (New fileinputstream (queries), "UTF-8");} else {In = new bufferedreader (New inputstreamreader (system. in, "UTF-8");} queryparser parser = new queryparser (version. required e_40, field, analyzer); While (true) {If (queries = NULL & querystring = NULL) {system. out. println ("Enter query:");} string line = querystring! = NULL? Querystring: In. readline (); If (line = NULL | line. length () =-1) break; line = line. trim (); If (line. length () = 0) break; query = parser. parse (line); system. out. println ("Searching for:" + query. tostring (field); If (Repeat> 0) // repeat & time as benchmark {date start = new date (); For (INT I = 0; I <repeat; I ++) searcher. search (query, null, 100); date end = new date (); system. out. println ("Time: +" + (end. g Ettime ()-start. gettime () + "ms. ");} dopagingsearch (in, searcher, query, hitsperpage, raw, queries = NULL & querystring = NULL); If (querystring = NULL) break ;}} public static void dopagingsearch (bufferedreader in, indexsearcher searcher, query, int hitsperpage, Boolean raw, Boolean interactive) throws ioexception {// collect enough docs to show 5 pagestopdocs Results = searcher. search (query, 5 * hitsperpage); scoredoc [] hits = results. scoredocs; int numtotalhits = results. totalhits; system. out. println (numtotalhits + "Total matching documents"); int start = 0; int end = math. min (numtotalhits, hitsperpage); While (true) {If (end> hits. length) {system. out. println ("only results 1-" + hits. length + "of" + numtotalhits + "Total matching events collected. "); system. out. println ("collect more (y/n )? "); String line = in. readline (); If (line. length () = 0 | line. charat (0) = 'n') {break;} Hits = searcher. search (query, numtotalhits ). scoredocs;} End = math. min (hits. length, start + hitsperpage); For (INT I = start; I <end; I ++) {If (raw) // output raw format {system. out. println ("Doc =" Your hitspolici2.16.doc + "score =" + hits [I]. score); continue;} document DOC = searcher.doc(hits? I =.doc); string Path = Doc. get ("path"); If (path! = NULL) {system. Out. println (I + 1) + "." + path); String title = Doc. Get ("title"); If (title! = NULL) {system. out. println ("title:" + Doc. get ("title") ;}} else {system. out. println (I + 1) + ". "+" no path for this document ") ;}} if (! Interactive | End = 0) {break;} If (numtotalhits> = END) {Boolean quit = false; while (true) {system. out. print ("Press"); If (START-hitsperpage> = 0) {system. out. print ("<p> revious page,");} If (start + hitsperpage <numtotalhits) {system. out. print ("(n) ext page,");} system. out. print ("(q) uit or enter number to jump to a page. "); string line = in. readline (); If (line. length () = 0 | line. charat (0) = 'q') {quit = true; B Reak;} If (line. charat (0) = 'P') {start = math. max (0, start-hitsperpage); break;} else if (line. charat (0) = 'n') {If (start + hitsperpage <numtotalhits) Start + = hitsperpage; break;} else {int page = integer. parseint (line); If (page-1) * hitsperpage <numtotalhits) {start = (page-1) * hitsperpage; break;} else {system. out. println ("No such page! ") ;}} If (quit) break; end = math. Min (numtotalhits, start + hitsperpage );}}}}