Working with Lucene.Net - Adding data to a document
(Page 4 of 5 )
Once you’ve created a Document, you’ll need to add data to it. This is done by creating one or more Fields for each piece of metadata in your file. For example, in the sample application, we created aFieldcalledpaththat holds the path to the file we are indexing, aFieldcalledmodifiedthat holds the date the file was last modified, and aFieldcalledcontentsthat holds the document’s raw text content. You can create moreFields as your application requires. When you create aField, you can also specify what type ofFieldit is.
The threeFields in our sample application are added to aDocumentlike so:
doc.Add(new Field("path", f.FullName, Field.Store.YES,
Field.Index.UN_TOKENIZED));
doc.Add(new Field("modified",
DateTools.TimeToString(f.LastWriteTime.Ticks,
DateTools.Resolution.MINUTE), Field.Store.YES,
Field.Index.UN_TOKENIZED));
doc.Add(new Field("contents", new System.IO.StreamReader(f.FullName,
System.Text.Encoding.Default)));
After you’ve populated aDocument object withFieldobjects, you’re ready to add theDocument to the index:
writer.AddDocument(IndexDocument(file));
Running the IndexFiles application. From the command line, run theIndexFilesapplication against the folder you have populated with raw text files. You can also simply pointIndexFilesto the Lucene.Net source directory, andIndexFileswill index the Lucene.Net source files for you. To startIndexFiles, issue the following command from the bin directory:IndexFiles C:\Lucene.Net\. OnceIndexFilesis done indexing your files, it creates a directory called index in the current directory and stores the index in it.
Next: Searching an index >>
More BrainDump Articles
More By O'Reilly Media
|
This article is excerpted from chapter four of the book Windows Developer Power Tools, written by James Avery and Jim Holmes (O'Reilly; ISBN: 0596527543). Check it out today at your favorite bookstore. Buy this book now.
|
|