Neo4j : Syntax Error - neo4j

Neo4j query for movie database NOT in Query
Neo4j - NOT IN query
MATCH (actor:Actor {name:"Tom Hanks"} )-[:ACTED_IN]->(movies)<-[:ACTED_IN]-(coactor)
WITH collect(distinct coactor) as coactors
MATCH (actor:Actor)
WHERE actor NOT IN coactors
RETURN actor
While running this query I got Error:
Invalid input 'N': expected whitespace, comment, node labels, MapLiteral, a parameter, a relationship pattern, '(', '.', '[', "=~", IN, STARTS, ENDS, CONTAINS, IS, '^', '*', '/', '%', '+', '-', '=', "<>", "!=", '<', '>', "<=", ">=", AND, XOR, OR, LOAD CSV, START, MATCH, UNWIND, MERGE, CREATE, SET, DELETE, REMOVE, FOREACH, WITH, RETURN, UNION, ';' or end of input (line 4, column 13 (offset: 160))
"WHERE actor NOT IN coactors"

I believe that's a small mistake in their example query.
WHERE actor NOT IN ...
is incorrect syntax (though I wouldn't mind a Cypher update to allow it). It should be
WHERE NOT actor IN ...

InverseFalcon has the correct answer, but note that the parser tells you exactly where the error is: line 4, column 13.
WHERE actor NOT IN coactors
^
It also tells you what can be expected at that point (including IN), and that doesn't include NOT.

Related

how show graph with relationship filtrered?

I have a node called "COG1476" which has different relationships with other nodes but I would like to get only those relationships that have a score> = 700 and I would also like to get the graph.
MATCH (cog1 {name: 'COG1497'})-[rel:coexpression|cooccurence|database|experimental|fusion|neighborhood|score|textmining]->(cog2)
WHERE toInteger(rel.score)>=700, toInteger(rel.cooccurence)>=700, toInteger(rel.coexpression)>=700, toInteger(rel.database)>=700, toInteger(rel.experimental)>=700, toInteger(rel.fusion)>=700, toInteger(rel.neighborhood)>=700,toInteger(rel.textmining)>=700
RETURN cog1, cog2, rel.score>=700, rel.cooccurence>=700, rel.coexpression>=700, rel.fusion>=700, rel.database=700, rel.experimental>=700, rel.neighborhood>=700, rel.textmining>=700
Neo.ClientError.Statement.SyntaxError: Invalid input ',': expected 0..9, '.', 'e', 'E', an identifier character, whitespace, node labels, '[', "=~", IN, STARTS, ENDS, CONTAINS, IS, '^', '*', '/', '%', '+', '-', '=', '~', "<>", "!=", '<', '>', "<=", ">=", AND, XOR, OR, FROM GRAPH, CONSTRUCT, LOAD CSV, START, MATCH, UNWIND, MERGE, CREATE UNIQUE, CREATE, SET, DELETE, REMOVE, FOREACH, WITH, CALL, RETURN, UNION, ';' or end of input (line 2, column 32 (offset: 160))
"WHERE toInteger(rel.score)>=700, toInteger(rel.cooccurence)>=700, toInteger(rel.coexpression)>=700, toInteger(rel.database)>=700, toInteger(rel.experimental)>=700, toInteger(rel.fusion)>=700, toInteger(rel.neighborhood)>=700,toInteger(rel.textmining)>=700"
^
Based on your comments, I think two things are wrong:
You've got a syntax error in your WHERE clause, which we fix by replacing the commas with ORs
You need to configure the Neo4j Browser app to only show matched relationships (or use the Table view)
First let's fix the query:
MATCH (cog1 {name: 'COG1497'})-[rel:coexpression|cooccurence|database|experimental|fusion|neighborhood|score|textmining]->(cog2)
WHERE toInteger(rel.score)>=700 OR toInteger(rel.cooccurence)>=700 OR toInteger(rel.coexpression)>=700 OR toInteger(rel.database)>=700 OR toInteger(rel.experimental)>=700 OR toInteger(rel.fusion)>=700 OR toInteger(rel.neighborhood)>=700 OR toInteger(rel.textmining)>=700
RETURN cog1, cog2, rel
That should return data and not blow up with an error. However, in the Browser you'll still see all the relationships between the nodes even though some of those relationships don't match our WHERE clause - that's just the default behaviour of the Neo4j Browser when visualising a graph.
To fix that, hit the Settings cog icon at the bottom left of the screen and untick the checkbox marked 'Connect result nodes' at the end of the configuration options. You'll now only see connections between nodes that you've explicitly selected - you may want to toggle this back on after you're done.
You can also check your results by using the Table view, which will show only those relationships that matched the criteria in your WHERE clause.

error in unwind command "Neo.ClientError.Statement.SyntaxError: Invalid input 'p': expected whitespace"

While loading data from csv file to neo4j, I am using below cypher query.
Load csv from
"file:///resume" AS row
FIELDTERMINATOR
'\u0001'
WITH
row, split(row[4], "|") AS pskills ,
split(row[5], "|") As frameworks,
split(row[6], "|") As databases,
UNWIND
pskills AS lang
UNWIND
frameworks AS fw
UNWIND
databases As db
MERGE
(p1:Person {name: row[1],id:row[0]})
MERGE
(p2:Skill:language {name: lang})
MERGE
(p3:Skill:framework {name: fw})
MERGE
(p4:Skill:database {name: db})
MERGE
(p1)-[:SKILLED_IN]->(p2)
MERGE
(p1)-[:SKILLED_IN]->(p3)
MERGE
(p1)-[:SKILLED_IN]->(p4);
which is giving error:
Neo.ClientError.Statement.SyntaxError: Invalid input 'p': expected
whitespace, comment, '{', node labels, MapLiteral, a parameter, a
relationship pattern, '(', '.', '[', "=~", IN, STARTS, ENDS, CONTAINS,
IS, '^', '*', '/', '%', '+', '-', '=', '~', "<>", "!=", '<', '>',
"<=", ">=", AND, XOR, OR, AS, ',', ORDER, SKIP, LIMIT, WHERE, FROM
GRAPH, CONSTRUCT, LOAD CSV, START, MATCH, UNWIND, MERGE, CREATE
UNIQUE, CREATE, SET, DELETE, REMOVE, FOREACH, WITH, CALL, RETURN,
UNION, ';' or end of input (line 5, column 10 (offset: 189)) " UNWIND
pskills AS lang "
Remove the extra comma(,) before first UNWIND

neo4j: is there a way to SKIP over results based on the total length of the result

I've got a query returning all nodes of a certain type. I'm trying to return only 10 results which are evenly distributed over the whole set.
I've tried:
$START t=node(*)
MATCH (image:Image) RETURN image.name ORDER BY image.name
SKIP toInt(count(image.name)/10);
However this returns an error message saying that I'm not allowed to use variables in SKIP.
I thought that I could try and re-run the whole statement in the count function then:
START t=node(*)
MATCH (image:Image) RETURN image.name ORDER BY image.name
SKIP toInt(count(MATCH (image:Image) RETURN image.name)/10);
However this returns an invalid input error:
Invalid input 'R': expected whitespace, comment, '.', node labels, '[', "=~", IN, STARTS, ENDS, CONTAINS, IS, '^', '*', '/', '%', '+', '-', '=', "<>", "!=", '<', '>', "<=", ">=", AND, XOR, OR, ',' or ')' (line 3, column 38 (offset: 112))
"SKIP toInt(count(MATCH (image:Image) RETURN image.name)/10);"
^
You could collect the results to a list and then return 10 elements from the collection:
MATCH (image:Image)
WITH image.name AS imageName
ORDER BY imageName
WITH COLLECT(imageName) AS imageNames, range(0, 10) AS indices
UNWIND indices AS index
RETURN imageNames[toInt(length(imageNames) * index / 10)]
To get the indices, we create a list of 10 numbers (0, ..., 9), UNWIND them get 10 separate rows and spread them across the indices.
Note that this query will not be very efficient as the server has to compute and collect all results, but the results set will only contain 10 rows.
(Note: I have not tested the query.)

Neo4j with distinct query

I have the following query in neo4j
MATCH (chatitems1)-[:PartOf]->(teamsChat)-[:OwnedBy]-()
WITH distinct teamsChat as teams order by teams.id limit 10,chatitems1
return teams
I want to select the first 10 different teams.id ordered and the id of chatitems1.
I get the error
Invalid input ',': expected 0..9, '.', 'e', 'E', an identifier character, whitespace, node labels, '[', "=~", IN, STARTS, ENDS, CONTAINS, IS, '^', '*', '/', '%', '+', '-', '=', "<>", "!=", '<', '>', "<=", ">=", AND, XOR, OR, WHERE, LOAD CSV, START, MATCH, UNWIND, MERGE, CREATE, SET, DELETE, REMOVE, FOREACH, WITH, RETURN, UNION, ';' or end of input (line 2, column 60 (offset: 115))
"WITH distinct teamsChat as teams order by teams.id limit 10,chatitems1"
Final query is
MATCH (user)-[:CreatesChat]-(chatitems)
WITH user ORDER BY user.id DESC LIMIT 10
MATCH (chatitems1)-[:PartOf]->(teamsChat)-[:OwnedBy]-()
WITH distinct teamsChat as teams,user,chatitems1
with user,chatitems1, teams as teams1 order by teams.id limit 10
with distinct user as users1, chatitems1, teams1
return chatitems1,teams1
I want to keep teamsChat.id for use later,
How can I fix this error?
you should use labels
the first part of your query expands paths, so you get duplicate users
use directions
you have a lot of duplicate with's which look like you're unsure what you'd use them for?
I don't think you need the last DISTINCT
try this:
MATCH (user:User)
WHERE SIZE( (user)-[:CreatesChat]->() ) > 0
WITH user ORDER BY user.id DESC LIMIT 10
MATCH (user)-[:CreatesChat]->(chatitems)-[:PartOf]->(teams)
WHERE (teamsChat)-[:OwnedBy]-()
RETURN teams,user, chatitems
ORDER BY teams.id limit 10
In a real world query you probably want to return certain attributes of your nodes.

Cypher query to get all the labels that have more than 2 nodes

I have a graph in which each connected component has a certain label let's say comp1, comp2, etc. I want to make a cypher query that returns all the labels that have more than one node.
I get all the labels like this:
match (n) return labels(n)
So i tried to do something like this in order to get only the labels that I needed:
match (n) with labels(n) as lb where count(k:lb[0]) >= 2) return lb limit 10
but I get a syntax error:
Invalid input ')': expected Digits, '.', 'E', whitespace, node labels,
'[', "=~", IN, IS, '*', '/', '%', '^', '+', '-', '<', '>', "<=", ">=",
'=', "<>", "!=", AND, XOR, OR, LOAD CSV, START, MATCH, UNWIND, MERGE,
CREATE, SET, DELETE, REMOVE, FOREACH, WITH, RETURN, UNION, ';' or end
of input (line 1, column 57)
I would also want to order the labels by the number of nodes that have that label...
if you're doing this with Neo4j version 2.0 you can achieve what you want with this cypher query:
Start n=node(*)
match (n)-->() with n,count(*) as rel_cnt where rel_cnt >= 2 return n;
but be aware that this query will transverse the whole graph so, it probably is a good idea to restrict it to certain labels. Cheers.
UPDATE
I read the question as nodes with more than 1 relation, my bad. This query wont do what the OP asked.
With 2.1
match (n)
unwind labels(n) as l
with l,count(*) as cnt
where cnt > 2
return l

Resources