GroupDocs.Search has the ability to remove individual documents from the index that are indexed from a stream or structure. Such documents are identified by their document keys. To remove documents indexed by a file system path, see Delete indexed paths.
Deleting documents does not rewrite the index. Deleted documents are excluded from search results immediately, but their data remain in the index files until the index is optimized. Therefore, after deleting a large number of documents, call the optimize method to physically remove them and free up disk space. The DeleteResult object returned by the delete method contains the total, successful and failed counts, the results for individual items and the error messages.
The following example shows how to remove documents from an index by document key. The document is indexed from a stream with a document loader (see Indexing from different sources); in Python, the loader is a plain class with the load_document and close_document methods.
importdatetimeimportioimportosfromgroupdocs.searchimportIndexfromgroupdocs.search.commonimportDocument,DocumentSourceKindfromgroupdocs.search.optionsimportIndexingOptions,UpdateOptions# Implementing document loaderclassDocumentLoader(object):def__init__(self,file_path):self.file_path=file_pathself.document_key=os.path.basename(file_path)defclose_document(self):passdefload_document(self):extension=os.path.splitext(self.file_path)[1]withopen(self.file_path,"rb")asf:buffer=f.read()stream=io.BytesIO(buffer)document=Document.create_from_stream(self.document_key,datetime.datetime.now(),extension,stream)returndocumentfile_path="./sample.pdf"# Creating an index in the specified folderindex=Index("./index")# Indexing the document from streamdocument_loader=DocumentLoader(file_path)document=Document.create_lazy(DocumentSourceKind.STREAM,document_loader.document_key,document_loader)documents=[document]index.add(documents,IndexingOptions())# Searching in the indexsearch_result=index.search("Einstein")print("Documents found: "+str(search_result.document_count))# Deleting indexed document from the indexdocument_keys=[document_loader.document_key]delete_result=index.delete(UpdateOptions(),document_keys)print("Deleted documents: "+str(delete_result.success_count))# Physically removing the deleted documents from the index filesindex.optimize()
The following sample file is used in this example: sample.pdf
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.