Parse PATH records

Hello

My
query of

records, summary, keys = driver.execute_query ( query_str , parameters_ = parameters, database_ = db )

returns list of records that are PATH

an example element of the list is in below.

I would like to have a python code snippet how to get start and end labe lnodes of the path and the label of relationship.

I appreciate your help

<Record path=<Path start=<Node element_id='4:4bab0b38-db0b-402b-86d5-25a1c49cb2d7:761' labels=frozenset({'PartRevision', 'ProductRevision'}) properties={'created': neo4j.time.DateTime(2023, 5, 23, 21, 40, 30, 131000000, tzinfo=), '_type': 'ProductRevision', 'masterRef': '#id54', 'accessRefs': '#id13', 'nameRef': '#id1267', 'revision': 'B', 'descriptionTextRef': '#id1268', 'lbl': 'RjVplLIJqd$DyB', 'name': 'CROSSKART_DC', 'subType': 'ItemRevision', 'id': 'id1266', 'updated': neo4j.time.DateTime(2023, 5, 23, 21, 47, 17, 584000000, tzinfo=)}> end=<Node element_id='4:4bab0b38-db0b-402b-86d5-25a1c49cb2d7:791' labels=frozenset({'Deluxe', 'Base', 'Occurrence'}) properties={'associatedAttachmentRefs': '#id1596 #id1603', 'instanceRefs': '#id91', 'created': neo4j.time.DateTime(2023, 5, 23, 21, 40, 57, 335000000, tzinfo=), 'occurrenceRefs': 'id1610', 'lbl': 'xDVplLIJqd$DyB/F1ZplLIJqd$DyB', '_type': 'Occurrence', 'instancedRef': '#id1834', 'id': 'id86', 'sourceRef': '#id90', 'updated': neo4j.time.DateTime(2023, 5, 23, 21, 47, 48, 390000000, tzinfo=), 'parentRef': '#id7'}> size=1> hops=1>

Can you post your query? You can get this directly from the query.

Hi Gary ,

my query is using apoc.path.expandConfig

match ( t:PartRevision) where t.name = 'CROSSKART_DC'
WITH t
CALL apoc.path.expandConfig(
t,
{
labelFilter:"PartRevision, Base",
relationshipFilter:"HAS_PARENT<, +Latest Working> | -Engineering Released>",
uniqueness: "NODE_GLOBAL",
minLevel : 1,
maxLevel : 3,
bfs : false
}) YIELD path
RETURN path , length(path ) as hops
order by hops

You can return the first and last nodes of each path with something like the following:

match ( t:PartRevision) where t.name = 'CROSSKART_DC'
WITH t
CALL apoc.path.expandConfig(
t,
{
labelFilter:"PartRevision, Base",
relationshipFilter:"HAS_PARENT<, +Latest Working> | -Engineering Released>",
uniqueness: "NODE_GLOBAL",
minLevel : 1,
maxLevel : 3,
bfs : false
}) YIELD path
WITH path , length(path) as hops, nodes(path) as nodes
RETURN path, nodes[0] as firstNode, nodes[hops] as lastNode, hops
order by hops

Once you have a node, you can use labels(node) to get its labels. You can add this to your return statement. It will be a list.

Which relationship do you want the type, as each path may have more than one? You can get the relationships of a path with relationships(path). Once you have a specific relationship, you can get its type with type(relationship). I added to the return statement a list of the path's relationship types using list comprehension to extract them.

match ( t:PartRevision) where t.name = 'CROSSKART_DC'
WITH t
CALL apoc.path.expandConfig(
t,
{
labelFilter:"PartRevision, Base",
relationshipFilter:"HAS_PARENT<, +Latest Working> | -Engineering Released>",
uniqueness: "NODE_GLOBAL",
minLevel : 1,
maxLevel : 3,
bfs : false
}) YIELD path
WITH path , length(path) as hops, nodes(path) as nodes
RETURN path, nodes[0] as firstNode, nodes[hops] as lastNode, hops, 
[i in relationships(path) | type(i)] as path_relationships_types
order by hops

Does this help? I generally try to extract the specific information I need in the query so my driver code is simple using map accessors.

thank you Gary

My original query returns 88 records , all nodes and relationships up to 3 levels from root node CROSSKART_DC

but your last query , returns only immediate nodes of CROSSKART_DC, that are only 11 records

do you know why?

also , because i want to get more hands on experience with how to manipulate the outputs in PYTHON, i would like you help me how i can get the firstnode , last node and name of relationships through PATH records i asked originally.

records, summary, keys = driver.execute_query ( query_str , parameters_ = parameters, database_ = db )

if my records[0] is this one, how can i use properties of Record object like keys, values, items to parse the content?

<Record path=<Path start=<Node element_id='4:4bab0b38-db0b-402b-86d5-25a1c49cb2d7:761' labels=frozenset({'PartRevision', 'ProductRevision'}) properties={'created': neo4j.time.DateTime(2023, 5, 23, 21, 40, 30, 131000000, tzinfo=), '_type': 'ProductRevision', 'masterRef': '#id54', 'accessRefs': '#id13', 'nameRef': '#id1267', 'revision': 'B', 'descriptionTextRef': '#id1268', 'lbl': 'RjVplLIJqd$DyB', 'name': 'CROSSKART_DC', 'subType': 'ItemRevision', 'id': 'id1266', 'updated': neo4j.time.DateTime(2023, 5, 23, 21, 47, 17, 584000000, tzinfo=)}> end=<Node element_id='4:4bab0b38-db0b-402b-86d5-25a1c49cb2d7:791' labels=frozenset({'Deluxe', 'Base', 'Occurrence'}) properties={'associatedAttachmentRefs': '#id1596 #id1603', 'instanceRefs': '#id91', 'created': neo4j.time.DateTime(2023, 5, 23, 21, 40, 57, 335000000, tzinfo=), 'occurrenceRefs': 'id1610', 'lbl': 'xDVplLIJqd$DyB/F1ZplLIJqd$DyB', '_type': 'Occurrence', 'instancedRef': '#id1834', 'id': 'id86', 'sourceRef': '#id90', 'updated': neo4j.time.DateTime(2023, 5, 23, 21, 47, 48, 390000000, tzinfo=), 'parentRef': '#id7'}> size=1> hops=1>

thanks a lot

is this something i can get help?

Sorry, been busy. I am not a python developer, but a Java developer. I can show you how in Java. I am sure it is similar, but different syntax.

Here's a short snippet of how to work with paths in Python

import neo4j


DATABASE = "neo4j"
URI = "neo4j://localhost:7687"
AUTH = ("neo4j", "pass")

with neo4j.GraphDatabase.driver(URI, auth=AUTH) as driver:
    # create some data
    driver.execute_query(
        "MERGE (n:Start)-[r:REL1]->(m:Middle)-[r2:REL2]->(e:End)",
        database_=DATABASE,
    )
    # query the path we've just created
    records, _, _ = driver.execute_query(
        "MATCH path = (m:Start)-[*]->(e:End) "
        "RETURN path",
        database_=DATABASE,
    )
    path = records[0]["path"]
    start_node = path.start_node
    end_node = path.end_node
    rel_types = [rel.type for rel in path.relationships]

    print("start node", start_node)
    print("end node", end_node)
    print("rel types", rel_types)

You also can find some documentation on the graph types (Nodes, Relationships, Paths) in the API docs.

1 Like