I. The foundation of Lucene (2017-05-11-10:40:46)
1. Lucene download
API Download: Http://pan.baidu.com/s/1nvLTG0L
2, the use of Lucene
/** * Indexing * * public voidIndex () {IndexWriter writer = null; Try{///1, create directory//directory directory = new Ramdirectory ();//build in memory//build on hard disk Directory directory = Fsdirectory.open (new File ("d:/import/studytool/lucene/index01"))); 2. Creation of IndexWriter indexwriterconfig IWC = new Indexwriterconfig (version.lucene_35, newStandardAnalyzer (version.lucene_35)); writer = new indexwriter (directory, IWC);//3. Create Document Object Document DOC = null;//4. Add field File f = new to document File ("D:/import/studytool/lucene/example"), for(file File:f.listfiles ()) {doc = new Document (); Doc.add ( New Field ("Content", new FileReader (file)); Doc.add (new field ("FileName", File.getname (), field.store.yes,field.index.not_analyzed)), Doc.add (new Field (" Path ", File.getpath (), field.store.yes,field.index.not_analyzed)); 5. Add the document to the index by IndexWriter writer.adddocument (DOC);}} Catch (Corruptindexexception e) {e.printstacktrace ();} catch (Lockobtainfailedexception e) { E.printstacktrace (); } catch (IOException e) {e.printstacktrace ();}}
/** * Search * * * public voidSearcher () {try{///1, create directory directory directory = Fsdirectory.open ("d:/import/studytool/lucene/index0 1 ")); 2. Create Indexreader Indexreader reader =Indexreader.open (directory); 3, according to Indexreader create indexsearcher indexsearcher searcher = newIndexsearcher (reader); 4. Create a search query//Create parser to determine what to search for the file, the second parameter indicates the domain searched Queryparser parser = new Queryparser (version.lucene _35, "Content", New StandardAnalyzer (version.lucene_35)); Create a query that represents a document that contains come in the search domain as content, query query = Parser.parse ("Come");//5, search by Seacher and return topdocs topdocs TDS = Searcher.search (query);//6, Topdocs gets Scoredoc object scoredoc[] SDS = tds.scoredocs; for(Scoredoc Sd:sds) {//7, obtaining specific document objects based on Seacher and Scorddoc objects Document D = Searcher.doc (sd.doc);//8, getting the required values based on the Document object System.out.println (D.get ("filename") + "[" +d.get ("path") + "]"),//9, close reader reader.close ();} Catch ( Corruptindexexception e) {e.printstacktrace (),} catch (IOException e) {e.printstacktrace ();} catch (Parseexc Eption e) {e.printstacktrace ();}}
3. Basic example 4, System architecture
Study and summary of Lucene (ii)