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.
Related
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 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.
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.
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
I am breaking out a question I asked elsewhere into a second part.
For a given node which has an id_str that is known to be in the graph, I have a list of new id_str that may or may not be in the graph. If they /are/ in the graph, I would like to create unique relationships to them. (If they are not, I want to ignore them.)
My current method is quite slow. I am doing the looping part outside of Neo, using py2neo and writing the entries one at a time using a very slow filter.
Originally, I was using...
fids = get_fids(record) # [100001, 100002, 100003, ... etc]
ids_in_my_graph = filter(id_is_in_graph, fids) # [100002]
def id_is_in_graph(id):
val = False
query = """MATCH (user:User {{id_str:"{}"}})
RETURN user
""".format(id)
n=neo4j.CypherQuery(graph_db,query).execute_one()
if n:
val = True
return(val)
for i in ids_in_my_graph:
"""MATCH (user:User {{id_str:"{}"}}),(friend:User {{id_str:"{}"}})
WHERE has(user.id_str) AND has(friend.id_str)
CREATE UNIQUE (user)-[:FRIENDS]->(friend)""".format(record.id, i)
And while I want new /unique/ [:FRIENDS] relationships, I do not want to create new users or new friends if a node does not already exist with a valid id_str.
So, I am trying to rewrite this using the FOREACH with collections. I think the actual syntax would be...
MATCH (user:User {id_str:"200001"}), (friends:User)
WHERE friends.id_str IN ["100001", "100002", "100003", "JUNK", "DOESNTMATCH", "IGNORED"]
FOREACH(friend in friends :
CREATE UNIQUE user -[:FRIENDS]-> friend)
But my error is
py2neo.neo4j.SyntaxException: Invalid input 'U': expected whitespace, comment, NodeLabel, MapLiteral, a parameter, a relationship pattern, '.', node labels, '[', "=~", IN, IS, '*', '/', '%', '^', '+', '-', '<', '>', "<=", ">=", '=', "<>", "!=", AND, XOR, OR or '|' (line 3, column 48)
" FOREACH(friend in friends : CREATE UNIQUE user -[:FRIENDS]-> friend)"
Create Unique does not seem to be supported for the FOREACH construct, even though this answer suggests this has been fixed.
And again, I cannot use the syntax suggested here in 11.2.2 because I do not want additional nodes to be created, only new relationships to already-existing nodes.
Couple of problems:
First, it will want parenthesises around the user and friend node in the CREATE UNIQUE pattern.
Second, the ":" separator inside FOREACH has been changed to "|", because there were readability clashes with the ":" used for types and labels.
Third, you should use MERGE instead of create unique. It's faster, more predictable and it replaces CREATE UNIQUE.
Finally:
Conceptually, the "friends" identifier points to one friend "at a time", so to speak, it isn't a collection of all the friends. You can turn it into such by doing:
WITH user, COLLECT(friends) AS friends
Of course, as you might've guessed, that actually means you don't need the FOREACH at all, so your final query could be:
MATCH (user:User {id_str:"200001"}), (friend:User)
WHERE friend.id_str IN ["100001", "100002", "100003", "JUNK", "DOESNTMATCH", "IGNORED"]
MERGE (user) -[:FRIENDS]-> (friend)
Make sure you have an index defined on friend.id_str, otherwise this will be very slow :)