GroupDocs.Search supports indexing of many document formats. In addition, you can implement support for any other format, or replace the built-in extraction for a supported format, with a custom text extractor.
A custom text extractor is a class implementing the IFieldExtractor interface. The Extensions property returns the file extensions the extractor is used for, and the two GetFields methods extract the fields of a document from a file or from a stream. The method that takes a stream is used for documents added from streams.
The following example demonstrates how to implement a custom text extractor.
C#
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){// The stream belongs to the caller, so it is not closed hereusing(StreamReaderreader=newStreamReader(stream,Encoding.UTF8,true,4096,true)){DocumentField[]fields=newDocumentField[]{newDocumentField("Content",reader.ReadToEnd()),};returnfields;}}privatestringExtractContent(stringfilePath){stringtext=File.ReadAllText(filePath);returntext;}}
To use a custom extractor, add it to the CustomExtractors collection of the index settings. A custom extractor takes precedence over the built-in extractor for the same extension. Custom extractors are not saved in an index, so they must be added each time the index is created or opened. The next example shows how to use the custom extractor for indexing.
C#
stringindexFolder=@"c:\MyIndex\";// Specify path to the index folderstringdocumentsFolder=@"c:\MyDocuments\";// Specify path to a folder containing documents to searchIndexSettingssettings=newIndexSettings();settings.CustomExtractors.Add(newLogExtractor());// Adding custom text extractor to the index settingsIndexindex=newIndex(indexFolder,settings);// Creating or loading an indexindex.Add(documentsFolder);// Indexing documents from the specified folder
To index a container document (for example, an archive) whose items are indexed as separate items of the document, implement the IContainerItemExtractor interface. In addition to the members of IFieldExtractor, this interface contains:
The FormatFamily property, which returns the format family of the container document.
Two GetContainerItems methods, which extract the items of a container document from a file or from a stream and return them as an array of ExtractedItemInfo objects.
Each ExtractedItemInfo object describes one item: its FormatFamily, the extracted Fields, the ItemInfo string that identifies the item in the container, and the InnerPath of the item. Items with an empty ItemInfo value are not indexed. The fields returned by GetFields are indexed as the fields of the container document itself. The indexed items of a container document can be obtained with the GetIndexedDocumentItems method.
The following example demonstrates a custom extractor that indexes each line of a text file as a separate item.
C#
publicclassLineContainerExtractor:IContainerItemExtractor{privatereadonlystring[]extensions=newstring[]{".lines"};publicstring[]Extensions{get{returnextensions;}}publicFormatFamilyFormatFamily{get{returnFormatFamily.Archive;}}publicDocumentField[]GetFields(stringfilePath){// The container document itself has only the file name fieldreturnnewDocumentField[]{newDocumentField("FileName",Path.GetFileName(filePath))};}publicDocumentField[]GetFields(Streamstream){returnnewDocumentField[0];}publicExtractedItemInfo[]GetContainerItems(stringfilePath){returnCreateItems(File.ReadAllLines(filePath));}publicExtractedItemInfo[]GetContainerItems(Streamstream){List<string>lines=newList<string>();using(StreamReaderreader=newStreamReader(stream,Encoding.UTF8,true,4096,true)){stringline;while((line=reader.ReadLine())!=null){lines.Add(line);}}returnCreateItems(lines.ToArray());}privatestaticExtractedItemInfo[]CreateItems(string[]lines){ExtractedItemInfo[]items=newExtractedItemInfo[lines.Length];for(inti=0;i<lines.Length;i++){DocumentField[]fields=newDocumentField[]{newDocumentField("Content",lines[i])};stringitemInfo="Line "+(i+1).ToString(CultureInfo.InvariantCulture);items[i]=newExtractedItemInfo(FormatFamily.WordProcessing,fields,itemInfo,string.Empty);}returnitems;}}
More resources
GitHub examples
You may easily run the code from documentation articles and see the features in action in our GitHub examples: