GroupDocs.Search allows indexing documents from various sources:
From files in the file system.
From a stream.
From a data structure as an array of fields.
The library also allows indexing from all presented sources with lazy initialization: a document created with the Document.CreateLazy method is loaded by an IDocumentLoader implementation only when the index processes it. The LoadDocument method of the loader returns the loaded document, and the CloseDocument method is called to release its resources. The loaded document must have the source kind and the key of the lazy document; otherwise, or if LoadDocument returns null, the document fails with an error. Since version 26.11, CloseDocument is called in this case as well, and a lazy document of a file (DocumentSourceKind.File) is loaded when the file is indexed and closed after it; before, the loader of such a document was not called.
Please note that the update operation automatically generates a list of changed files only when indexing from the local file system. When indexing from streams or structures, documents cannot be updated with the update operation. To update documents from these sources, you must re-index the modified documents by passing their keys and updated data to the Add method.
When a folder is added to the index, the documents of all its subfolders are indexed. Since version 26.11, the links to folders inside an added folder, such as symbolic links and junctions, are not followed: to index the documents under such a link, add the folder that the link leads to. An added path that is a link itself is followed. The documents that an earlier version has indexed under such a link are deleted from the index by the next update.
Indexing from a file
It should be borne in mind that the Add method with the parameter of type Document[] allows indexing only documents individually, and not entire folders. The advantage of using this method overload is that you can add attributes and additional fields to the indexed document before calling the Add method. Since version 26.11, a document of a file is indexed with its Attributes and AdditionalFields; before, the file was indexed by its path only, and they were lost. The file is found by the key of the document, as a file added by its path: a file that does not exist or does not pass the document filter is not indexed, and a file passed more than once is indexed with its last document. An update indexes a changed file again by its path: the file keeps the attributes of its indexed version, but not the additional fields of its document; give them in the FileIndexing event if they must stay. The following example demonstrates how to index a document from a file.
C#
stringindexFolder=@"c:\MyIndex";stringdocumentFilePath=@"c:\MyDocuments\ExampleDocument.pdf";// Creating an indexIndexindex=newIndex(indexFolder);// Creating a document objectDocumentdocument=Document.CreateFromFile(documentFilePath);document.Attributes=newstring[]{"main"};// Setting the attributes of the documentdocument.AdditionalFields=newDocumentField[]// Setting the additional fields of the document{newDocumentField("Subject","Physics"),};Document[]documents=newDocument[]{document,};// Indexing document from the fileIndexingOptionsoptions=newIndexingOptions();index.Add(documents,options);
Indexing from a stream
Since version 26.11, a document can also be indexed from a stream that cannot seek, for example a network stream; for the extraction in a separate process, such a stream is read into memory first, up to 1 GB. The text and HTML documents of such streams are indexed, but the extraction of other formats may need a stream that can seek: copy such a stream into a MemoryStream first, as the examples of lazy-loaded documents below do.
The following example demonstrates how to index a document from a stream.
C#
stringindexFolder=@"c:\MyIndex";stringdocumentFilePath=@"c:\MyDocuments\ExampleDocument.pdf";// Creating an indexIndexindex=newIndex(indexFolder);// Creating a document objectStreamstream=File.OpenRead(documentFilePath);// Opening a streamDocumentdocument=Document.CreateFromStream(documentFilePath,DateTime.Now,".pdf",stream);Document[]documents=newDocument[]{document,};// Indexing document from the streamIndexingOptionsoptions=newIndexingOptions();index.Add(documents,options);// Closing the document stream after indexing is completestream.Close();
Indexing from a structure
The following example demonstrates how to index a document from a structure.
C#
stringindexFolder=@"c:\MyIndex";stringdocumentFilePath=@"c:\MyDocuments\ExampleDocument.txt";// Creating an indexIndexindex=newIndex(indexFolder);// Creating a document objectstringtext=File.ReadAllText(documentFilePath);DocumentField[]fields=newDocumentField[]{newDocumentField(CommonFieldNames.Content,text),};Documentdocument=Document.CreateFromStructure("ExampleDocument",DateTime.Now,fields);Document[]documents=newDocument[]{document,};// Indexing document from the structureIndexingOptionsoptions=newIndexingOptions();index.Add(documents,options);
Indexing from URL
The following example demonstrates how to index a document by URL when lazy initialized.
C#
publicclassDocumentLoaderFromUrl:IDocumentLoader{// A single HttpClient instance is reused for all requestsprivatestaticreadonlyHttpClienthttpClient=newHttpClient();privatereadonlystringdocumentKey;privatereadonlystringurl;privatereadonlystringextension;publicDocumentLoaderFromUrl(stringdocumentKey,stringurl,stringextension){this.documentKey=documentKey;this.url=url;this.extension=extension;}publicDocumentLoadDocument(){// HttpClient uses the security protocols allowed by the operating system (TLS 1.2 or later)// On .NET Framework, TLS 1.2 may have to be enabled explicitly:// ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;byte[]data=httpClient.GetByteArrayAsync(url).GetAwaiter().GetResult();MemoryStreammemoryStream=newMemoryStream(data);Documentdocument=Document.CreateFromStream(documentKey,DateTime.Now,extension,memoryStream);returndocument;}publicvoidCloseDocument(){}}stringindexFolder=@"c:\MyIndex";stringurl="https://example.com/ExampleDocument.pdf";// Creating an indexIndexindex=newIndex(indexFolder);// Creating a document objectstringdocumentKey=url;IDocumentLoaderdocumentLoader=newDocumentLoaderFromUrl(documentKey,url,".pdf");Documentdocument=Document.CreateLazy(DocumentSourceKind.Stream,documentKey,documentLoader);Document[]documents=newDocument[]{document,};// Indexing the lazy-loaded documentIndexingOptionsoptions=newIndexingOptions();index.Add(documents,options);
Indexing from FTP
The following example demonstrates how to index a document from FTP when lazy initialized.
C#
publicclassDocumentLoaderFromFtp:IDocumentLoader{privatereadonlystringdocumentKey;privatereadonlystringurl;privatereadonlystringextension;publicDocumentLoaderFromFtp(stringdocumentKey,stringurl,stringextension){this.documentKey=documentKey;this.url=url;this.extension=extension;}publicDocumentLoadDocument(){FtpWebRequestrequest=(FtpWebRequest)WebRequest.Create(url);using(WebResponseresponse=request.GetResponse())using(Streamstream=response.GetResponseStream()){MemoryStreammemoryStream=newMemoryStream();stream.CopyTo(memoryStream);memoryStream.Position=0;Documentdocument=Document.CreateFromStream(documentKey,DateTime.Now,extension,memoryStream);returndocument;}}publicvoidCloseDocument(){}}stringindexFolder=@"c:\MyIndex";stringurl="ftp://example.com/ExampleDocument.pdf";// Creating an indexIndexindex=newIndex(indexFolder);// Creating a document objectstringdocumentKey=url;IDocumentLoaderdocumentLoader=newDocumentLoaderFromFtp(documentKey,url,".pdf");Documentdocument=Document.CreateLazy(DocumentSourceKind.Stream,documentKey,documentLoader);Document[]documents=newDocument[]{document,};// Indexing the lazy-loaded documentIndexingOptionsoptions=newIndexingOptions();index.Add(documents,options);
Indexing from Amazon S3 Storage
The following example demonstrates how to index a document from Amazon S3 Storage when lazy initialized. The example uses the AWSSDK.S3 package.
C#
publicclassDocumentLoaderFromAmazon:IDocumentLoader{privatereadonlystringdocumentKey;privatereadonlystringextension;privatereadonlystringstorageKey;publicDocumentLoaderFromAmazon(stringdocumentKey,stringextension,stringstorageKey){this.documentKey=documentKey;this.extension=extension;this.storageKey=storageKey;}publicDocumentLoadDocument(){AmazonS3Clientclient=newAmazonS3Client();stringbucketName="my-bucket";GetObjectRequestrequest=newGetObjectRequest{Key=storageKey,BucketName=bucketName,};using(GetObjectResponseresponse=client.GetObjectAsync(request).GetAwaiter().GetResult()){MemoryStreammemoryStream=newMemoryStream();response.ResponseStream.CopyTo(memoryStream);memoryStream.Position=0;Documentdocument=Document.CreateFromStream(documentKey,DateTime.Now,extension,memoryStream);returndocument;}}publicvoidCloseDocument(){}}stringindexFolder=@"c:\MyIndex";stringkey="example.pdf";// Creating an indexIndexindex=newIndex(indexFolder);// Creating a document objectIDocumentLoaderdocumentLoader=newDocumentLoaderFromAmazon("documentKey",".pdf",key);Documentdocument=Document.CreateLazy(DocumentSourceKind.Stream,"documentKey",documentLoader);Document[]documents=newDocument[]{document,};// Indexing the lazy-loaded documentIndexingOptionsoptions=newIndexingOptions();index.Add(documents,options);
Indexing from Azure Blob Storage
The following example demonstrates how to index a document from Azure Blob Storage when lazy initialized. The example uses the Azure.Storage.Blobs package.
C#
publicclassDocumentLoaderFromAzure:IDocumentLoader{privatereadonlystringdocumentKey;privatereadonlystringextension;privatereadonlystringblobName;publicDocumentLoaderFromAzure(stringdocumentKey,stringextension,stringblobName){this.documentKey=documentKey;this.extension=extension;this.blobName=blobName;}publicDocumentLoadDocument(){stringconnectionString="***";// The connection string of the storage accountstringcontainerName="***";BlobClientblobClient=newBlobClient(connectionString,containerName,blobName);MemoryStreammemoryStream=newMemoryStream();blobClient.DownloadTo(memoryStream);memoryStream.Position=0;Documentdocument=Document.CreateFromStream(documentKey,DateTime.Now,extension,memoryStream);returndocument;}publicvoidCloseDocument(){}}stringindexFolder=@"c:\MyIndex";stringblobName="example.pdf";// Creating an indexIndexindex=newIndex(indexFolder);// Creating a document objectIDocumentLoaderdocumentLoader=newDocumentLoaderFromAzure("documentKey",".pdf",blobName);Documentdocument=Document.CreateLazy(DocumentSourceKind.Stream,"documentKey",documentLoader);Document[]documents=newDocument[]{document,};// Indexing the lazy-loaded documentIndexingOptionsoptions=newIndexingOptions();index.Add(documents,options);
More resources
GitHub examples
You may easily run the code from documentation articles and see the features in action in our GitHub examples: