Code from LIKE clause in CYPHER Query
MATCH (n) WHERE n.name =~ '(?i).*SUBSTRING.*' RETURN n;
results in
Expected 1 to be a java.lang.String, but it was a java.lang.Long (cause of =~)
Is there a way to for strings case insensitive in all possible places?
If
MATCH (n) WHERE str(n.name) =~ '(?i).*SUBSTRING.*' RETURN n;
works then you've got a node with a name property that isn't a String, and that's why the error
Related
A cypher query in neo4j can be written with WHERE clause:
match (n:PERSON) where n.name = 'Jonash' return n
But it can be also written with parentheses:
match (n:PERSON {name: 'Jonash'}) return n
Is this always possible for different operators, like contains, > or <?
Since 4.4 you can do node pattern predicates like this:
match (m:Movie where m.title contains "Matrix" and m.released = 1999)
return m
From cypher manual: https://neo4j.com/docs/cypher-manual/current/clauses/where/#node-pattern-predicates
But the short hand (n:Label{propertyKeyName: propertyKeyValue}) syntax is only there for equality.
In the latest version of Cypher, I can use this query to get all nodes with relationships:
MATCH (n)-[r]-(m) RETURN n,r,m
However, I'm missing nodes without any relationships.
In trying to query the missing nodes, this attempt gives me the error: Variable 'r' not defined
MATCH (n) WHERE NOT (n)-[r]->() RETURN n
And, this attempt shows zero results:
MATCH (n)-[r]->() WHERE r is null RETURN n
I can see the stragglers with:
MATCH (n) RETURN n
But, then I'm missing the relationships.
How do I phrase my query to find all nodes and all relationships without duplicates?
You can try the OPTIONAL MATCH:
MATCH (n)
OPTIONAL MATCH (n)-[r]-(m)
RETURN n, r, m
I'm having trouble while trying to write a Cypher query that returns all the nodes which their name starts with a certain string. I also need this query to be case insensitive.
Cypher has built in functions for both cases, but I don't know how to combine them
Query for matching the beginning of a string:
MATCH (n) WHERE n.Name STARTS WITH 'Pet' RETURN n
Query for case-insensitive strings
MATCH (n) WHERE n.Name =~ '(?i)ANDR.*' RETURN n
Any help will be appreciated.
For a case insensitive comparison using the STARTS WITH string comparison operator you can use the toLower() string function to convert each side of the comparison to lower case. For example:
MATCH (n)
WHERE toLower(n.name) STARTS WITH toLower('Pet')
RETURN n
NOT RELEVANT - SKIP TO Important Edit.
I have the following query:
MATCH (n)
WHERE (n:person) AND n.id in ['af97ab48544b'] // id is our system identifier
OPTIONAL MATCH (n)-[r:friend|connected|owner]-(m)
WHERE (m:person OR m:dog OR m:cat)
RETURN n,r,m
This query returns all the persons, dogs and cats that have a relationship with a specific person. I would like to turn it over to receive all the nodes & relationships that NOT includes in this query results.
If it was SQL it would be
select * from graph where id NOT IN (my_query)
I think that the OPTIONAL MATCH is the problematic part. I How can I do it?
Any advice?
Thanks.
-- Important Edit --
Hey guys, sorry for changing my question but my requirements has been changed. I need to get the entire graph (all nodes and relationships) connected and disconnected except specific nodes by ids. The following query is working but only for single id, in case of more ids it isn't working.
MATCH (n) WHERE (n:person)
OPTIONAL MATCH (n)-[r:friend|connected|owner]-(m) WHERE (m:person OR m:dog OR m:cat)
WITH n,r,m
MATCH (excludeNode) WHERE excludeNode.id IN ['af97ab48544b']
WITH n,r,m,excludeNode WHERE NOT n.id = excludeNode.id AND (NOT m.id = excludeNode.id OR m is null)
RETURN n,m,r
Alternatively I tried simpler query:
MATCH (n) WHERE (n:person) AND NOT n.id IN ['af97ab48544b'] return n
But this one does not returns the relationships (remember I need disconnected nodes also).
How can I get the entire graph exclude specific nodes? That includes nodes and relationships, connected nodes and disconnected as well.
try this:
match (n) where not n.id = 'id to remove' optional match (n)-[r]-(m)
where not n.id in ['id to remove'] and not m.id in ['id to remove']
return n,r,m
You've gotta switch the 'perspective' of your query... start by looping over every node, then prune the ones that connect to your person.
MATCH (bad:person) WHERE bad.id IN ['af97ab48544b']
WITH COLLECT(bad) AS bads
MATCH path = (n:person) - [r:friend|:connected|:owner] -> (m)
WHERE n._id = '' AND (m:person OR m:cat OR m:dog) AND NOT ANY(bad IN bads WHERE bad IN NODES(path))
RETURN path
That said, this is a problem much more suited to SQL than to a graph. Any time you have to loop over every node with a label, you're in relational territory, the graph will be less efficient.
query:
Match (d:User {name:"User"}) -[r:IS_MEMBER_OF]->(g:Group:Local) - [r1:IS_SUBGROUP_OF*0..]->(g1:Group) Return type(r), type(r1)
the cypher command type is valid for the relation without variable length paths but not valid for the variable ones even when they have the same name. How would I grab the name (type) of r1 as return from the query?
Thanks,
B
Unfortunately, there seems to be a bug in version 2.2.1 (and maybe some earlier versions) that prevents this from working:
MATCH (:User { name:"User" })-[r:IS_MEMBER_OF]->(:Group:Local)-[r1:IS_SUBGROUP_OF*0..]->(:Group)
RETURN type(r), EXTRACT(rel IN r1 | type(rel)) AS ancestorGroupTypes;
So, this is a workaround until the above simpler query works again:
MATCH p=(:User {name:"User"})-[r:IS_MEMBER_OF]->(:Group:Local)-[r1:IS_SUBGROUP_OF*0..]->(:Group)
Return type(r), EXTRACT (rel IN TAIL(RELATIONSHIPS(p)) | type(rel)) AS ancestorGroupTypes;