Lucene Small Example

Source: Internet
Author: User

 Packageorg.itat.test;ImportJava.io.File;ImportJava.io.FileReader;Importjava.io.IOException;ImportOrg.apache.lucene.analysis.standard.StandardAnalyzer;Importorg.apache.lucene.document.Document;ImportOrg.apache.lucene.document.Field;Importorg.apache.lucene.index.CorruptIndexException;ImportOrg.apache.lucene.index.IndexReader;ImportOrg.apache.lucene.index.IndexWriter;ImportOrg.apache.lucene.index.IndexWriterConfig;Importorg.apache.lucene.queryParser.ParseException;ImportOrg.apache.lucene.queryParser.QueryParser;ImportOrg.apache.lucene.search.IndexSearcher;ImportOrg.apache.lucene.search.Query;ImportOrg.apache.lucene.search.ScoreDoc;ImportOrg.apache.lucene.search.TopDocs;Importorg.apache.lucene.store.Directory;Importorg.apache.lucene.store.FSDirectory;Importorg.apache.lucene.store.LockObtainFailedException;Importorg.apache.lucene.store.RAMDirectory;Importorg.apache.lucene.util.Version; Public classhellolucene{/*** Build Index *@throwsIOException *@throwscorruptindexexception*/     Public voidIndex ()throwscorruptindexexception, IOException {//1. Create a directoryDirectory directory =NewRamdirectory ();//built in-memory//Directory directory = fsdirectory.open (new File ("d:/lucene1/index01")); //2. Create IndexWriterIndexwriterconfig IWC =NewIndexwriterconfig (version.lucene_35,NewStandardAnalyzer (version.lucene_35)); IndexWriter writer=NULL; Try{writer=NewIndexWriter (directory, IWC); //3. Create a Document ObjectDocument doc =NULL; //4. Add field to documentFile f =NewFile ("D:/lucene");  for(File file:f.listfiles ()) {doc=NewDocument (); Doc.add (NewField ("Content",Newfilereader (file)); Doc.add (NewField ("filename", File.getname (), Field.Store.YES, Field.Index.NOT_ANALYZED)); Doc.add (NewField ("Path", File.getabsolutepath (), Field.Store.YES, Field.Index.NOT_ANALYZED)); //5. Adding documents to the index via IndexWriterWriter.adddocument (DOC); }                    }        Catch(corruptindexexception e) {//TODO Auto-generated catch blockE.printstacktrace (); }        Catch(lockobtainfailedexception e) {//TODO Auto-generated catch blockE.printstacktrace (); }        Catch(IOException e) {//TODO Auto-generated catch blockE.printstacktrace (); }        finally        {            if(Writer! =NULL) {writer.close (); }}} @SuppressWarnings ("Resource")     Public voidSearch ()throwsParseException {//1. Create a directoryDirectory directory;        Indexreader reader; Try{directory= Fsdirectory.open (NewFile ("d:/lucene1/index01")); //2. Create IndexreaderReader =indexreader.open (directory); //3. Create Indexsearcher according to Indexreader//4. Create a query for the searchQueryparser parser =NewQueryparser (version.lucene_35, "content",NewStandardAnalyzer (version.lucene_35)); //Create a parser to determine the contents of the file to search for, the second parameter represents the domain of the search//Create a query that indicates that the search domain is a document containing Java in contentQuery query = parser.parse ("com"); //5. Search by sea man and return to TopdocsTopdocs TDS =NewIndexsearcher (reader). Search (Query, 100); //6. Get Scoredoc objects based on Topdocsscoredoc[] SDS =Tds.scoredocs;  for(Scoredoc Sd:sds) {//7. Get specific document objects based on searcher and Scoredoc objectsDocument d =Newindexsearcher (reader). doc (Sd.doc); //8. Get the values you want based on the Document objectSystem.out.println (D.get ("filename") + "[" + D.get ("path") + "]"); }                                                           //9. Close ReaderReader.close (); }        Catch(IOException e) {//TODO Auto-generated catch blockE.printstacktrace (); }    }}

Test class:

 Packageorg.itat.test;Importjava.io.IOException;Importorg.apache.lucene.index.CorruptIndexException;Importorg.apache.lucene.queryParser.ParseException;Importorg.junit.Test; Public classtestlucene{@Test Public voidTestindex ()throwscorruptindexexception, IOException, parseexception {hellolucene hl=NewHelloLucene ();    Hl.index (); } @Test Public voidTestsearcher ()throwsparseexception {hellolucene hl=NewHelloLucene ();//Hl.index ();Hl.search (); }}


Lucene Small Example

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.