Imagine I have 2 nodes A & B where relation from
A -> B is x
and relation from
B -> A is y
Is there a way where I can get just the outgoing relations from A ?
Like I need a CQL when asked for A -> B , gives me only x
EDIT:
I'm looking for a query which gives me relation from A -> B (only), not from B-> A
Thanks for your time.
You can simply do this to match all outgoing relations from A:
Match (A:yourLabel{yourProperty: theValue})-[r]->(B) return r
This will return all the outgoint relations from A.
Related
Say I have the following graph
Z
/ \
/ \
A -> B -> C -> D
\ /
-> X -> Y
I compute the paths
match p=((:A)-[*]->[:D])
return p
This will return three paths (rows): AZD, ABCD and AXYD
But I would like to return a subgraph that contains all the paths between A and D. so the result should be a subgraph. My understanding is that the only format for a subgraph return is nodes and relationships. So a query like below
// query logic
return nodes, relationships
What should I write in the query logic? NOTE:
this is not the entire graph, there are other subgraphs in my graph, so returning the entire graph does not work
A and D are just node types here, there will be many type A and type D nodes and there will be one or multiple paths between each A and D node pair.
One way to retrieve the unique set of nodes and relationships is using apoc
MATCH p=((:A)-[*]->(:D))
RETURN apoc.coll.toSet(
apoc.coll.flatten(
COLLECT(nodes(p))
)
) AS nodes,
apoc.coll.toSet(
apoc.coll.flatten(
COLLECT(relationships(p))
)
) AS relationships
I am trying to get a query that starting from a node, it returns the missing node that, when making a new relation to it, would complete a circle. Also it should respond which is the node that, if the circle is close, will end up having a relationship with the input node. Example:
Let's say I have B -> C and C -> A. In this case, if I pass A as input, I would like to receive { newRelationToMake: B, relationToInputNode: C } as a result, since connecting A -> B will result in a closed circle ABC and the relation that the node A will be having will come from C.
Ideally, this query should work for a maximum of n depths. For example for a depth of 4, with relations B -> C, C -> D and D -> A, and I pass A as input, I would need to receive { newRelationToMake: C, relationToInputNode: D} (since if I connect A -> C I close the ACD circle) but also receive {newRelationToMake: B, relationToInputNode: D }(since if I connect A -> B I would close the ABCD circle).
Is there any query to get this information?
Thanks in advance!
You are basically asking for all distinct nodes on paths leading to A, but which are not directly connected to A.
Here is one approach (assuming the nodes all have a Foo label and the relationships all have the BAR type):
MATCH (f:Foo)-[:BAR*2..]->(a:Foo)
WHERE a.id = 'A' AND NOT EXISTS((f)-[:BAR]->(a))
RETURN DISTINCT f AS missingNodes
The variable-length relationship pattern [:BAR*2..] looks for all paths of length 2 or more.
I'm trying to find all relations between node a and node b, and the relations could be multi-directions. For example,
a <- c -> b or a -> d -> b where c and d are nodes.
I've tried MATCH (a:PERSON {name: 'WD'})-[r*..3]-(b:PERSON{name: 'EK'}) RETURN r, a, b, but I got two isolated nodes, because the relation between a and b is: a <- c -> b.
Any help would be appreciated.
You can return the path if you need all the relationships and nodes in between.
Following query will
You can modify your query to return full paths instead of just nodes a and b as following:
MATCH paths=(a:PERSON {name: 'WD'})-[r*..3]-(b:PERSON{name: 'EK'})
RETURN paths
This will return paths of length up to 3, change it as you need.
I've got a Cypher query that gets a set of nodes 'n' of type 't', say (it works it's way through a number of different node types in the graph to reach this point).
If we assume the following:
The rest of type t nodes are the set 'm', so no intersect between m and n.
Type t nodes have multiple types of relationships between them.
I have a specific relationship 'r' that I'm interested in. In this specific case I know the following to be true:
Type t nodes can have 0 or more of these r relationships, incoming/outgoing.
The nodes within set n have no outgoing r relationships to set m
The nodes within set m may have outgoing r relationships to set m or n.
I have set n, I'm trying to determine the nodes from set m that meet the following conditions:
Have 0 r relationships
OR
Only have r relationships to set n, but not to any node in set m.
Some example data:
Type t nodes:
n1, n2, n3
m1, m2, m3
Type r relationships
m1 (no r relationships)
m2->n1, m2->n2
m3->n3, m3->m2
The results should return m1 and m2, but not m3.
I'm quite new to Cypher, so feel free to point to relevant documentation as required. Also, if you can explain the process you go through to determine the answer, I'd appreciate that as I suspect I'm just not quite understanding something simple here.
Your example is more model than data, you may know how to tell m:s and n:s apart but I cant write a query on the identifiers alone, there must be some actual data or structure to discriminate. For isntance, assume all nodes in the graph are type t, let sets n, m be distinguished by labels :N, :M, let the identifiers you use be values for property uid (to make the query results map with your question), and let type r relationship be [:R], then create your graph with
CREATE
(n1:N{uid:"n1"}), (n2:N{uid:"n2"}), (n3:N{uid:"n3"})
,(m1:M{uid:"m1"}), (m2:M{uid:"m2"}), (m3:M{uid:"m3"})
, m2-[:R]->n1, m2-[:R]->n2
, m3-[:R]->n3, m3-[:R]->m2
The query could then look something like
MATCH (n:N) // bind each node in the set n
WITH collect(n) AS nn // collect and treat them as a set nn
MATCH (m:M) // grab each node in the set m
OPTIONAL MATCH m-[:R]->(x) // optionally expand from m to unknown by r
WITH nn, m, collect(x) AS xx // collect unknown per m as xx where
WHERE ALL (x IN xx // all unknown nodes are in the nn set
WHERE x IN nn) // (if m has no -[:R]-> then the set xx is empty
// and the condition is true–i.e.
// either m has no outgoing r or
// the other node is in nn)
RETURN m
Result
m
(3:M {uid:"m1"})
(4:M {uid:"m2"})
You can try the query here.
I need to find common node between two nodes. For example find B from A -> B -> C
A = node 1
B = node 2
C = node 3
A, B and C have common property (user_id, fullname) and relation property is KNOWS. node index is user_id.
Node related to:
A [:KNOWS] B and B [:KNOWS] C
I have A and C node id. I want find B node id. How can I do this using Cypher or neo4jphp?
Would really prefer to see something that you had written yourself, but I suppose sometimes that's just too much effort...
START a=node(1)
MATCH (a)-[:KNOWS]->(b)-[:KNOWS]->(c)
WHERE a.user_id = ... (Explanation on what exactly should be done here was lacking)
RETURN b