Cypher Where IN ['GUID', 'GUID'] in Neo4j - neo4j

I am trying to find nodes in Neo4j Db by Guid from my object.
I have 'Id' field in my object, which is Guid.
Need to find several nodes by those Guid Id, but when I use Where clause and use IN command with array of Guid Id's I get response, that (no changes, no records)
MATCH (n) WHERE n.Id IN ['44BEE918-507A-15C6-0950-0017D4A12234', 'BC2D286F-0B73-6926-B01D-27C8F0EA919D'] RETURN n LIMIT 25
I tried to pass Guids without quotes and with double quotes, but it didn't help.
When I pass the Name of those nodes it works perfect.
What I am doing wrong?

I found an issue.
The problem was with case sensitivity. Means my Guid in the database looks like '44bee918-507a-15c6-0950-0017d4a12234', but I tried to find in upper case.

Related

Neo4j error when property name contains \u

I have a node which has a property named authentication\username but when I try to use the cypher like:
match (n) where n.`authentication\username` = 'user' return n
neo4j shows an error
Neo.ClientError.Statement.SyntaxError
Invalid input 's': expected four hexadecimal digits specifying a unicode character
what is wrong with that?
Thanks
I am unsure, how this property got created, but now that it's there, it's pretty evident from the error message that \u in the property name is considered a unicode character. Directly matching, will not work here, so we will have to figure out the workarounds. I will suggest two workarounds, using a predicate function, any:
match (n) where any(property IN keys(properties(n)) WHERE property = "authentication\\username" AND apoc.map.get(properties(n), property) = "user") return n.
match (n) where any(property IN keys(properties(n)) WHERE property STARTS WITH "authentication" AND property ENDS WITH "username" AND apoc.map.get(properties(n), property) = "user") return n
In both the queries, we loop over the keys of the properties of a node.
In the first one, we look for the exact match of the key, notice I have escaped the \ while matching. In the second one, we look for key starting with authentication and ending with username, since that suits our pattern. Finally, I read the matching key value, using APOC map function. So you need to have APOC library installed.
One of them should definitely work for you. Try them out.

How to find a node quick in Neo4J 3.3.9 - cypher query optimization

I have the following Neo4J DB model:
I need to be able to find a node of the type Concept called "idea" made by the user infranodus and see which nodes of the type Context it is connected to.
I use the following query, but it's taking too long and doesn't even load...
MATCH (ctx:Context)-[:BY]->(u:User{name:'infranodus'})
WITH DISTINCT ctx
MATCH (c:Concept{name:'idea'})-[:AT]->(ctx)<-[:AT]-(cn:Concept)
RETURN DISINCT c, ctx, count(cn);
How to fix it?
adding indexes to the neo4j database would be helpful ( i didnt see any there ) then start your match with the indexed node and traverse from there. i think that having a
MATCH (c:Concept{name:'idea'})-[:AT]->(ctx)<-[:AT]-(cn:Concept)
WHERE (ctx)-[:BY]->(u:User{name:'infranodus'})
....
would also avoid querying twice.
Create an index on the name property of nodes with the User label - a unique index would be great for this.
An index for the name property on concept would be good, too (probably without uniqueness constraint - even though you're looking for distinct nodes explicitly).
Start your query from there.
MATCH (u:User {name:'infranodus'})
MATCH (c:Concept {name: 'idea'})
MATCH (u)<-[:BY]-(ctx:Context)<-[:AT]-(c)
MATCH (c:Concept{name:'idea'})-[:AT]->(ctx)<-[:AT]-(cn:Concept)
RETURN c, ctx, size((c)-[:AT]->(ctx)<-[:AT]-(:Concept))
Doesn't look as nice, but you're asking for query tuning. Make sure to PROFILE your existing query and this. Plus, you had a typo in your query after your return keyword.

What is wrong with this create statement syntax for neo4j?

I'm new to neo4j and I'm stuck on a simple problem:
create (machine1:host);
match (n) where n.host='machine1' return n;
The match fails. When using explain I see this:
The provided property key is not in the database
One of the property names in your query is not available in the database, make sure you didn't misspell it or that the label is available when you run this statement in your application (the missing property name is: host)
What am I doing wrong? Thanks for everyone's time.
You should know the difference between labels and properties.
A label is a grouping facility for Node where all nodes having a label are part of the same group.
I think you should use Machine label for Nodes which represents machines.
And you should use host property to store its name value.
create (:Machine {host:'machine1'});
match (n) where n.host='machine1' return n;
or even better:
match (n:Machine) where n.host='machine1' return n;

Where On Unique Property not working in neo4j

I have a graph in neo4j with 100 million nodes. I created an unique constraint on a property but when I use the property in my where clause it returns no rows. I know it has a result but returns no rows.
my Cypher query is like below:
MATCH(n:Person{PK:'1'})
RETURN n
or
MATCH(n:Person)
WHERE n.PK='1'
RETURN n
Can you try to use :schema in the browser to check your constraint?
Also note that it is case sensitive, both for the label and the key.
Just because you have a unique constraint doesn't mean that you have data, in this case a person, with that property. Try just pulling up a person and seeing what properties are set.
match (p:Person) return p limit 5;
My suspicion is that the problem is in how you are creating the Person nodes. Can you share that code with us?

Cypher query returning null values even when the pattern is available in database

Is something wrong with this cypher query
MATCH (owner:SidNode)<-[:OWNED_BY]-(acl:AclNode)-[:SECURES]->(class:ClassNode)
OPTIONAL MATCH (acl)<-[:COMPOSES]-(ace:AceNode)-[:AUTHORIZES]->(sid:SidNode)
WITH acl, ace, owner, sid, class
WHERE (acl.objectIdIdentity = {objectIdIdentity1} AND class.className = {className1})
RETURN
owner.principal AS aclPrincipal,
owner.sid AS aclSid,
acl.objectIdIdentity AS objectIdIdentity,
ace.aceOrder AS aceOrder,
ID(acl) AS aclId,
acl.parentObject AS parentObject,
acl.entriesInheriting AS entriesInheriting,
ID(ace) AS aceId, ace.mask AS mask,
ace.granting AS granting,
ace.auditSuccess AS auditSuccess,
ace.auditFailure AS auditFailure,
sid.principal AS acePrincipal,
sid.sid AS aceSid,
class.className AS className
ORDER BY acl.objectIdIdentity ASC, ace.aceOrder ASC
It is returning null values for ace nodes even though there are multiple nodes available in graph db.
But some times it is returning proper values like 4 rows if there are 4 ace nodes in db.
code i am writing is about spring security acl
reference link:
https://github.com/shazin/spring-security-acl-neo4j/blob/master/src/main/java/org/springframework/security/acls/neo4j/Neo4jLookupStrategy.java
Please suggest modifications.
Your problem comes from OPTIONAL MATCH, according to Neo4j's documentation, OPTIONAL MATCH returns NULLif the property or the element is not found.
You are getting NULL values because of this. If your acl node doesn't have any ace node related to him, the node will be replaced with NULL
You bind the ace nodes in an optional match. When that optional match fails to match something, ace will be null.
If you think the optional match ought to be successful in cases when it is not, maybe you could provide an example. A good way to do so is to create a small sample graph at http://console.neo4j.org
The conditions in your WHERE clause look like they belong with your MATCH clause. You might want to move the WHERE clause up and you can then remove the WITH altogether. It won't affect nulls, but it will make your query more efficient and more readable. (Also, you don't need parentheses in your WHERE clause.)
MATCH (owner:SidNode)<-[:OWNED_BY]-(acl:AclNode)-[:SECURES]->(class:ClassNode)
WHERE acl.objectIdIdentity = {objectIdIdentity1} AND class.className = {className1}
OPTIONAL MATCH (acl)<-[:COMPOSES]-(ace:AceNode)-[:AUTHORIZES]->(sid:SidNode)
RETURN ...

Resources