/*
* Create an index and then add the created document to the index
* Tip Use document and field to add field to document first.
* Then build the document into an index
* */
Package comthree;
Import java.io.IOException;
Import Org.apache.lucene.analysis.standard.StandardAnalyzer;
Import org.apache.lucene.document.Document;
Import Org.apache.lucene.document.Field;
Import Org.apache.lucene.index.IndexWriter;
public class bookindex{
Create an indexed directory
Private String Index_path = "E:\\lucene project \ \ Index Directory";
Document bookdoc1 = null;
Public Bookindex () {
Bookdoc1 = new Document ();
System.out.println ("Start creating field fields for book---------->>>>");
The number of the book
Field bookno = new Field ("Booknumber", "FB309663004", Field.Store.YES, Field.Index.TOKENIZED);
Title
Field bookname = new Field ("BookName", "How Steel is Tempered", Field.Store.YES, Field.Index.TOKENIZED);
The author of the book
Field anthor = new Field ("Author", "Anonymous", Field.Store.YES, Field.Index.TOKENIZED);
Publication date
Field publishdate = new Field ("Publishdate", "Publication date", Field.Store.YES, Field.Index.NO);
Describe
Field bookabstract = new Field ("Abstract", "How Steel is Tempered", Field.Store.NO, Field.Index.TOKENIZED);
Price
Field price = new Field ("Price", "25.00", Field.Store.YES, Field.Index.NO);
System.out.println ("Field---------->>>>" to complete the creation of the book);
System.out.println ("Start the field of the book into the document-------------");
Add field to document
Bookdoc1.add (Bookno);
Bookdoc1.add (BookName);
Bookdoc1.add (bookabstract);
Bookdoc1.add (publishdate);
Bookdoc1.add (price);
System.out.println ("Complete the field of the book into the document------------->>>>>");
try{
System.out.println ("Start creating index-------------------->>>>");
Creating an Index instance
IndexWriter writer = new IndexWriter (Index_path, New StandardAnalyzer ());
Building an instance of a IndexWriter
Writer.adddocument (BOOKDOC1);
It must be closed here, otherwise an exception will occur, such as the data is not written intact.
Writer.close ();
SYSTEM.OUT.PRINTLN ("Completion of the creation of the index and storage, the location of the index in:" + Index_path);
}catch (IOException e) {
E.printstacktrace ();
}
}
public static void Main (string[] args) {
Bookindex BI = new Bookindex ();
}
}
Indexing with Lucene's IndexWriter (detailed)