1using System;
2using System.IO;
3using Lucene.Net.Documents;
4using Lucene.Net.Util;
5
6namespace LuceneEngine.MakeFileDoc
7{
8	public class FileDocument 
9	{
10		private FileDocument() {}
11
12		public static Document Document(FileInfo f)
13		{
14			//Make a new, empty document
15			Document doc = new Document();
16
17			//Add the path of the file as a field named "path".  Use a Text field, 
18			//so that the index stores the path, and so that the path is searchable
19			doc.Add(Field.Text("filename", f.Name));
20			doc.Add(Field.Text("path", f.FullName));
21
22			//Add the filename as a field named "name" and file length as 
23			//a filed name "length". Use a Keyword field, so that it's 
24			//searchable, but so that no attempt is made to tokenize the
25			//field into words.
26			doc.Add(Field.Keyword("name", f.Name));
27			doc.Add(Field.Keyword("length", f.Length.ToString()));
28
29			//Add the contents of the file a field named "contents".  Use a Text
30			//field, specifying a Reader, so that the text of the file is tokenized.
31			FileStream _is = new FileStream(f.FullName, FileMode.Open,FileAccess.Read);
32			TextReader reader = new StreamReader(_is);
33			doc.Add(Field.Text("contents", reader));
34
35			//Add the creation time, last write time and last modified date of 
36			//the file as fields named "creation_time", "last_write_time" and
37			//"last_access_time".  Use a Keyword field, so that it's 
38			//searchable, but so that no attempt is made to tokenize the
39			//field into words.
40			doc.Add(Field.Keyword("creation_time", DateField.TimeToString(Date.GetTime(f.CreationTime))));
41			doc.Add(Field.Keyword("last_write_time", DateField.TimeToString(Date.GetTime(f.LastWriteTime))));
42			doc.Add(Field.Keyword("last_access_time", DateField.TimeToString(Date.GetTime(f.LastAccessTime))));
43
44			//return the document
45			return doc;
46		}
47	}
48}