Indexing with Lucene's IndexWriter (detailed)

Source: Internet
Author: User

/*
* 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)

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.