How can i did not pass specific relationship with the cypher - neo4j

with the bus Station problem, how can i did not pass same station with the Cypher eg not any Node? or the relationship
StationF--StationG
| |
StationA--StationB--StationC--StationD--StationE
| |
Station H
for example, I want not to pass the StationC-StationH And
StationH-StasionD
match(a:StationA)-[r:Road]-(z:StationE)
not any (node in nodes(p) | node.station) = StationH

If you are just trying to find the shortest path between a and z, you can use the shortestpath function.
For example:
MATCH (a:StationA), (z:StationE), p = shortestPath((a)-[:Road*]-(z))
RETURN p

Related

In neo4j, a query to count the number of distinct structures

In neo4j my database consists of chains of nodes. For each distinct stucture/layout (does graph theory has a better word?), I want to count the number of chains. For example, the database consists of 9 nodes and 5 relationships as this:
(:a)->(:b)
(:b)->(:a)
(:a)->(:b)
(:a)->(:b)->(:b)
where (:a) is a node with label a. Properties on nodes and relationships are irrelevant.
The result of the counting should be:
------------------------
| Structure | n |
------------------------
| (:a)->(:b) | 2 |
| (:b)->(:a) | 1 |
| (:a)->(:b)->(:b) | 1 |
------------------------
Is there a query that can achieve this?
Appendix
Query to create test data:
create (:a)-[:r]->(:b), (:b)-[:r]->(:a), (:a)-[:r]->(:b), (:a)-[:r]->(:b)-[:r]->(:b)
EDIT:
Thanks for the clarification.
We can get the equivalent of what you want, a capture of the path pattern using the labels present:
MATCH path = (start)-[*]->(end)
WHERE NOT ()-->(start) and NOT (end)-->()
RETURN [node in nodes(path) | labels(node)[0]] as structure, count(path) as n
This will give you a list of the labels of the nodes (the first label present for each...remember that nodes can be multi-labeled, which may throw off your results).
As for getting it into that exact format in your example, that's a different thing. We could do this with some text functions in APOC Procedures, specifically apoc.text.join().
We would need to first add formatting around the extraction of the first label to add the prefixed : as well as the parenthesis. Then we could use apoc.text.join() to get a string where the nodes are joined by your desired '->' symbol:
MATCH path = (start)-[*]->(end)
WHERE NOT ()-->(start) and NOT (end)-->()
WITH [node in nodes(path) | labels(node)[0]] as structure, count(path) as n
RETURN apoc.text.join([label in structure | '(:' + label + ')'], '->') as structure, n

Get all node-relation originating from a node in Neo4J

Let's say we have this cypher
match (n:Person{pid:322})-[k*1..2]->(s) return k
k would be a Collection of all relation originating from a specific node n in at most 2 hops.
How can I get all Node-Relation-Node where Relation is in k? It's something like match (a)-[b]->(c) foreach b in k return a,b,c but I know this query is invalid since foreach keyword in Neo4J can't be used in this case.
Edit:
I think I should add some illustration to make things clearer. Here I use an example from Neo4J documentation:
When I start from Charlie Sheen and using 1..2 range, the query must return (assuming the query ends with return a,b,c)
a | b | c
Charlie Sheen | ACTED_IN | Wall Street
Charlie Sheen | FATHER | Martin Sheen
Martin Sheen | ACTED_IN | The American President
Martin Sheen | ACTED_IN | Wall Street
This query should produce the a, b, and c values your edited question asks for (assuming you always want the name property values):
MATCH (n:Person{pid:322})-[k*1..2]->(s)
WITH LAST(k) AS lk
RETURN STARTNODE(lk).name AS a, TYPE(lk) AS b, ENDNODE(lk).name AS c;
I'm not sure why that wouldn't just be:
match (n:Person{pid:322})-[k*1..2]->(s) return n,k,s
Unless I'm misunderstanding your question.
A path will give you the sequence of nodes and relationsjips from n to s via k
MATCH p=(n:Person{pid:322})-[k*1..2]->(s) return p
You can also extract only the nodes and/or only the relationships from the path using nodes(p) or rels(p)

Cypher - Best way to create relationships between two arrays of nodes

I have two arrays of nodes
TYPE1 : [Node1, Node2, ...NodeN]
TYPE2 : [OtherNode1, OtherNode2....OtherNodeN]
I'm trying to connect each TYPE1 node to its corresponding TYPE2 node as follows.
(Node1) -[:RELATED_TO] -> (OtherNode1)
It's a simple one-to-one correspondence.
I used
MATCH (x:TYPE1),(y:TYPE2)
with x, y
with COLLECT(x) as n1, COLLECT(y) as n2
FOREACH(i in RANGE(0, 9) |
CREATE (n1[i])-[:RELATED_TO]->(n2[i])
)
which fails with
Error: Invalid input '[': expected an identifier character, node labels, a property map, ')' or a relationship pattern (line 4, column 21)
I have two questions.
What am I doing wrong in the query?
What is the best way to accomplish what I'm doing?
Many thanks!
Consider the following example data:
FOREACH (i IN range(1,10) | CREATE (:TYPE1), (:TYPE2))
Because you aren't interested in ordering your collections by any properties, you'll just be joining nodes in whatever order they are found by MATCH. The following query will do (what I think) you are trying to do, though it is inelegant:
MATCH (x:TYPE1), (y:TYPE2)
WITH COLLECT(DISTINCT x) AS n1, COLLECT(DISTINCT y) AS n2
WHERE LENGTH(n1) = LENGTH(n2)
FOREACH (i IN RANGE(0, LENGTH(n1) - 1) |
FOREACH (node IN [n1[i]] |
FOREACH (othernode IN [n2[i]] |
MERGE (node)-[:RELATED_TO]-(othernode)
)
)
)
Browser view post-query:

Express abscence of edges in Cypher

how do I express the following in Cypher
"Return all nodes with at least one incoming edge of type A and no outgoing edges".
Best Regards
You can use a pattern to exclude nodes from the result subset like this:
MATCH ()-[:A]->(n) WHERE NOT (n)-->() RETURN n
Try
MATCH (n)
WHERE ()-[:A]->n AND NOT n-->()
RETURN n
or
MATCH ()-[:A]->(n)
WHERE NOT n-->()
RETURN DISTINCT n
Edit
Pattern expressions can be used both for pattern matching and as predicates for filtering. If used in the MATCH clause, the paths that answer the pattern are included in the result. If used for filtering, in the WHERE clause, the pattern serves as a limiting condition on the paths that have previously been matched. The result is limited, not extended to include the filter condition. When a pattern is used as a predicate for filtering, the negation of that predicate is also a predicate that can be used as a filter condition. No path answers to the negation of a pattern (if there is such a thing) so negations of patterns cannot be used in the MATCH clause. The phrase
Return all nodes with at least one incoming edge of type A and no outgoing edges
involves two patterns on nodes n, namely any incoming relationship [:A] on n and any outgoing relationship on n. The second must be interpreted as a pattern for a predicate filter condition since it involves a negation, not any outgoing relationship on n. The first, however, can be interpreted either as a pattern to match along with n, or as another pattern predicate filter condition.
These two interpretations give rise to the two cypher queries above. The first query matches all nodes and uses both patterns to filter the result. The second matches the incoming relationship on n along with n and uses the second pattern to filter the results.
The first query will match every node only once before the filtering happens. It will therefore return one result item per node that meets the criteria. The second query will match the pattern any incoming relationship [:A] on n once for each path, i.e. once for each incoming relationship on n. It may therefore contain a node multiple times in the result, hence the DISTINCT keyword to remove doubles.
If the items of interest are precisely the nodes, then using both patterns for predicates in the WHERE clause seems to me the correct interpretation. It is also more efficient since it needs to find only zero or one incoming [:A] on n to resolve the predicate. If the incoming relationships are also of interest, then some version of the second query is the right choice. One would need to bind the relationship and do something useful with it, such as return it.
Below are the execution plans for the two queries executed on a 'fresh' neo4j console.
First query:
----
Filter
|
+AllNodes
+----------+------+--------+-------------+------------------------------------------------------------------------------------------------------------------------------+
| Operator | Rows | DbHits | Identifiers | Other |
+----------+------+--------+-------------+------------------------------------------------------------------------------------------------------------------------------+
| Filter | 0 | 0 | | (nonEmpty(PathExpression((17)-[ UNNAMED18:A]->(n), true)) AND NOT(nonEmpty(PathExpression((n)-[ UNNAMED36]->(40), true)))) |
| AllNodes | 6 | 7 | n, n | |
+----------+------+--------+-------------+------------------------------------------------------------------------------------------------------------------------------+
Second query:
----
Distinct
|
+Filter
|
+TraversalMatcher
+------------------+------+--------+-------------+--------------------------------------------------------------+
| Operator | Rows | DbHits | Identifiers | Other |
+------------------+------+--------+-------------+--------------------------------------------------------------+
| Distinct | 0 | 0 | | |
| Filter | 0 | 0 | | NOT(nonEmpty(PathExpression((n)-[ UNNAMED30]->(34), true))) |
| TraversalMatcher | 0 | 13 | | n, UNNAMED8, n |
+------------------+------+--------+-------------+--------------------------------------------------------------+

Get Node ID's in Neo4j using Python

I have recently begun using Neo4j and am struggling to understand how things work. I am trying to create relationships between nodes that I created earlier in my script. The cypher query that I found looks like it should work, but I don't know how to get the id's to replace the #'s
START a= node(#), b= node(#)
CREATE UNIQUE a-[r:POSTED]->b
RETURN r
If you want to use plain cypher, the documentation has a lot of usage examples.
When you create nodes you can return them (or just their ids by returning id(a)), like this:
CREATE (a {name:'john doe'}) RETURN a
This way you can keep the id around to add relationships.
If you want to attach relationships later, you should not use the internal id of the nodes to reference them from external system. They can for example be re-used if you delete and create nodes.
You can either search for a node by scanning over all and filtering using WHERE or add an index to your database, e.g. if you add an auto_index on name:
START n = node:node_auto_index(name='john doe')
and continue from there. Neo4j 2.0 will support index lookup transparently so that MATCH and WHERE should be as efficient.
If you are using python, you can also take a look at py2neo which provides you with a more pythonic interface while using cypher and the REST interface to communicate with the server.
This could be what you are looking for:
START n = node(*) , x = node(*)
Where x<>n
CREATE UNIQUE n-[r:POSTED]->x
RETURN r
It will create POSTED relationship between all the nodes like this
+-----------------------+
| r |
+-----------------------+
| (0)-[10:POSTED]->(1) |
| (0)-[10:POSTED]->(2) |
| (0)-[10:POSTED]->(3) |
| (1)-[10:POSTED]->(0) |
| (1)-[10:POSTED]->(2) |
| (1)-[10:POSTED]->(3) |
| (2)-[10:POSTED]->(0) |
| (2)-[10:POSTED]->(1) |
| (2)-[10:POSTED]->(3) |
| (3)-[10:POSTED]->(0) |
| (3)-[10:POSTED]->(1) |
| (3)-[10:POSTED]->(2) |
And if you don't want a relation between the reference node(0) and the other nodes, you can make the query like this
START n = node(*), x = node(*)
WHERE x<>n AND id(n)<>0 AND id(x)<>0
CREATE UNIQUE n-[r:POSTED]->x
RETURN r
and the result will be like that:
+-----------------------+
| r |
+-----------------------+
| (1)-[10:POSTED]->(2) |
| (1)-[10:POSTED]->(3) |
| (2)-[10:POSTED]->(1) |
| (2)-[10:POSTED]->(3) |
| (3)-[10:POSTED]->(1) |
| (3)-[10:POSTED]->(2) |
On the client side using Javascript I post the cypher query:
start n = node(*) WHERE n.name = '" + a.name + "' return n
and then parse the id number from response "self" in the form of:
server_url:7474/db/data/node/node_id
After hours of trying to figure this out, I finally found what I was looking for. I was struggling with how nodes were getting returned and found that
userId=person[0][0][0].id
would return what I wanted. Thanks for all your help though!
Using py2neo, the way I've found that is really useful is to use the remote module.
from py2neo import Graph, remote
graph = Graph()
graph.run('CREATE (a)-[r:POSTED]-(b)')
a = graph.run('MATCH (a)-[r]-(b) RETURN a').evaluate()
a_id = remote(a)._id
b = graph.run('MATCH (a)-[r]-(b) WHERE ID(a) = {num} RETURN b', num=a_id).evaluate()
b_id = remote(b)._id
graph.run('MATCH (a)-[r]-(b) WHERE ID(a)={num1} AND ID(b)={num2} CREATE (a)-[x:UPDATED]-(b)', num1=a_id, num2=b_id)
The remote function takes in a py2neo Node object and has an _id attribute that you can use to return the current ID number from the graph database.

Resources