Skip to content

Commit

Permalink
CYPHER: moved back to id() vs elementId() as its significantly faster…
Browse files Browse the repository at this point in the history
…, this will have to be resolved in the future, as id is deprecated
  • Loading branch information
Julian Minder committed Dec 21, 2023
1 parent 37205c4 commit ea98548
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions rel2graph/neo4j/cypher.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,9 @@ def _merge_clause(name, merge_key, value, keys, prefix="(", suffix=")"):

def _match_clause(name, node_key, value, prefix="(", suffix=")"):
if node_key is None:
# ... add MATCH by id clause # TODO: elementid() is only guaranteed to be consistent within a transaction
# ... add MATCH by id clause # TODO: id() is deprecated, in the future we need to move to something else
# -> find alternative
return "MATCH %s%s%s WHERE elementId(%s) = %s" % (prefix, name, suffix, name, value)
return "MATCH %s%s%s WHERE id(%s) = %s" % (prefix, name, suffix, name, value)
else:
# ... add MATCH by label/property clause
nk = NodeKey(node_key)
Expand Down
20 changes: 10 additions & 10 deletions rel2graph/neo4j/graph_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,22 +184,20 @@ def __db_create__(self, tx):

for labels, nodes in node_dict.items():
pq = unwind_create_nodes_query(list(map(dict, nodes)), labels=labels)
pq = cypher_join(pq, "RETURN elementId(_)")
# TODO: id() is deprecated, in the future we need to move to something else
pq = cypher_join(pq, "RETURN id(_)")
records = tx.run(*pq)
# print("##CREATE REL##\n\n")
# print(pq)
# print("####\n\n")

for i, record in enumerate(records):
node = nodes[i]
node.identity = record[0]
for r_type, relationships in rel_dict.items():
data = map(lambda r: [r.start_node.identity, dict(r), r.end_node.identity],
relationships)
pq = unwind_create_relationships_query(data, r_type)
pq = cypher_join(pq, "RETURN elementId(_)")
# print("##CREATE REL##\n\n")
# print(pq)
# print("####\n\n")
# TODO: id() is deprecated, in the future we need to move to something else
pq = cypher_join(pq, "RETURN id(_)")

for i, record in enumerate(tx.run(*pq)):
relationship = relationships[i]
relationship.identity = record[0]
Expand Down Expand Up @@ -251,7 +249,8 @@ def __db_merge__(self, tx, primary_label=None, primary_key=None):
if pl is None or pk is None:
raise ValueError("Primary label and primary key are required for node MERGE operation")
pq = unwind_merge_nodes_query(map(dict, nodes), (pl, pk), labels)
pq = cypher_join(pq, "RETURN elementId(_)")
# TODO: id() is deprecated, in the future we need to move to something else
pq = cypher_join(pq, "RETURN id(_)")
identities = [record[0] for record in tx.run(*pq)]
if len(identities) > len(nodes):
raise ValueError("Found %d matching nodes for primary label %r and primary "
Expand All @@ -269,7 +268,8 @@ def __db_merge__(self, tx, primary_label=None, primary_key=None):
pq = unwind_merge_relationships_query(data, r_type)
else:
pq = unwind_merge_relationships_query(data, (r_type, pk))
pq = cypher_join(pq, "RETURN elementId(_)")
# TODO: id() is deprecated, in the future we need to move to something else
pq = cypher_join(pq, "RETURN id(_)")
identities = [record[0] for record in tx.run(*pq)]
if len(identities) > len(relationships):
raise ValueError("Found %d matching relations for primary "
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
setup(
name = "rel2graph",
packages = find_packages(),
version = "1.0.1",
version = "1.0.2",
description = "Library for converting relational data into graph data (neo4j)",
author = "Julian Minder",
author_email = "[email protected]",
Expand Down

0 comments on commit ea98548

Please sign in to comment.