neo4jcypherneo4j-apoc

Real relations from graph shows up when i try to create a sub-graph with virtual relationships


I have two large graphs in my database. Here is a query that works fine when I use it on the first graph:

MATCH (fe:FEATURE)-->+(t:THREAT)-->+(g:GAP)
MATCH (g)<--+(fa:FACT)
where t.key="FRAUD_PAN" 
WITH collect(DISTINCT [fe, t]) + collect(DISTINCT [t, g]) AS hases,
    collect(DISTINCT [fa, g]) AS requireses
WITH 
    COLLECT {
        WITH hases
        UNWIND hases AS pair
        RETURN [
            pair[0], 
            apoc.create.vRelationship(pair[0], 'HAS', {}, pair[1]), 
            pair[1]
        ] AS tuple
    } +
    COLLECT {
        WITH requireses
        UNWIND requireses AS pair
        RETURN [
            pair[0], 
            apoc.create.vRelationship(pair[0], 'REQUIRE', {}, pair[1]), 
            pair[1]
        ] AS tuple
    } AS tuples
UNWIND tuples AS tuple
RETURN tuple[0], tuple[1], tuple[2]

Here is its output

enter image description here

here is the output if I use the same query on another graph by just changing the threat key as per the graph

enter image description here Why does it produce two HAS relations? One is the virtual relation created through query and the other relation is from the main graph. Why is this happening?

Here is another version of this query that contains a few other nodes

// Match and collect the nodes and relationships
MATCH (fe:FEATURE)-->+(t:THREAT)-->+(g:GAP)
MATCH (g)<--+(fa:FACT)
MATCH (g)-->+(s:SCENARIO)
MATCH (fa)<--+(d:DATA)
WHERE fe.key="Test_S3 Storage"
WITH DISTINCT fe, t, g, fa, s, d

// Collect distinct pairs for each relationship type
WITH 
    collect(DISTINCT [fe, t]) AS hases,
    collect(DISTINCT [t, g]) AS threat_gaps,
    collect(DISTINCT [fa, g]) AS requireses,
    collect(DISTINCT [g, s]) AS scenarioes,
    collect(DISTINCT [d, fa]) AS datas
WITH 
    hases + threat_gaps + requireses + scenarioes + datas AS all_pairs
UNWIND all_pairs AS pair

// Create unique identifiers for virtual relationships
WITH DISTINCT pair[0] AS fromNode, pair[1] AS toNode,
    CASE
        WHEN pair[0]:FEATURE AND pair[1]:THREAT THEN 'V_HAS1'
        WHEN pair[0]:THREAT AND pair[1]:GAP THEN 'V_HAS2'
        WHEN pair[0]:FACT AND pair[1]:GAP THEN 'V_REQUIRE1'
        WHEN pair[0]:GAP AND pair[1]:SCENARIO THEN 'V_HAS3'
        WHEN pair[0]:DATA AND pair[1]:FACT THEN 'V_OF1'
    END AS relationshipType

// Return only virtual relationships, ensuring no real relationships are included
RETURN DISTINCT fromNode, apoc.create.vRelationship(fromNode, relationshipType, {}, toNode) AS virtualRelationship, toNode

Here is its output

enter image description here It can be seen that the original relation HAS is there even if it is not defined in the cypher for virtual relations. why the real relations does not pop-up in my first graph as I shown above? I am very confused with this.


Solution

  • The neo4j-desktop caused the original(present in main graph) relations to appear in the graph with virtual relations. I had to disable the property to get rid of these irrelevant edges. Here is the property that caused the issues:

    enter image description here