def link_network_exists(self, link_object): """ Check whether a link object already exists in the network index :param link_object: The link object to check if exists :return: True if exists otherwise False """ if not isinstance(link_object, Link): raise LinkObjectError() connection = self.create_connection() if connection is not None: res = connection.search(index=self.network_index, body=link_object.get_search_dict()) if res["hits"]["total"] > 0: return True else: raise RequestError("Cannot connect to ElasticSearch") return False
def remove_network_link(self, link_object): """ Removes a link from the network :param link_object: The Link object being removed from the index. :return: Bool """ if not isinstance(link_object, Link): raise LinkObjectError() if self.link_network_exists(link_object): connection = self.create_connection() if connection is not None: connection.delete_by_query( index=self.network_index, body=link_object.get_search_dict(), ) return True else: raise RequestError("Cannot connect to ElasticSearch") else: raise LinkNotExistError()
def add_network_link(self, link_object): """ Adds a link to the network index :param link_object: The Link object being added to the index :return: The unique ID give to the link """ if not isinstance(link_object, Link): raise LinkObjectError() if not self.link_network_exists(link_object): connection = self.create_connection() if connection is not None: unique_id = str(uuid.uuid4()) connection.index( index=self.network_index, id=unique_id, body=link_object.get_dict(), ) return unique_id else: raise RequestError("Cannot connect to ElasticSearch") else: raise LinkExistError()