I am new to Neo4j and graph databases. I am trying to create a simple graph DB, but my MATCH query is returning null values. How do I avoid nulls or any errors in my coding?
Create DB
CREATE
(d2:CorNode:course:dscor{Cors_Name:'DataScience_Sem2',Start_Date:'January2018',No_Students:[tointeger(10)]})-[:sem2dsrelation{Name:'Semester2',Cor_Name:'Data Science',A_Y:'AY2017-18',Cor_Node:'CMM534'}]->(Rob:FLNode:course:dscor{Name_F:'Rob Lothian', Role:'Leader',Mod_code:'CMM534'}),
(d2)-[:sem2dsrelation{Name:'Semester2', Cor_Name:'Data Science',A_Y:'AY2017-18',Cor_Node:'CMM534'}]->(Eyad:FLNode:course:dscor{Name_F:'Eyad Elyan',Role:'Leader',Mod_code:'CMM534'}),
(d2)-[:sem2dsrelation{Name:'Semester2', Cor_Name:'Data Science',A_Y:'AY2017-18',Cor_Node:'CMM507'}]->(David:FLNode:course:dscor{Name_F:'David Lonie', Role:'Leader', Mod_code:'CMM507'}),
(d2)-[:sem2dsrelation{Name:'Semester2',Cor_Name:'Data Science', A_Y:'AY2017-18',Cor_Node:'CMM535'}]->(Rob),
(d1:CorNode:course:dscor{Cors_Name:'DataScience_Sem1',Start_Date:'Septemer2017',No_Students:[tointeger(25)]})-[:sem1dsrelation{Name:'Semester1',Cor_Name:'Data Science',A_Y:'AY2017-18',Cor_Node:'CMM524'}]->(Rob),
(d1)-[:sem1dsrelation{Name:'Semester1', Cor_Name:'Data Science',A_Y:'AY2017-18',Cor_Node:'CMM531'}]->(Eyad),
(d1)-[:sem1dsrelation{Name:'Semester1', Cor_Name:'Data Science',A_Y:'AY2017-18',Cor_Node:'CMM510'}]->(Ines:FLNode:course:dscor{Name_F:'Ines Arana',Role:'Leader',Mod_code:'CMM510'})
Query DB
MATCH (:dscor)-[ds1:sem1dsrelation{Name:'Semester1',Cor_Name:'Data Science'}]-(FLNode:dscor)
RETURN DISTINCT [FLNode.Name_F,ds1.Cor_Node]
You basically have a typo. In (FLNode:dscor), "FLNode" is being used a a variable name instead of as a label.
To fix this, you need to assign a variable name (say, x) to the node, make sure FLNode is specified to be a label (by putting a : in front of it), and then use x in the result. Like so:
MATCH (:dscor)-[ds1:sem1dsrelation{Name:'Semester1',Cor_Name:'Data Science'}`]-(x:FLNode:dscor)
RETURN DISTINCT [x.Name_F,ds1.Cor_Node];
Related
Using Neo4j.
I would like to add a integer number to values already existing in properties of several relationships that I call this way:
MATCH x=(()-[y]->(s:SOL{PRB:"Taking time"})) SET y.points=+2
But it doesn't add anything, just replace by 2 the value I want to incremente.
To achieve this use
SET y.points = y.points + 2
From your original question it looks like you were trying to use the Addition Assignment operator which exists in lots of languages (e.g. python, type/javascript, C#, etc.). However, in cypher += is a little different and is designed to do this in a way which allows you to add or update properties to or on entire nodes or relationships based on a mapping.
If you had a parameter like the below (copy this into the neo4j browser to create a param).
:param someMapping: {a:1, b:2}
The query below would create a property b on the node with value 2, and set the value of property a on that node to 1.
MATCH (n:SomeLabel) WHERE n.a = 0
SET n+= $someMapping
RETURN n
I want to create a friends relation between abcd node and vbnm node having same node-name - Student
neo4j graph database visualization
I execute the following query, It doesn't show me any error but this query doesn't create any relation
match(Student:Stu),(Student:Stu)where Student.name="abcd" AND Student.name="vbnm" create(Student)-[fr:friends]->(Student)
You need use different variable name:
match(Student1:Stu),(Student2:Stu)
where Student1.name="abcd" AND
Student2.name="vbnm"
create(Student1)-[fr:friends]->(Student2)
I think you are confused by the syntax a little bit. Let me give you an example of a MATCH query syntax.
MATCH (variable1:Label),(variable2:Label) where variable1.foo = variable2.foo
You mixed label and variable in your query and each entity should have its own variable (variable1 and variable2) so you can interact with them.
So in your case the optimal query looks something like:
MATCH (s1:Student),(s2:Student ) where s1.name="abcd" AND s2.name="vbnm"
CREATE (s1)-[:friends]->(s2)
Note that you do not need to assign a variable to [:friends] relationship as you do not interact with it later in the same query.
I'd like to make a cypher query that generates a specific json output. Part of this output includes an object with a dynamic amount of keys relative to the children of a parent node:
{
...
"parent_keystring" : {
child_node_one.name : child_node_one.foo
child_node_two.name : child_node_two.foo
child_node_three.name : child_node_three.foo
child_node_four.name : child_node_four.foo
child_node_five.name : child_node_five.foo
}
}
I've tried to create a cypher query but I do not believe I am close to achieving the desired output mentioned above:
MATCH (n)-[relone:SPECIFIC_RELATIONSHIP]->(child_node)
WHERE n.id='839930493049039430'
RETURN n.id AS id,
n.name AS name,
labels(n)[0] AS type,
{
COLLECT({
child.name : children.foo
}) AS rel_two_representation
} AS parent_keystring
I had planned for children.foo to be a count of how many occurrences of each particular relationship/child of the parent. Is there a way to make use of the reduce function? Where a report would generate based on analyzing the array proposed below? ie report would be a json object where each key is a distinct RELATIONSHIP and the property value would be the amount of times that relationship stems from the parent node?
Thank you greatly in advance for guidance you can offer.
I'm not sure that Cypher will let you use a variable to determine an object's key. Would using an Array work for you?
COLLECT([child.name, children.foo]) AS rel_two_representation
I think, Neo4j Server API output by itself should be considered as any database output (like MySQL). Even if it is possible to achieve, with default functionality, desired output - it is not natural way for database.
Probably you should look into creating your own server plugin. This allows you to implement any custom logic, with desired output.
I'm searching for a way to remove every property, of any node in the DB, having a specific value using Cypher.
Context
I got a csv bulk file from a relational table with plenty of NULL values. LOAD CSV brings them as values. Removing them (replacing them with empty '' within the csv file) resulted in the same issue (properties without values). Tried many (many) Cypher operations to discard NULL values but nothing worked.
Can't find anything in the docs neither by Googling. Can this be done using only Cypher? It seems to me not (yet) supported.
Thanks.
How about this (when you know the property-names):
MATCH (n:Label)
WHERE n.property = ''
REMOVE n.property;
MATCH (u:User)
WHERE u.age = ''
SET u.age = null;
If you know which columns these are in your import you can do something like this
load csv with headers from "" as line
with line, case line.foo when '' then null else line.foo end as foo
create (:User {name:line.name, foo:foo})
It won't create the properties with null.
For numeric values it's easier as toInt() and toFloat() return null on unparseable values like ''.
No, there is no way to do this only with chypher. I suppose that you have already seen the way to do it via REST. That is the best solution for now.
I'm using Neo4j 2.0.0-M06. Just learning Cypher and reading the docs. In my mind this query would work, but I should be so lucky...
I'm importing tweets to a mysql-database, and from there importing them to neo4j. If a tweet is already existing in the Neo4j database, it should be updated.
My query:
MATCH (y:Tweet:Socialmedia) WHERE
HAS (y.tweet_id) AND y.tweet_id = '123'
CREATE UNIQUE (n:Tweet:Socialmedia {
body : 'This is a tweet', tweet_id : '123', tweet_userid : '321', tweet_username : 'example'
} )
Neo4j says: This pattern is not supported for CREATE UNIQUE
The database is currently empty on nodes with the matching labels, so there are no tweets what so ever in the Neo4j database.
What is the correct query?
You want to use MERGE for this query, along with a unique constraint.
CREATE CONSTRAINT on (t:Tweet) ASSERT t.tweet_id IS UNIQUE;
MERGE (t:Tweet {tweet_id:'123'})
ON CREATE
SET t:SocialMedia,
t.body = 'This is a tweet',
t.tweet_userid = '321',
t.tweet_username = 'example';
This will use an index to lookup the tweet by id, and do nothing if the tweet exists, otherwise it will set those properties.
I would like to point that one can use a combination of
CREATE CONSTRAINT and then a normal
CREATE (without UNIQUE)
This is for cases where one expects a unique node and wants to throw an exception if the node unexpectedly exists. (Far cheaper than looking for the node before creating it).
Also note that MERGE seems to take more CPU cycles than a CREATE. (It also takes more CPU cycles even if an exception is thrown)
An alternative scenario covering CREATE CONSTRAINT, CREATE and MERGE (though admittedly not the primary purpose of this post).