简体   繁体   中英

Neo4j Cypher how to include another relationship node of parent in APOC neo4j cypher query

Let us considered

  • Drone (Parent)
    • Quadcropter (Child)
      • Pixel (Grand Child)

with [:SUB_CATEGORY] as relationship node 在此处输入图片说明

below Cypher query to fetch along with its parts tagged with each nodes

MATCH p=(n:Category)-[:SUB_CATEGORY*]->(m)<-[:TAGGED_TO]-(part:Part)
WHERE NOT ()-[:SUB_CATEGORY]->(n) AND toLower(part.name) STARTS WITH toLower('atm')
WITH COLLECT(p) AS ps
RETURN ps

above cypher query returns actual result set ie, its showing all relationship both SUB_CATEGORY and TAGGED_TO

在此处输入图片说明

Now if I used to convert this into Tree structure using APOC procedure then it skip TAGGED_TO relationship node of Parent Node ie, Drone

MATCH p=(n:Category)-[:SUB_CATEGORY*]->(m)<-[:TAGGED_TO]-(part:Part)
WHERE NOT ()-[:SUB_CATEGORY]->(n) AND toLower(part.name) STARTS WITH toLower('atm')
WITH COLLECT(p) AS ps
CALL apoc.convert.toTree(ps) YIELD value
RETURN value

Can you give me suggestion for getting the TAGGED_TO node of all nodes along with parent node using APOC

In the first query, the TAGGED_TO relationship between the parent node Drone and the :Part node is shown because the browser option Connect result nodes is enabled:

If this is checked, after a cypher query result is retrieved, a second query is executed to fetch relationships between result nodes.

But in fact in the result there is no such relationship because match pattern does not take into account the possibility of a path with a long zero . Try this:

MATCH p=(n:Category)-[:SUB_CATEGORY*0..]->(m:Category)<-[:TAGGED_TO]-(part:Part)
WHERE NOT ()-[:SUB_CATEGORY]->(n) AND toLower(part.name) STARTS WITH toLower('atm')
WITH COLLECT(p) AS ps
CALL apoc.convert.toTree(ps) YIELD value
RETURN value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM