The example demonstrates how to implement the interface IFieldExtractor.
publicclassLogExtractor:IFieldExtractor{privatereadonlystring[]extensions=newstring[]{".log"};publicstring[]Extensions{get{returnextensions;}}publicDocumentField[]GetFields(stringfilePath){FileInfofileInfo=newFileInfo(filePath);DocumentField[]fields=newDocumentField[]{newDocumentField("FileName",fileInfo.FullName),newDocumentField("CreationDate",fileInfo.CreationTime.ToString(CultureInfo.InvariantCulture)),newDocumentField("Content",ExtractContent(filePath)),};returnfields;}publicDocumentField[]GetFields(Streamstream){thrownewNotSupportedException();// Extraction from a stream is not supported in this example}privatestringExtractContent(stringfilePath){StringBuilderresult=newStringBuilder();using(StreamReaderstreamReader=File.OpenText(filePath)){stringline;while((line=streamReader.ReadLine())!=null){stringprocessedLine=line.Remove(0,12);result.AppendLine(processedLine);}}returnresult.ToString();}}
The example demonstrates how to use the custom extractor for indexing.
stringindexFolder=@"c:\MyIndex\";// Specify path to the index folderstringdocumentsFolder=@"c:\MyDocuments\";// Specify path to a folder containing documents to searchIndexindex=newIndex(indexFolder);// Creating or loading an indexindex.IndexSettings.CustomExtractors.Add(newLogExtractor());// Adding custom text extractor to index settingsindex.Add(documentsFolder);// Indexing documents from the specified folder