first, the name of the experiment: Build Index
Second, Experiment Date:2013/9/21
Third, Experiment Objective:
1) The data modeling process that can understand the document-field structure in Lucene;
2) can be compiled for specific data generation index file.
Four , instruments and materials for laboratory use :
MyEclipse 10,JDK
Five , the steps and methods of the experiment :
Title one: Generate an index of 3 books in the specified directory, requiring the establishment of 3 document to store the title data separately. Truncate the generated index file (composite index and generic index are generated once).
Figure 1: The general index
Figure 2: The composite Index
Title Two: Modify the code of the title one, using a multi-domain in a document to store 3 book title value.
Topic Three: Three documents for topic one, do the following: Delete a value from the index, modify the field value of a document, according to the title.
Experimental process:
Topic One source code:
Package Lab02;import Java.io.file;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;import org.apache.lucene.index.Term; Import Org.apache.lucene.store.directory;import Org.apache.lucene.store.fsdirectory;import Org.apache.lucene.util.version;public class Greatindex {public static void main (string[] args) {Greatindex Greateindexobj=new Greatindex (); try {greateindexobj.setup ();} catch (Exception e) {//Todo:handle EXCEPTIONE.PRINTST Acktrace ();}} Private String indexdir= "E:/users/administrator/workspaces/myeclipse 10/mylucene/src/lab02/index";p rivate Directory directory; Indicates that the index holds the directory public void SetUp () throws Exception {//directory =new ramdirectory ();//index is stored in RAM in memory directory = Fsdirectory.open ((New File (Indexdir))); The index is stored in the file system of the physical hard disk (that is, the specified path is stored) IndexWriter writer=new indexwriter (directory,new standardanalyzer (version.lucene_30), true,indexwriter.maxfieldlength.unlimited);//write.setusecompoundfile (false);//Set False to use a generic index (with multiple files)// Creation of 3 Books of Documentdocument doc1=new document ();D ocument doc2=new document ();D ocument doc3=new document ();//build name called " BookName "field and add field values to the document, set the Country field value to be stored in the index, not by word breaker with weighted doc1.add (" BookName "," clear ", field.store.yes,field.index.not_ analyzed_no_norms));d Oc2.add (New Field ("BookName", "Arcane Shrine", field.store.yes,field.index.not_analyzed_no_norms)); Doc1.add (New Field ("BookName", "Song of Ice and Fire", Field.store.yes,field.index.not_analyzed_no_norms)); Writer.adddocument ( Doc1); writer.adddocument (DOC2); writer.adddocument (DOC3); Writer.close (); }}
Topic Two source code:
Package Lab02;import Java.io.file;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;import org.apache.lucene.index.Term; Import Org.apache.lucene.store.directory;import Org.apache.lucene.store.fsdirectory;import Org.apache.lucene.util.version;public class Greatindex {public static void main (string[] args) {Greatindex Greateindexobj=new Greatindex (); try {//greateindexobj.setup (); Greateindexobj.setup2 ();} catch (Exception e) {//Todo:handle exceptione.printstacktrace ();}} Private String indexdir= "E:/users/administrator/workspaces/myeclipse 10/mylucene/src/lab02/index";p rivate Directory directory; Directory for index storage private string[] booknames={"clear", "Arcane", "Song of Ice and Fire"};p ublic void SetUp2 () throws Exception{//directory =new Ramdirectory (); The index is stored in RAM in memory directory =fsdirectory.open ((new File (Indexdir))); The index is stored within the file system of the physical hard disk (that is, the specified path) Indexwriter writer=new IndexWriter (directory,new standardanalyzer (version.lucene_30), True, IndexWriter.MaxFieldLength.UNLIMITED);//writer.setusecompoundfile (false);//Set False to use a generic index (with multiple files)// Create a documentdocument doc=new Document () with three domain values, a for (String bookname:booknames) {Doc.add (new field ("BookName", BookName, field.store.yes,field.index.not_analyzed_no_norms));} Writer.adddocument (DOC); Writer.close ();}}
Topic Three source code:
Topic three public void Deletedocument () throws Ioexception{indexwriter writer=new IndexWriter (directory,new StandardAnalyzer (version.lucene_30), true,indexwriter.maxfieldlength.unlimited); Writer.optimize ();// Use optimization policy to delete documents (delete directly, cannot reply) writer.deletedocuments (new Trem ("BookName", "felling")); Writer.close (); public void UpdateDocument () throws Ioexception{indexwriter writer=new IndexWriter (directory,new standardanalyzer ( version.lucene_30), true,indexwriter.maxfieldlength.unlimited);//Build a new document with and replace document Doc=new document (); Doc.add (New Field ("BookName", "Lucene Combat second Edition", Field.store.yes,field.index.not_analyzed_no_norms)); Writer.updatedocument (New term ("BookName", "Guan Xian"), doc); Writer.close ();}
Six, data logging and calculation :
Structure diagram of the project:
Seven, the experiment Results or conclusion :
Summary: Through this experiment, I basically understand the document-field structure of lucene data modeling process, can be compiled for specific data generation index file. In this experiment, the experiment is not very smooth, this experiment let me feel the strong lucene, Increase my interest in Lucene!
Viii. remarks or instructions :
Nine , reference Reference Documents :
http://lucene.apache.org
"Lucene Experiment 1" Build index