Study and summary of Lucene (II.)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.