GroupDocs.Search allows indexing documents from various sources:
From files in the file system.
From a stream.
From a data structure as a list of fields.
The library also allows indexing from all presented sources with lazy initialization: a document created with the Document.create_lazy method is loaded by an IDocumentLoader implementation only when the index processes it. In Python, the loader is a plain class with the two methods of the interface; do not derive it from the IDocumentLoader class. The load_document method of the loader returns the loaded document, and the close_document method is called to release its resources. The loader is called for lazy documents of the DocumentSourceKind.STREAM and DocumentSourceKind.STRUCTURE kinds; a lazy document of the DocumentSourceKind.FILE kind is read from the file whose path is its key. If the loader raises an exception, the document is not indexed, and the error is reported through the error_occurred event.
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.
Indexing from a file
It should be borne in mind that the add method with a list of Document objects allows indexing only documents individually, and not entire folders. The advantage of indexing a list of documents is that you can add attributes and additional fields to the indexed document before calling the add method. The following example demonstrates how to index a document from a file.
fromgroupdocs.searchimportIndexfromgroupdocs.search.commonimportDocumentfromgroupdocs.search.optionsimportIndexingOptionsdocument_file_path="./sample.pdf"# Creating an indexindex=Index("./index")# Creating a document objectdocument=Document.create_from_file(document_file_path)documents=[document,]# Indexing document from the fileoptions=IndexingOptions()index.add(documents,options)print("Documents found:",index.search("Einstein").document_count)
The following sample file is used in this example: sample.pdf
The following example demonstrates how to index a document from a stream. A Python file object opened in binary mode can be passed as the stream; io.BytesIO works too.
importdatetimefromgroupdocs.searchimportIndexfromgroupdocs.search.commonimportDocumentfromgroupdocs.search.optionsimportIndexingOptionsdocument_file_path="./sample.pdf"# Creating an indexindex=Index("./index")# Creating a document objectstream=open(document_file_path,"rb")# Opening a streamdocument=Document.create_from_stream(document_file_path,datetime.datetime.now(),".pdf",stream)documents=[document,]# Indexing document from the streamoptions=IndexingOptions()index.add(documents,options)# Closing the document stream after indexing is completestream.close()print("Documents found:",index.search("Einstein").document_count)
The following sample file is used in this example: sample.pdf
The following example demonstrates how to index a document from a structure.
importdatetimefromgroupdocs.searchimportIndexfromgroupdocs.search.commonimportDocument,DocumentFieldfromgroupdocs.search.optionsimportCommonFieldNames,IndexingOptionsdocument_file_path="./sample.txt"# Creating an indexindex=Index("./index")# Creating a document objectwithopen(document_file_path,encoding="utf-8")asf:text=f.read()fields=[DocumentField(CommonFieldNames.CONTENT,text),]document=Document.create_from_structure("ExampleDocument",datetime.datetime.now(),fields)documents=[document,]# Indexing document from the structureoptions=IndexingOptions()index.add(documents,options)print("Documents found:",index.search("Einstein").document_count)
The following sample file is used in this example: sample.txt
The following example demonstrates how to index a document by URL when lazy initialized. The document is downloaded with urllib.request from the Python standard library.
importdatetimeimportioimporturllib.requestfromgroupdocs.searchimportIndexfromgroupdocs.search.commonimportDocument,DocumentSourceKindfromgroupdocs.search.optionsimportIndexingOptionsclassDocumentLoaderFromUrl(object):def__init__(self,document_key,url,extension):self.document_key=document_keyself.url=urlself.extension=extensiondefload_document(self):withurllib.request.urlopen(self.url)asresponse:data=response.read()memory_stream=io.BytesIO(data)document=Document.create_from_stream(self.document_key,datetime.datetime.now(),self.extension,memory_stream)returndocumentdefclose_document(self):passurl="https://example.com/ExampleDocument.pdf"# Creating an indexindex=Index("./index")# Reporting errors, for example, a document that cannot be loadedindex.events.error_occurred+=lambdasender,args:print(args.message)# Creating a document objectdocument_key=urldocument_loader=DocumentLoaderFromUrl(document_key,url,".pdf")document=Document.create_lazy(DocumentSourceKind.STREAM,document_key,document_loader)documents=[document,]# Indexing the lazy-loaded documentoptions=IndexingOptions()index.add(documents,options)print("Indexed documents:",len(index.get_indexed_documents()))
Indexing from FTP
The following example demonstrates how to index a document from FTP when lazy initialized. urllib.request downloads ftp:// URLs too.
importdatetimeimportioimporturllib.requestfromgroupdocs.searchimportIndexfromgroupdocs.search.commonimportDocument,DocumentSourceKindfromgroupdocs.search.optionsimportIndexingOptionsclassDocumentLoaderFromFtp(object):def__init__(self,document_key,url,extension):self.document_key=document_keyself.url=urlself.extension=extensiondefload_document(self):# urllib reads ftp:// URLs as well; credentials can be given as ftp://user:password@host/pathwithurllib.request.urlopen(self.url)asresponse:memory_stream=io.BytesIO(response.read())document=Document.create_from_stream(self.document_key,datetime.datetime.now(),self.extension,memory_stream)returndocumentdefclose_document(self):passurl="ftp://example.com/ExampleDocument.pdf"# Creating an indexindex=Index("./index")# Reporting errors, for example, a document that cannot be loadedindex.events.error_occurred+=lambdasender,args:print(args.message)# Creating a document objectdocument_key=urldocument_loader=DocumentLoaderFromFtp(document_key,url,".pdf")document=Document.create_lazy(DocumentSourceKind.STREAM,document_key,document_loader)documents=[document,]# Indexing the lazy-loaded documentoptions=IndexingOptions()index.add(documents,options)print("Indexed documents:",len(index.get_indexed_documents()))
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 boto3 package (pip install boto3), which takes the AWS credentials from its usual configuration, for example, environment variables.
importdatetimeimportiotry:importboto3# pip install boto3exceptImportError:boto3=Nonefromgroupdocs.searchimportIndexfromgroupdocs.search.commonimportDocument,DocumentSourceKindfromgroupdocs.search.optionsimportIndexingOptionsclassDocumentLoaderFromAmazon(object):def__init__(self,document_key,extension,storage_key):self.document_key=document_keyself.extension=extensionself.storage_key=storage_keydefload_document(self):client=boto3.client("s3")bucket_name="my-bucket"response=client.get_object(Bucket=bucket_name,Key=self.storage_key)memory_stream=io.BytesIO(response["Body"].read())document=Document.create_from_stream(self.document_key,datetime.datetime.now(),self.extension,memory_stream)returndocumentdefclose_document(self):passkey="example.pdf"# Creating an indexindex=Index("./index")# Reporting errors, for example, a document that cannot be loadedindex.events.error_occurred+=lambdasender,args:print(args.message)# Creating a document objectdocument_loader=DocumentLoaderFromAmazon("documentKey",".pdf",key)document=Document.create_lazy(DocumentSourceKind.STREAM,"documentKey",document_loader)documents=[document,]# Indexing the lazy-loaded documentifboto3isNone:print("boto3 is not installed (pip install boto3), so the document is not indexed")else:options=IndexingOptions()index.add(documents,options)print("Indexed documents:",len(index.get_indexed_documents()))
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-blob package (pip install azure-storage-blob).
importdatetimeimportiotry:fromazure.storage.blobimportBlobClient# pip install azure-storage-blobexceptImportError:BlobClient=Nonefromgroupdocs.searchimportIndexfromgroupdocs.search.commonimportDocument,DocumentSourceKindfromgroupdocs.search.optionsimportIndexingOptionsclassDocumentLoaderFromAzure(object):def__init__(self,document_key,extension,blob_name):self.document_key=document_keyself.extension=extensionself.blob_name=blob_namedefload_document(self):connection_string="***"# The connection string of the storage accountcontainer_name="***"blob_client=BlobClient.from_connection_string(connection_string,container_name,self.blob_name)memory_stream=io.BytesIO(blob_client.download_blob().readall())document=Document.create_from_stream(self.document_key,datetime.datetime.now(),self.extension,memory_stream)returndocumentdefclose_document(self):passblob_name="example.pdf"# Creating an indexindex=Index("./index")# Reporting errors, for example, a document that cannot be loadedindex.events.error_occurred+=lambdasender,args:print(args.message)# Creating a document objectdocument_loader=DocumentLoaderFromAzure("documentKey",".pdf",blob_name)document=Document.create_lazy(DocumentSourceKind.STREAM,"documentKey",document_loader)documents=[document,]# Indexing the lazy-loaded documentifBlobClientisNone:print("azure-storage-blob is not installed (pip install azure-storage-blob), so the document is not indexed")else:options=IndexingOptions()index.add(documents,options)print("Indexed documents:",len(index.get_indexed_documents()))
More resources
GitHub examples
You may find more code examples in our GitHub repository:
Along with the full-featured library, we provide a free online document search app. You are welcome to search over your PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX and more with our Free Online Document Search App.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.