1
votes

Having the following graphs:

  • node g1 with child nodes (a, b)

  • node g2 with child nodes (b, c)

using the query

MATCH (n)-[]-(m) WHERE ID(m) = id RETURN n

being id the id of the node g1, I get a and b, and vice-versa when using the id of g2. What I would like to understand is how can I get the intersection of those two results, in this case having the first return (a, b) and the second return (b, c) getting as final result (b).

I tried using the WITH cause but I wasn't able to achieve the desired result. Keep in mind that I'm new to Neo4j and only came here after a few failed attempts, research on Neo4j Documentation, general google search and Stackoverflow.

Edit1 (one of my tries):

MATCH (n)-[]->(m) 
WHERE ID(m) = 750 
WITH n 
MATCH (o)-[]->(b) 
WHERE ID(b) = 684 and o = n 
RETURN o

Edit2:

The node (b), that I represented as being the same on both graphs are in fact two different nodes on the db, each one relating to a different graph (g1 and g2). Representatively they are the same as they have the exactly same info (labels and attributes), but on the database thy are not. I'm sorry since it was my fault for not being more explicit on this matter :(

Edit3:

Why I don't using a single node (b) for both graphs

Using the graphs above as example, imagine that I have yet another layer so: on g1 the child node (b) as a child (e), while on g2 the child node (b) as a child (f). If I had (b) as a single node, when I create (e) and (f) I only could add it to (b) loosing the hierarchy, becoming impossible to distinguish which of them, (e) or (f), belonged to g1 ou g2.

2
Is there some reason there are two separate "b" nodes with the exact same labels and properties, instead of using only a single node to represent it? If the node represents what is logically the same entity, it usually makes sense to use a single node for it in a graph db instead of multiple, since this lets you query on how it's connected to different data in different contexts, such as this one where it could be a part of both graphs.InverseFalcon
Well my problem is simple. Using the graphs above as example, imagine that I have yet another layer so: on g1 the child node (b) as a child (e), while on g2 the child node (b) as a child (f). If I had (b) as a single node, when I create (e) and (f) I only could add it to (b) losing the hierarchy, becoming impossible to distinguish which of them, (e) or (f), belonged to g1 ou g2.Vitor Mexia
Fair enough, if the hierarchy is important and if child nodes are expected to be different on different subgraphs, then that makes sense. (One alternate approach, however, is to use relationship properties, with relations from one graph having distinct properties from relations from another subgraph) However, you still need a means of identifying nodes that are logically the same, even if they aren't the same node. If all properties must match, then any query that checks for the "same" nodes must check all nodes against all other nodes, that's not efficient.InverseFalcon
You mean like having (b)<-[belongsTo:g1]-(e) and (b)<-[belongsTo:g2]-(e)? How would that help on achieving my objective? Imagine a case in which I have this definition, if I wanted to show the entire graph g1 (only the nodes and relations that belong to these graph) would I be able to?Vitor Mexia
Something like that, although the correct representation (assuming for the moment the relationships have a type of :Connects, since you haven't supplied the type) would be (b)<-[:Connects {belongsTo: 'g1'}]-(e). If you wanted the entire 'g1' subgraph, if you've already matched on a node n within that subgraph, you could get all nodes in it with MATCH (n)-[rels:Connects*0..]-(subgraphNode) WHERE ALL (rel in rels WHERE rel.belongsTo = 'g1') RETURN DISTINCT subgraphNodeInverseFalcon

2 Answers

2
votes

This should work (assuming you pass id1 and id2 as parameters):

MATCH (a)--(n)--(c)
WHERE ID(a) = {id1} AND ID(c) = {id2}
RETURN n;

[UPDATED, based on new info from comments]

If you have multiple "clones" of the "same" node and you want to quickly determine which clones are related without having to perform a lot of (slow) property comparisons, you can add a relationship (say, typed ":CLONE") between clones. That way, a query like this would work:

MATCH (a)--(m)-[:CLONE]-(n)--(c)
WHERE ID(a) = {id1} AND ID(c) = {id2}
RETURN m, n;
0
votes

You can find the duplicity of the node, by using this query - [1] Duplicity with single node -

MATCH pathx =(n)-[:Relationship]-(find) WHERE  find.name = "action" RETURN pathx; 

[2] or for two nodes giving only immediate parent node

MATCH pathx =(n)-[:Relationship]-(find), pathy= (p)-[:Relationship]
-(seek) WHERE  find.name = "action" AND seek.name="requestID" RETURN pathx, 
pathy;

[3] or to find the entire network i.e. all the nodes connected -

MATCH pathx =(n)--()-[:Relationship]-(find), pathy= (p)--()-[:Relationship]- 
(seek) WHERE  find.name = "action" 
AND seek.name="requestID" RETURN pathx, pathy;