/* Before learning how to create an index is IndexWriter, which is used to read the created
* Index method Indexreader.
* and his sub-class
* Multireader
* */
Package indexreader;
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.MultiReader;
Import Org.apache.lucene.index.Term;
Import Org.apache.lucene.index.TermDocs;
public class indexreader{
Private String Index_store_path = "E:\\lucene project \ \ target file";
IndexWriter writer = null;
Org.apache.lucene.index.IndexReader reader = null;
Public Indexreader () {
try{
Create an index
writer = new IndexWriter (Index_store_path, New StandardAnalyzer (), true);
Writer.setusecompoundfile (FALSE);
Document Doc1 = new document ();
Document DOC2 = new document ();
Document DOC3 = new document ();
Field f1 = new Field ("BookName", "How Steel is Tempered", Field.Store.YES, Field.Index.TOKENIZED);
Field F2 = new Field ("BookName", "Children of Heroes", Field.Store.YES, Field.Index.TOKENIZED);
Field F3 = new Field ("BookName", "Fence Woman and Dog", Field.Store.YES, Field.Index.TOKENIZED);
Doc1.add (F1);
Doc2.add (F2);
Doc3.add (F3);
Writer.adddocument (Doc1);
Writer.adddocument (DOC2);
Writer.adddocument (DOC3);
Writer.close ();
Reading an index using Indexreader
Reader = Multireader.open (Index_store_path);
SYSTEM.OUT.PRINTLN ("Index document list:");
for (int i = 0; i < Reader.numdocs (); i++) {
System.out.println (Reader.document (i));
}
Output version information for the current index
SYSTEM.OUT.PRINTLN ("indexed version:" + reader.getversion ());
Output the current number of index files
System.out.println ("Number of text indexed:" + reader.numdocs ());
Constructs an entry and finds it in the index
System.out.println ("============================");
System.out.println ("Find entry female----------> Start search");
Term Term1 = new term ("BookName", "female");
Termdocs docs = Reader.termdocs (TERM1);
while (Docs.next ()) {
System.out.println ("---------------------in Search");
SYSTEM.OUT.PRINTLN ("The document with lookup < + Term1 +" > is numbered "+ Docs.doc ()");
System.out.println ("Number of term occurrences in document" + Docs.freq ());
System.out.println ("----------------------------");
}
Reader.close ();
}catch (IOException e) {
E.printstacktrace ();
}
}
public static void Main (string[] args) {
TODO auto-generated Method Stub
Indexreader IR = new Indexreader ();
}
}
Read the index file information using Lucene's Indexreader