Lucene Document Example




import java.io.File;
import java.io.Reader;
import java.io.FileInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.DateField;


public class FileDocument {
  public static Document Document(File f) throws java.io.FileNotFoundException {
	 
    Document doc = new Document();

    doc.add(Field.Text("path", f.getPath()));

    doc.add(Field.Keyword("modified",
			  DateField.timeToString(f.lastModified())));

    FileInputStream is = new FileInputStream(f);
    Reader reader = new BufferedReader(new InputStreamReader(is));
    doc.add(Field.Text("contents", reader));

    return doc;
  }

  private FileDocument() {}
}