If I write
match (:Person)-[:RATING]->(m:Movie) return m
then (no rows) are returned, but if I use
match (:Person)-[r]->(m:Movie) return m
I got the results I need.
I tried figuring out why this is happening by typing
match (:Person)-[r]->(m:Movie) return type(r)
and the result is RATING.
Can anyone give me some ideas how to solve this?
I assume that your relationship type has some weird character at the beginning or end. To prove that theory try:
MATCH (:Person)-[r]->(:Movie)
WHERE type(r) <> 'RATING'
RETURN r
Related
I have a query which supposes to join two results and give the union as output.
MATCH (:Some )-[r*0..1]-> (p:Address) WITH collect(p) AS rows1
MATCH (:Some2)-[r*0..1]-> (pp:Address)
WITH rows1+collect(pp) AS rows2
UNWIND rows2 AS row RETURN row
As you can see the selection has two parts. So it works fine if there is matching data for both queries but if the second part of the match does not return anything then it returns empty. Meaning MATCH (:Some2)-[r*0..1]-> (pp:Address) returns empty then the whole union fails and returns null even if MATCH (:Some )-[r*0..1]-> (p:Address) return values .
How to fix this? Is it a bug in neo4j?
You have not actually asked a question (you have indeed just described the expected behaviour for pattern matching ... namely that if there is no match there is no result) ... but I assume you want a solution ?
MATCH (:Some )-[r*0..1]-> (p:Address)
RETURN p
UNION
MATCH (:Some2 )-[r*0..1]-> (p:Address)
RETURN p
should do the trick. Note that the only thing that matters is that the variables returned are exactly the same (what they contain doesn't actually matter).
Also ... you might want to have a look at what OPTIONAL MATCH does ...
Hope this helps.
Regards,
Tom
Update
CALL apoc.cypher.run("
MATCH (:Some )-[r*0..1]-> (p:Address)
RETURN p
UNION
MATCH (:Some2 )-[r*0..1]-> (p:Address)
RETURN p",{}) YIELD value
RETURN value.p AS result
ORDER BY result;
This simple query should work:
MATCH (s)-[*0..1]->(p:Address)
WHERE s:Some OR s:Some2
RETURN p;
Thanks for your inputs I used following version
MATCH (p:Address)
WHERE exist ((:Some )-[r*0..1]-> (p)) OR ((:Some2 )-[r*0..1]-> (p))
RETURN p;
Beginner question. How do I get the distinct types of edge in Cypher?
I know how to get all the edges in the database:
MATCH (a)-[r*1..1]->(b)
RETURN extract(x IN r | {rel: x})
And I can see that each one has a TYPE property. But how do I refine this to get the distinct TYPEs?
i think this is what you are looking for
MATCH (a)-[r]->(b)
RETURN distinct(type(r))
you might also use
call db.relationshipTypes
edit: if the first doesnt return anything that means you have no relationship in db, second might work only from neo4j 3.1 version on
For any length paths you can use UNWIND and DISTINCT:
MATCH p = (a)-[r*1..5]->(b)
UNWIND relationships(p) as rel
RETURN distinct type(rel) as type
The builtin procedure db.relationshipTypes will return a collection of all relationship types very quickly, from cached data. For example:
CALL db.relationshipTypes() YIELD relationshipType
RETURN relationshipType;
Prior to version 3.0.0-M05, this procedure was named sys.db.relationshipTypes.
I have modified the 'vanilla' initial query in this console, and added one relationship type 'LOCKED' between the 'Morpheus' and 'Cypher' nodes.
How can I modify the existing (first-run) query, which is a variable length path so that it no longer reaches the Agent Smith node due to the additional Locked relationship I've added?
First-run query:
MATCH (n:Crew)-[r:KNOWS|LOVES*2..4]->m
WHERE n.name='Neo'
RETURN n AS Neo,r,m
I have tried this kind of thing:
MATCH p=(n:Crew)-[r:KNOWS|LOVES*2..4]->m
WHERE n.name='Neo'
AND none(rel IN rels(p) WHERE EXISTS (StartNode(rel)-[:LOCKED]->EndNode(rel)))
RETURN n AS Neo,r,m
..but it doesn't recognize the pattern inside the none() function.
I'm using Community 2.2.1
Thanks for reading
I'm pretty sure you can't use a function in a MATCHy type clause like that (though it's clever). What about this?
MATCH path=(neo:Crew)-[r:KNOWS|LOVES|LOCKED*2..4]->m
WHERE neo.name='Neo'
AND NOT('LOCKED' IN rels(path))
RETURN neo,r,m
EDIT:
Oops, looks like Dave might have beat me to the punch. Here's the solution I came up with anyway ;)
MATCH p=(neo:Crew)-[r:KNOWS|LOVES*2..4]->m
WHERE neo.name='Neo'
WITH p, neo, m
UNWIND rels(p) AS rel
MATCH (a)-[rel]->(b)
OPTIONAL MATCH a-[locked_rel:LOCKED]->b
WITH neo, m, collect(locked_rel) AS locked_rels
WHERE none(locked_rel IN locked_rels WHERE ()-[locked_rel]->())
RETURN neo, m
Ok, this is a little convoluted but i think it works. The approach is to take all of the paths and find the last known good nodes (ones that have LOCKED relationships leaving them). Then use that node(s) as a new ending point(s) and return the paths.
match p=(n:Crew)-[r:KNOWS|LOVES|LOCKED*2..4]->m
where n.name='Neo'
with n, relationships(p) as rels
unwind rels as r
with n
, case
when type(r) = 'LOCKED' then startNode(r)
else null
end as last_good_node
with n
, (collect( distinct last_good_node)) as last_good_nodes
unwind last_good_nodes as g
match p=n-[r:KNOWS|LOVES*]->g
return p
I think this would be simpler if there was a locked: true property on the KNOWS and LOVES relationships.
The query as followings and get empty result. When I remove person-[:TEACH]-lesson, it works fine. Anybody can tell me what is the reason? Cheers
start person=node(1)
match person-[:TEACH|LEARN]-lesson,
person-[:TEACH]-lesson
return person,lesson
You probably don't have a TEACH relation between person and lesson.
match person-[:TEACH|LEARN]-lesson,
person-[:TEACH]-lesson
is match person-[:TEACH|LEARN]-lesson AND match person[:TEACH]-lesson, which is a repetition anyway.
start person=node(1)
match person-[:TEACH|LEARN]-lesson
return person,lesson
will work if you want to match TEACH or LEARN (as long as at least one of them exists, you'll get a result).
This is my cypher query:
start n=node(*) match p=n-[r:OWES*1..200]->n
with count(n) as numbern ,count(r) as numberr
where HAS(n.taxnumber) and numbern >= numberr
return extract(s in `relationships(p) : s.amount), extract(t in nodes(p) : ID(t)), length(p); `
This gives me Unknown identifier n error. What is wrong with this? I use Neo4j 1.8.2 for this.
n is no longer visible after your WITH,
p is also not visible then.
This query doesn't make any sense, what do you want to achieve with the aggregation?
Both counts return the same number btw.
Besides what we already discussed in the other issues, what do you want to achieve?