def index_documents()

in ees_microsoft_outlook/sync_enterprise_search.py [0:0]


    def index_documents(self, documents):
        """This method indexes the documents to the Enterprise Search.
        :param documents: Documents to be indexed
        """
        try:
            if documents:
                error_count = 0
                documents_dict = collections.defaultdict(dict)
                for document in documents:
                    documents_dict[document["id"]] = document
                total_records_dict = self.get_records_by_types(documents)
                total_inserted_record_dict = copy.deepcopy(total_records_dict)
                responses = self.workplace_search_custom_client.index_documents(
                    documents,
                    constant.CONNECTION_TIMEOUT,
                )
                if responses:
                    for each in responses["results"]:
                        if each["errors"]:
                            # Removing the failed document from the successfully indexed document count
                            error_count += 1
                            type = documents_dict[each["id"]]["type"]
                            total_inserted_record_dict[type].remove(each["id"])
            for type, count in total_records_dict.items():
                self.logger.info(
                    f"Total {type} indexed: {len(total_inserted_record_dict[type])} out of {len(count)}."
                    if total_inserted_record_dict
                    else "There is no record found to index into Workplace Search"
                )
            if error_count:
                self.logger.info(
                    f"Total {error_count} documents missed due to some error and it will sync in next full-sync cycle"
                )
        except Exception as exception:
            self.logger.info(
                f"Error while indexing {len(documents)} documents into Workplace Search. Error: {exception}"
            )