Indexing from different sources

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Search MCP server lets your agent work with files on your machine; they are never uploaded. It runs in Docker: replace /path/to/documents with the folder it may use.

claude mcp add groupdocs-search -- docker run --rm -i -v /path/to/documents:/data ghcr.io/groupdocs-search/search-net-mcp:latest
Folders, licensing and more →

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#

string indexFolder = @"c:\MyIndex";
string documentFilePath = @"c:\MyDocuments\ExampleDocument.pdf";
  
// Creating an index
Index index = new Index(indexFolder);
  
// Creating a document object
Document document = Document.CreateFromFile(documentFilePath);
document.Attributes = new string[] { "main" }; // Setting the attributes of the document
document.AdditionalFields = new DocumentField[] // Setting the additional fields of the document
{
    new DocumentField("Subject", "Physics"),
};
Document[] documents = new Document[]
{
    document,
};
  
// Indexing document from the file
IndexingOptions options = new IndexingOptions();
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#

string indexFolder = @"c:\MyIndex";
string documentFilePath = @"c:\MyDocuments\ExampleDocument.pdf";
  
// Creating an index
Index index = new Index(indexFolder);
  
// Creating a document object
Stream stream = File.OpenRead(documentFilePath); // Opening a stream
Document document = Document.CreateFromStream(documentFilePath, DateTime.Now, ".pdf", stream);
Document[] documents = new Document[]
{
    document,
};
  
// Indexing document from the stream
IndexingOptions options = new IndexingOptions();
index.Add(documents, options);
  
// Closing the document stream after indexing is complete
stream.Close();

Indexing from a structure

The following example demonstrates how to index a document from a structure.

C#

string indexFolder = @"c:\MyIndex";
string documentFilePath = @"c:\MyDocuments\ExampleDocument.txt";
  
// Creating an index
Index index = new Index(indexFolder);
  
// Creating a document object
string text = File.ReadAllText(documentFilePath);
DocumentField[] fields = new DocumentField[]
{
    new DocumentField(CommonFieldNames.Content, text),
};
Document document = Document.CreateFromStructure("ExampleDocument", DateTime.Now, fields);
Document[] documents = new Document[]
{
    document,
};
  
// Indexing document from the structure
IndexingOptions options = new IndexingOptions();
index.Add(documents, options);

Indexing from URL

The following example demonstrates how to index a document by URL when lazy initialized.

C#

public class DocumentLoaderFromUrl : IDocumentLoader
{
    // A single HttpClient instance is reused for all requests
    private static readonly HttpClient httpClient = new HttpClient();

    private readonly string documentKey;
    private readonly string url;
    private readonly string extension;
  
    public DocumentLoaderFromUrl(string documentKey, string url, string extension)
    {
        this.documentKey = documentKey;
        this.url = url;
        this.extension = extension;
    }
  
    public Document LoadDocument()
    {
        // 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();
        MemoryStream memoryStream = new MemoryStream(data);
        Document document = Document.CreateFromStream(documentKey, DateTime.Now, extension, memoryStream);
        return document;
    }
  
    public void CloseDocument()
    {
    }
}
 
 
string indexFolder = @"c:\MyIndex";
string url = "https://example.com/ExampleDocument.pdf";
  
// Creating an index
Index index = new Index(indexFolder);
  
// Creating a document object
string documentKey = url;
IDocumentLoader documentLoader = new DocumentLoaderFromUrl(documentKey, url, ".pdf");
Document document = Document.CreateLazy(DocumentSourceKind.Stream, documentKey, documentLoader);
Document[] documents = new Document[]
{
    document,
};
  
// Indexing the lazy-loaded document
IndexingOptions options = new IndexingOptions();
index.Add(documents, options);

Indexing from FTP

The following example demonstrates how to index a document from FTP when lazy initialized.

C#

public class DocumentLoaderFromFtp : IDocumentLoader
{
    private readonly string documentKey;
    private readonly string url;
    private readonly string extension;
  
    public DocumentLoaderFromFtp(string documentKey, string url, string extension)
    {
        this.documentKey = documentKey;
        this.url = url;
        this.extension = extension;
    }
  
    public Document LoadDocument()
    {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url);
        using (WebResponse response = request.GetResponse())
        using (Stream stream = response.GetResponseStream())
        {
            MemoryStream memoryStream = new MemoryStream();
            stream.CopyTo(memoryStream);
            memoryStream.Position = 0;
            Document document = Document.CreateFromStream(documentKey, DateTime.Now, extension, memoryStream);
            return document;
        }
    }
  
    public void CloseDocument()
    {
    }
}
 
 
string indexFolder = @"c:\MyIndex";
string url = "ftp://example.com/ExampleDocument.pdf";
  
// Creating an index
Index index = new Index(indexFolder);
  
// Creating a document object
string documentKey = url;
IDocumentLoader documentLoader = new DocumentLoaderFromFtp(documentKey, url, ".pdf");
Document document = Document.CreateLazy(DocumentSourceKind.Stream, documentKey, documentLoader);
Document[] documents = new Document[]
{
    document,
};
  
// Indexing the lazy-loaded document
IndexingOptions options = new IndexingOptions();
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#

public class DocumentLoaderFromAmazon : IDocumentLoader
{
    private readonly string documentKey;
    private readonly string extension;
    private readonly string storageKey;
  
    public DocumentLoaderFromAmazon(string documentKey, string extension, string storageKey)
    {
        this.documentKey = documentKey;
        this.extension = extension;
        this.storageKey = storageKey;
    }
  
    public Document LoadDocument()
    {
        AmazonS3Client client = new AmazonS3Client();
        string bucketName = "my-bucket";
        GetObjectRequest request = new GetObjectRequest
        {
            Key = storageKey,
            BucketName = bucketName,
        };
        using (GetObjectResponse response = client.GetObjectAsync(request).GetAwaiter().GetResult())
        {
            MemoryStream memoryStream = new MemoryStream();
            response.ResponseStream.CopyTo(memoryStream);
            memoryStream.Position = 0;
            Document document = Document.CreateFromStream(documentKey, DateTime.Now, extension, memoryStream);
            return document;
        }
    }
  
    public void CloseDocument()
    {
    }
}
 
 
string indexFolder = @"c:\MyIndex";
string key = "example.pdf";
  
// Creating an index
Index index = new Index(indexFolder);
  
// Creating a document object
IDocumentLoader documentLoader = new DocumentLoaderFromAmazon("documentKey", ".pdf", key);
Document document = Document.CreateLazy(DocumentSourceKind.Stream, "documentKey", documentLoader);
Document[] documents = new Document[]
{
    document,
};
  
// Indexing the lazy-loaded document
IndexingOptions options = new IndexingOptions();
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#

public class DocumentLoaderFromAzure : IDocumentLoader
{
    private readonly string documentKey;
    private readonly string extension;
    private readonly string blobName;
  
    public DocumentLoaderFromAzure(string documentKey, string extension, string blobName)
    {
        this.documentKey = documentKey;
        this.extension = extension;
        this.blobName = blobName;
    }
  
    public Document LoadDocument()
    {
        string connectionString = "***"; // The connection string of the storage account
        string containerName = "***";
        BlobClient blobClient = new BlobClient(connectionString, containerName, blobName);

        MemoryStream memoryStream = new MemoryStream();
        blobClient.DownloadTo(memoryStream);
        memoryStream.Position = 0;
  
        Document document = Document.CreateFromStream(documentKey, DateTime.Now, extension, memoryStream);
        return document;
    }
  
    public void CloseDocument()
    {
    }
}
 
 
string indexFolder = @"c:\MyIndex";
string blobName = "example.pdf";
  
// Creating an index
Index index = new Index(indexFolder);
  
// Creating a document object
IDocumentLoader documentLoader = new DocumentLoaderFromAzure("documentKey", ".pdf", blobName);
Document document = Document.CreateLazy(DocumentSourceKind.Stream, "documentKey", documentLoader);
Document[] documents = new Document[]
{
    document,
};
  
// Indexing the lazy-loaded document
IndexingOptions options = new IndexingOptions();
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:

Free online document search App

Along with full featured .NET library we provide simple, but powerful free Apps.

You are welcome to search over your PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX and more with our free online Free Online Document Search App.