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. Since version 26.11, a document can also be added from a stream that cannot seek, and the method then gets a stream that cannot seek: an extractor that seeks in the stream needs a document from a stream that can seek.
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
Since version 26.11, the Add method of the collection throws ArgumentNullException for an extractor that is null, and the collection is not changed. Before, such an extractor was added, and every indexing of the index failed.
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. The method that takes a stream gets the stream that GetFields has got before. Since version 26.11, a stream that can seek is set back to the position where GetFields got it; a stream that cannot seek is where GetFields has left it. Before, every stream was where GetFields had left it, usually at its end.
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: