def delete_snapshot_nodes_by_id(id_list): if not isinstance(id_list, list): id_list = [id_list] es = Common.get_elasticsearch().get_connection() for i in id_list: try: es.delete(index=SNAPSHOT_INDEX, doc_type=SNAPSHOT_DOC_TYPE, id=i) except ElasticsearchException, e: Common.get_logger().error('Error deleting node<%s>\n%s' % (i, e))
def execute_query(query, **kwargs): es = Common.get_elasticsearch().get_connection() try: response = es.search(index=SNAPSHOT_INDEX, doc_type=SNAPSHOT_DOC_TYPE, body=query, **kwargs) return response except ElasticsearchException, e: Common.get_logger().error('Error ES query execution\n%s' % e)
def get_snapshot_nodes(**kwargs): for key, val in kwargs.iteritems(): break if not isinstance(val, list): val = [val] es = Common.get_elasticsearch_connection() q_string_arr = [] for elem in val: q_string_arr.append( {"query_string": { "fields": [key, "*.%s" % key], "query": elem }}) try: query = {"query": {"bool": {"should": q_string_arr}}} response = es.search(index=SNAPSHOT_INDEX, doc_type=SNAPSHOT_DOC_TYPE, body=query) entities = {} if response['hits']['total'] > 0: for hit in response['hits']['hits']: entities[hit['_id']] = hit['_source'] return entities return None except Exception as e: logger = Common.get_logger() logger.error(str(e)) return None
def validate(self, subj, pred, objc): ''' if pred == to:subClassOf: (objc + objc_subclass) in [to:Entity, to:Property, to:Literal] elif pred == to:ontoLabel: subj must be define i.e. subj_subclass is non empty elif pred == to:description: subj must be define i.e. subj_subclass is non empty elif pred == to:domain: objc needs to exists in db as subject elif pred == to:range: objc needs to exists in db as subject if subj_subclass is Relational/Complex Relational Property: subj_range_subclass in [to:Entity, to:Property] elif subj_subclass is Property: subj_range_subclass is [to:Literal] elif pred == 'to:isUnique': pass else: not valid ''' input_tuple = (subj, pred, objc) (subj_subclass, ) = self.gather_info_from_toflerdb(subj, ['subclass']) if not subj_subclass: error_txt = ("Subject is not defined : %s" "\nInput tuple : %s") % (subj, input_tuple) Common.get_logger().error(error_txt) raise exceptions.InvalidInputValueError(error_txt) if pred == 'to:subClassOf': (objc_subclass, ) = self.gather_info_from_toflerdb( objc, ['subclass']) if not collection.intersection( objc_subclass + [objc], ['to:Entity', 'to:Property', 'to:Literal']): error_txt = ("Superclass is not defined : %s" "\nInput tuple : %s") % (objc, input_tuple) Common.get_logger().error(error_txt) raise exceptions.InvalidInputValueError(error_txt) elif pred == 'to:ontoLabel': pass elif pred == 'to:description': pass elif pred == 'to:domain': if not dbutils.exists_in_eternity( objc, additional_lookup=self._normalized_input, ontology_only=True): error_txt = ("Domain is not defined : %s" "\nInput tuple : %s") % (objc, input_tuple) Common.get_logger().error(error_txt) raise exceptions.InvalidInputValueError(error_txt) elif pred == 'to:range': if not dbutils.exists_in_eternity( objc, additional_lookup=self._normalized_input, ontology_only=True): error_txt = ("Range is not defined : %s" "Input tuple : %s") % (objc, input_tuple) Common.get_logger().error(error_txt) raise exceptions.InvalidInputValueError(error_txt) (objc_subclass, ) = self.gather_info_from_toflerdb( objc, ['subclass']) if collection.intersection( ['to:RelationalProperty', 'to:ComplexRelationalProperty'], subj_subclass): if not collection.intersection([objc] + objc_subclass, ['to:Entity', 'to:Property']): error_txt = ( "For relational property declaration, range must" "be either of type to:Entity or to:Property." "\nProperty : %s, range type: %s." "\nInput tuple : %s") % (subj, objc_subclass, input_tuple) Common.get_logger().error(error_txt) raise exceptions.InvalidInputValueError(error_txt) elif 'to:Property' in subj_subclass: if not 'to:Literal' in objc_subclass: error_txt = ("For property declaration," "range must be of type to:Literal." "\nProperty : %s, range type: %s." "\nInput tuple : %s") % (subj, objc_subclass, input_tuple) Common.get_logger().error(error_txt) raise exceptions.InvalidInputValueError(error_txt) elif pred == 'to:isUnique': pass else: error_txt = ( "Predicate must be one of to:subClassOf, to:ontoLabel," " to:description, to:domain, to:domain, to:range." "\nInput Tuple : %s") % str(input_tuple) Common.get_logger().error(error_txt) raise exceptions.InvalidInputValueError(error_txt)