LUCENE-01: Creating an Index

Source: Internet
Author: User
Tags createindex

We built a folder under the D-Disk called Lucene,lucene two more folders, a call example, a call index01. Example folder under three TXT files, a.txt content for Hello java,b.txt content for hello lucene,c.txt content for Hello Hadoop.

 Packagecom.amazing;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.IndexWriter;ImportOrg.apache.lucene.index.IndexWriterConfig;Importorg.apache.lucene.store.Directory;Importorg.apache.lucene.store.FSDirectory;Importorg.apache.lucene.util.Version; Public classHelloLucene { Public voidCreateIndex () {IndexWriter writer=NULL; Try{Directory directory= Fsdirectory.open (NewFile ("D:" +file.separator+ "Lucene" +file.separator+ "index01")); Indexwriterconfig IWC=NewIndexwriterconfig (version.lucene_35,NewStandardAnalyzer (version.lucene_35)); Writer=NewIndexWriter (DIRECTORY,IWC); Document Doc=NULL; File F=NewFile ("D:" +file.separator+ "Lucene" +file.separator+ "Example");  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));            Writer.adddocument (DOC); }        } Catch(IOException e) {e.printstacktrace (); } finally{            if(Writer! =NULL){                Try{writer.close (); } Catch(corruptindexexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }            }        }            }}

To run the test class:

 Package com.amazing; Import org.junit.Test;  Public class testlucene {    @Test    publicvoid  Testcreateindex () {        new  HelloLucene ();        Hl.createindex ();    }}

Some files appear under folder index01:

LUCENE-01: Creating an Index

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.