def query_entity(self): nmatcher = NodeMatcher(self.graph) rmatcher = RelationshipMatcher(self.graph) # 大通有哪些车 a_node = nmatcher.match('品牌', name='上汽大通').first() b_node = nmatcher.match('车系').first() relation = rmatcher.match({a_node, b_node}).first() zz = self.graph.match((a_node, ), r_type=type(relation).__name__).all() result = [] for z in zz: result.append(z.end_node.get('name')) print('大通具有:') print(result) # nodes = rmatcher.match({a_node, b_node}).all() # print(nodes[0]) # print(nodes[0].start_node) # print(nodes[0].end_node) # print(nodes[0].nodes) # print(type(nodes[0]).__name__) # # A(实体)车的基本参数(实体) a_node = nmatcher.match('车型', name='2019款 2.0T柴油自动四驱精英型长厢高底盘').first() b_node = nmatcher.match('基本参数').first() relation = rmatcher.match({a_node, b_node}).first() print(relation) print(type(relation).__name__) print(dict(nmatcher.match(name='2019款 2.0T柴油自动四驱精英型长厢高底盘').first())) zz = self.graph.match(nodes=(relation.start_node, ), r_type=type(relation).__name__).all()[0].end_node print(zz)
def relationships(self): """ A :class:`.RelationshipMatcher` for this graph. This can be used to find relationships that match given criteria as well as efficiently count relationships. """ return RelationshipMatcher(self)
def query_test(self): nmatcher = NodeMatcher(self.graph) rmatcher = RelationshipMatcher(self.graph) # A车的基本参数 a_node = nmatcher.match('车型', name='2019款 2.0T柴油自动四驱精英型长厢高底盘').first() # relation = rmatcher.match({a_node, b_node}).first() relation = self.graph.match(nodes=(a_node, ), r_type='基本参数').all() print(dict(relation[0].end_node)) # gg = self.graph.match(nodes=(relation.start_node, relation.end_node)).all() # b_node = nmatcher.match('车型基本参数', name='基本参数节点').first()
def match(self, nodes=None, r_type=None, limit=None): """ Match and return all relationships with specific criteria. For example, to find all of Alice's friends:: for rel in graph.match((alice, ), r_type="FRIEND"): print(rel.end_node["name"]) :param nodes: Sequence or Set of start and end nodes (:const:`None` means any node); a Set implies a match in any direction :param r_type: type of relationships to match (:const:`None` means any type) :param limit: maximum number of relationships to match (:const:`None` means unlimited) """ return RelationshipMatcher(self).match(nodes=nodes, r_type=r_type).limit(limit)
def hydrate_object(obj, inst=None): from py2neo.data import Node, Relationship, Path from py2neo.client.packstream import Structure if isinstance(obj, Structure): tag = obj.tag fields = obj.fields if tag == ord(b"N"): return Node.hydrate(self.graph, fields[0], fields[1], hydrate_object(fields[2]), into=inst) elif tag == ord(b"R"): return Relationship.hydrate(self.graph, fields[0], fields[1], fields[2], fields[3], hydrate_object(fields[4]), into=inst) elif tag == ord(b"P"): # Herein lies a dirty hack to retrieve missing relationship # detail for paths received over HTTP. nodes = [hydrate_object(node) for node in fields[0]] u_rels = [] typeless_u_rel_ids = [] for r in fields[1]: u_rel = self.unbound_relationship(*map(hydrate_object, r)) assert u_rel.type is None typeless_u_rel_ids.append(u_rel.id) u_rels.append(u_rel) if typeless_u_rel_ids: r_dict = {r.identity: r for r in RelationshipMatcher(graph).get(typeless_u_rel_ids)} for i, u_rel in enumerate(u_rels): if u_rel.type is None: u_rels[i] = self.unbound_relationship( u_rel.id, type(r_dict[u_rel.id]).__name__, u_rel.properties ) sequence = fields[2] return Path.hydrate(self.graph, nodes, u_rels, sequence) else: try: f = self.hydration_functions[tag] except KeyError: # If we don't recognise the structure type, just return it as-is return obj else: return f(*map(hydrate_object, obj.fields)) elif isinstance(obj, list): return list(map(hydrate_object, obj)) elif isinstance(obj, dict): return {key: hydrate_object(value) for key, value in obj.items()} else: return obj
def match(): """这里的节点是正常的,它有两个属性name和age name是Liz age是34 match("Person").where(age=34).first() 正常 match("Person").where(name='Liz').first() 正常 match("Person", name="Liz").first() 正常 match("Person", age=34).first() 正常 match("Person", age=34).where(name="Liz").first() None match("Person", name="Liz").where(age=34).first() None """ matcher_1 = NodeMatcher(graph) matcher_2 = RelationshipMatcher(graph) # TODO: 这里的 age 属性使用后返回结果为 None node = matcher_1.match("Person", name="Liz").where(age=34).first() relation = matcher_2.match(r_type='FRIENDS') return list(relation), node, type(relation)
def hydrate_object(self, obj): log.debug("Hydrating object %r", obj) from py2neo.data import Node, Relationship, Path if isinstance(obj, Structure): tag = obj.tag fields = obj.fields if tag == ord(b"N"): obj = Node.ref(self.graph, fields[0]) if fields[1] is not None: obj._remote_labels = frozenset(fields[1]) obj.clear_labels() obj.update_labels(fields[1]) if fields[2] is not None: obj.clear() obj.update(self.hydrate_object(fields[2])) return obj elif tag == ord(b"R"): start_node = Node.ref(self.graph, fields[1]) end_node = Node.ref(self.graph, fields[2]) obj = Relationship.ref(self.graph, fields[0], start_node, fields[3], end_node) if fields[4] is not None: obj.clear() obj.update(self.hydrate_object(fields[4])) return obj elif tag == ord(b"P"): # Herein lies a dirty hack to retrieve missing relationship # detail for paths received over HTTP. nodes = [self.hydrate_object(node) for node in fields[0]] u_rels = [] typeless_u_rel_ids = [] for r in fields[1]: u_rel = self.unbound_relationship( *map(self.hydrate_object, r)) assert u_rel.type is None typeless_u_rel_ids.append(u_rel.id) u_rels.append(u_rel) if typeless_u_rel_ids: r_dict = { r.identity: r for r in RelationshipMatcher(self.graph).get( typeless_u_rel_ids) } for i, u_rel in enumerate(u_rels): if u_rel.type is None: u_rels[i] = self.unbound_relationship( u_rel.id, type(r_dict[u_rel.id]).__name__, u_rel.properties) sequence = fields[2] return Path.hydrate(self.graph, nodes, u_rels, sequence) else: try: f = self.hydration_functions[tag] except KeyError: # If we don't recognise the structure type, just return it as-is return obj else: return f(*map(self.hydrate_object, obj.fields)) elif isinstance(obj, list): return list(map(self.hydrate_object, obj)) elif isinstance(obj, dict): return { key: self.hydrate_object(value) for key, value in obj.items() } else: return obj
# coding:utf-8 from py2neo import Graph, Node, data, Path, Relationship from py2neo.matching import \ NodeMatcher, RelationshipMatcher, \ EQ, NE, LT, LE, GT, GE, \ STARTS_WITH, ENDS_WITH, CONTAINS, LIKE, \ IN, AND, OR, XOR import pandas as pd graph = Graph('http://localhost:7474', username='******', password='******') graphMather = RelationshipMatcher(graph=graph) data = pd.read_excel('./data/test3.xlsx', index_col=0) for index in data.index: content = data.loc[index].dropna() #### 创建车系节点 ########################################################## car_series_node = graph.nodes.match('车系', name=content['车系1']).first() if car_series_node == None: car_series_node = Node('车系', name=content['车系1']) graph.create(car_series_node) ############################################################## #### 创建车型节点 ########################################### car_model_node = Node('车型', name=content['车型']) graph.create(car_model_node) ############################################### #### 创建车系车型边######################################
def hydrate_object(obj, inst=None): from py2neo.data import Path if isinstance(obj, Structure): tag = obj.tag fields = obj.fields if tag == b"N": return self.hydrate_node(inst, fields[0], fields[1], hydrate_object(fields[2])) elif tag == b"R": return self.hydrate_relationship(inst, fields[0], fields[1], fields[2], fields[3], hydrate_object(fields[4])) elif tag == b"P": # Herein lies a dirty hack to retrieve missing relationship # detail for paths received over HTTP. nodes = [hydrate_object(node) for node in fields[0]] u_rels = [] typeless_u_rel_ids = [] for r in fields[1]: u_rel = self.unbound_relationship(*map(hydrate_object, r)) assert u_rel.type is None typeless_u_rel_ids.append(u_rel.id) u_rels.append(u_rel) if typeless_u_rel_ids: r_dict = {r.identity: r for r in RelationshipMatcher(graph).get(typeless_u_rel_ids)} for i, u_rel in enumerate(u_rels): if u_rel.type is None: u_rels[i] = self.unbound_relationship( u_rel.id, type(r_dict[u_rel.id]).__name__, u_rel.properties ) sequence = fields[2] last_node = nodes[0] steps = [last_node] for i, rel_index in enumerate(sequence[::2]): next_node = nodes[sequence[2 * i + 1]] if rel_index > 0: u_rel = u_rels[rel_index - 1] rel = self.hydrate_relationship(None, u_rel.id, last_node.identity, next_node.identity, u_rel.type, u_rel.properties) else: u_rel = u_rels[-rel_index - 1] rel = self.hydrate_relationship(None, u_rel.id, next_node.identity, last_node.identity, u_rel.type, u_rel.properties) steps.append(rel) # steps.append(next_node) last_node = next_node return Path(*steps) else: try: f = self.hydration_functions[obj.tag] except KeyError: # If we don't recognise the structure type, just return it as-is return obj else: return f(*map(hydrate_object, obj.fields)) elif isinstance(obj, list): return list(map(hydrate_object, obj)) elif isinstance(obj, dict): return {key: hydrate_object(value) for key, value in obj.items()} else: return obj
def __init__(self): self.tx = g.begin() self.schema = Schema(g) self.n_matcher = NodeMatcher(g) self.r_matcher = RelationshipMatcher(g)
def __post_init__(self): self.nmatcher = NodeMatcher(self.graph) self.rmatcher = RelationshipMatcher(self.graph)