neo4j cypher strange behaviour - neo4j

i just encountered an odd behaviour in neo4j
when i run this query
match (n)-[rel:HAS_A|HAS_DIPNOT *]->(c) where id(n) = 9457 return c
it returns expected nodes and relations..
however when i run this:
match (n)-[rel:HAS_DIPNOT *]->(c) where id(n) = 9457 return c
it returns nothing as can be seen on the screen capture..
do you have any idea why this happens?

Which relationships does the node with id 9457 have?
I presume it only has :HAS_A relationships. So your second query cannot traverse out the single step needed until there are :HAS_DIPNOT to continue on.
The nodes you highlighted have other id's.

Related

Unexpected relation and node COUNT in Neo4j

Whenever I am using a query to get the count of a specific node, I always get the number greater than 1 even though there is only one distinct type of that node existing.
Sample query:
MATCH (p)-[rel]->(v:myDistinctNode) RETURN COUNT(v)
Output: 80
MATCH (p)-[rel]->(v:myDistinctNode) RETURN COUNT(DISTINCT v)
Output: 1
I see different results while using DISTINCT, but I cannot use DISTINCT all the time. Why I am seeing this and how can I avoid it? Thanks!
Neo4j Kernel-Version: 3.5.14
The short answer is that you need to use a collect statement to make it work.
MATCH (p)<-[rel]-(v:myDistinctNode) WITH collect(v) AS nodes RETURN count(nodes)
This should return one.
I'm not a cypher expert, but I believe the reason it doesn't work is that the cypher result seems more like a table where in one row you have p, another row you have r, and the last row you have v. Even though v is a unique entity, there are still 80 rows that have v.

Neo4j Cypher query running endlessly

I have rerun the query below multiple times for the last two days and the Neo4j interface says it's running but it seems like it is running endlessly. I have run other queries which have all return an output. I left the query running for 9 hours and it was still running after 9 hr. I'm not sure what the issue is but would appreciate any help.
I'm running Neo4j-community-2.3.12 which is an older version but it should work as I am following a tutorial and the rest of the queries work fine.
Cypher script - which is very basic:
match p=(ione)-[:ResponseTo*]->(itwo)
where length(p)=9 with p
match (u)-[:CreateChat]->(i)
where i in nodes(p)
return count(distinct u);
Image of query running endlessly:
This query looks like an endless loop.
I would suggest instead of getting all the paths and checking length later get the paths of the desired length(9).
Also, consider adding labels in path query.
match p=(ione)-[:ResponseTo*9]->(itwo)
with p
match (u)-[:CreateChat]->(i)
where i in nodes(p)
return count(distinct u);
As Raj noted, you will want to use labels in this, as right now this is doing an all nodes scan which isn't performant.
We can also make sure the second match is more performant by ensuring we start i with the previously matched nodes, rather than applying that as a filter after the match:
match p=(ione)-[:ResponseTo*9]->(itwo)
unwind nodes(p) as i
with DISTINCT i
match (u)-[:CreateChat]->(i)
return count(distinct u);

Cypher query fails with variable length paths when trying to find all paths with unique node occurences

I have a highly interconnected graph where starting from a specific node
i want to find all nodes connected to it regardless of the relation type, direction or length. What i am trying to do is to filter out paths that include a node more than 1 times. But what i get is a
Neo.DatabaseError.General.UnknownError: key not found: UNNAMED27
I have managed to create a much simpler database
in neo4j sandbox and get the same message again using the following data:
CREATE (n1:Person { pid:1, name: 'User1'}),
(n2:Person { pid:2, name: 'User2'}),
(n3:Person { pid:3, name: 'User3'}),
(n4:Person { pid:4, name: 'User4'}),
(n5:Person { pid:5, name: 'User5'})
With the following relationships:
MATCH (n1{pid:1}),(n2{pid:2}),(n3{pid:3}),(n4{pid:4}),(n5{pid:5})
CREATE (n1)-[r1:RELATION]->(n2),
(n5)-[r2:RELATION]->(n2),
(n1)-[r3:RELATION]->(n3),
(n4)-[r4:RELATION]->(n3)
The Cypher Query that causes this issue in the above model is
MATCH p= (n:Person{pid:1})-[*0..]-(m)
WHERE ALL(c IN nodes(p) WHERE 1=size(filter(d in nodes(p) where c.pid = d.pid)) )
return m
Can anybody see what is wrong with this query?
The error seems like a bug to me. There is a closed neo4j issue that seems similar, but it was supposed to be fixed in version 3.2.1. You should probably create a new issue for it, since your comments state you are using 3.2.5.
Meanwhile, this query should get the results you seem to want:
MATCH p=(:Person{pid:1})-[*0..]-(m)
WITH m, NODES(p) AS ns
UNWIND ns AS n
WITH m, ns, COUNT(DISTINCT n) AS cns
WHERE SIZE(ns) = cns
return m
You should strongly consider putting a reasonable upper bound on your variable-length path search, though. If you do not do so, then with any reasonable DB size your query is likely to take a very long time and/or run out of memory.
When finding paths, Cypher will never visit the same node twice in a single path. So MATCH (a:Start)-[*]-(b) RETURN DISTINCT b will return all nodes connected to a. (DISTINCT here is redundant, but it can affect query performance. Use PROFILE on your version of Neo4j to see if it cares and which is better)
NOTE: This works starting with Neo4j 3.2 Cypher planner. For previous versions of
the Cypher planner, the only performant way to do this is with APOC, or add a -[:connected_to]-> relation from start node to all children so that path doesn't have to be explored.)

Fixing inconsistent Cypher results in Neo4j

I've been getting some strange results with a Neo4j database I made (version 2.1.0-M01). I have a graph with the following relationship:
Node[211854]{name : "dst"} <-[:hasrel]- Node[211823]{name : "src"}
I've confirmed this relationship using the following query:
START m=node(211854) MATCH (m)<-[r]-(n) RETURN m,r,n;
which returns a one row result, as expected:
| m | r | n
| Node[211854] | :hasrel[225081] | Node[211823]
The following query returns nothing, however:
START n=node(211823) MATCH (m)<-[r]-(n) RETURN m,r,n
Any thoughts on what might be happening? I've run these queries with and without indexes on the name properties for the nodes.
EDIT: Fixed typo with initial node number.
EDIT2: I rebuilt the server and both queries return the results I expect. Perhaps the error was corruption in the first database?
Using the node id's is not such a good idea, you can use the properties on your node to query them.
For example:
MATCH (m)<-[r]-(n {name: "src"}) RETURN m,r,n;
Does that query return what you expected?
You have to invert the relationship-direction. As you are looking for incoming relationships for your node 211823, this is not one of them. It's an outgoing relationship.
Please also update your database to the current version: 2.1.2 http://neo4j.org/download
START n=node(211823) MATCH (m)-[r]->(n) RETURN m,r,n
Perhaps you should give your nodes and relationships more descriptive names, so you spot easier when you inverted a domain concept.

Neo4J Cypher Match by multiple relationship types - strange behavior

My graph looks like this
medium-[:firstChapter]->chapter1-[:nextChapter]->chapter2_to_N
there is only one node connected via :firstChapter and then several nodes may follow, connected via :nextChapter
I tried to match all nodes that are either connected via relationship :firstChapter to medium or connected via :nextChapter from one chapter to another
The query I tried looks like this
start n=node(543) match n-[:firstChapter|nextChapter*]->m return m;
node(543) is the node medium.
Surprisingly, this query returns all nodes in the path, even though the nodes are not connected to n (=medium node)
If I leave out the * sign after nextChapter, only the first node with the :firstChapter relationship is returned (chapter1), which seems to be correct.
start n=node(543) match n-[:firstChapter|nextChapter*]->m return m;
Why does the query above return nodes not connected to n? As far as I understand it, the * sign usually returns nodes that are an unlimited number of relationships away, right?
What is the best way to match all nodes of a path (only once) that are either connected via :firstChapter or :nextChapter to a start node? In this case all chapters
The query above serves that purpose, but I don't think the output is correct...
EDIT:
Added a diagramm to clarify.
As you can see, the first chapter may only be reached via :firstChapter,
So it is still unclear, why the query above returns ALL chapter nodes
Try doing match p=n-[:firstChapter|nextChapter*]->m to see what p is. Hopefully that provides some insight about how they are connected.
What you might be looking for in the end is:
start n=node(543)
match n-[:firstChapter|nextChapter*]->m
return collect(distinct m);
To get a collection of distinct chapter nodes that follow n.
update
Here's another one--didn't actually test it but it might get you closer to what you want:
start n=node(543)
match n-[:firstChapter]->f-[:nextChapter*]-m
return f + collect(distinct m);
Using the * operator, the query looks for all relationships along the line for both relationship types, :firstChapter and :nextChapter (not just :nextChapter). Your chapter data for node(543) likely contains some relationships to chapter nodes not in the 543 chain and the query is returning them.
Consider adding an extra relationship using type :nextChapter to connect the start node to the first chapter, and check the relationships that exist on your chapters.
Then run:
start n=node(543)
match n-[:nextChapter*]->m
return m;
and see if you still get extra results. If so, you could run the following, bumping up n each time until you find the node that has the extra relationship(s) - (though I'm sure there are other ways!)
start n=node(543)
match n-[:nextChapter*1..n]->m
return m;

Resources