FileDocCategorySizeDatePackage
FileDocument.javaAPI DocApache Lucene 1.4.32565Tue Mar 30 00:48:00 BST 2004org.apache.lucene.demo

FileDocument

public class FileDocument extends Object
A utility for making Lucene Documents from a File.

Fields Summary
Constructors Summary
private FileDocument()

Methods Summary
public static org.apache.lucene.document.DocumentDocument(java.io.File f)
Makes a document for a File.

The document has three fields:

  • path--containing the pathname of the file, as a stored, tokenized field;
  • modified--containing the last modified date of the file as a keyword field as encoded by DateField; and
  • contents--containing the full contents of the file, as a Reader field;

    	 
        // make a new, empty document
        Document doc = new Document();
    
        // Add the path of the file as a field named "path".  Use a Text field, so
        // that the index stores the path, and so that the path is searchable
        doc.add(Field.Text("path", f.getPath()));
    
        // Add the last modified date of the file a field named "modified".  Use a
        // Keyword field, so that it's searchable, but so that no attempt is made
        // to tokenize the field into words.
        doc.add(Field.Keyword("modified",
    			  DateField.timeToString(f.lastModified())));
    
        // Add the contents of the file a field named "contents".  Use a Text
        // field, specifying a Reader, so that the text of the file is tokenized.
        // ?? why doesn't FileReader work here ??
        FileInputStream is = new FileInputStream(f);
        Reader reader = new BufferedReader(new InputStreamReader(is));
        doc.add(Field.Text("contents", reader));
    
        // return the document
        return doc;