The IndexRepository class can be used to combine several indexes into a group for searching all indexes at once. In addition, a single call to the update method can update all indexes in the repository. The index repository also serves as an event hub for all indexes in it: you can subscribe to any events available for an index also through the index repository. However, note that indexing documents is performed separately for each index.
To start using the index repository, indexes must be created or loaded and added to an instance of the index repository with the add_to_repository method, or created directly in the repository with the create method. The indexes property returns the indexes of the repository. Examples of creating and adding indexes to the repository are presented below.
fromgroupdocs.searchimportIndex,IndexRepository# Creating an index repository instanceindex_repository=IndexRepository()# Creating or loading an index and adding to the index repositoryindex1=Index("./index1")index_repository.add_to_repository(index1)# Creating an index via the index repository by calling a single methodindex2=index_repository.create("./index2")# Adding documents to the index 1index1.add("./documents1")# Adding documents to the index 2index2.add("./documents2")print("Indexes in the repository:",len(index_repository.indexes))
The following example shows how to subscribe to the repository events, update indexes in the repository, and search in the repository.
fromgroupdocs.searchimportIndex,IndexRepository# Creating an index repository instanceindex_repository=IndexRepository()# Subscribing to an eventdefon_error_occurred(sender,args):print("Index:",args.index_folder)print("Error:",args.message)index_repository.events.error_occurred+=on_error_occurred# Creating or loading an index and adding to the index repositoryindex1=Index("./index1")index_repository.add_to_repository(index1)# Creating or loading an index and adding to the index repositoryindex2=Index("./index2")index_repository.add_to_repository(index2)# Adding documents to the index 1index1.add("./documents1")# Adding documents to the index 2index2.add("./documents2")# Changing, deleting, adding documents to document folders# ...# Updating all indexes in the repositoryindex_repository.update()# Searching in the repositoryresult=index_repository.search("Einstein")print("Documents found:",result.document_count)
Index: index2
Error: Error during text extraction from documents2\damaged.pdf
GroupDocs.Parser.Exceptions.UnsupportedDocumentFormatException: Exception of type 'GroupDocs.Parser.Exceptions.UnsupportedDocumentFormatException' was thrown.
at .(Stream , LoadOptions )
at .()
at GroupDocs.Parser.Parser.GetMetadata()
at .()
at .(Document , , String )
Index: index2
Error: Error during text extraction from documents2\damaged.pdf
[TRUNCATED]