Is there a way to set Parameters in Cypher? - neo4j

I have a LOAD_CSV cypher script that creates and sets properties for nodes and edges.
I want to add a parameter at run time (i.e. when I do cat mycypher.cql | cypher-shell -u xxxx -p xxx) so that a key property gets set on nodes -- like so:
LOAD CSV WITH HEADERS FROM $MY_CSV AS row
MERGE (a:abcLabel {abcId: toInteger(row.abc_id), extraProp: $EXTRA_PROPERTY})
ON CREATE SET
abc.name = row.abc_name
MERGE (b:bcdLabel {bcdId: toInteger(row.bcd_id), extraProp: $EXTRA_PROPERTY})
ON CREATE SET
etc ....
Now, know that I can't use shell-like params, but is there a way to set $EXTRA_PROPERTY and $MY_FILE so that I can rerun the cql against a separate data set and ensure that a subsequent MATCH (:abcProperty {extraLabel: "xyz"}) will return nodes that were given the "xyz" property?
In principle this would be completely automated and templated so I will never do a manual load.
TIA

Upcoming version 1.2 of cypher-shell will support the command line option --param, which would allow you to specify Cypher parameters.
Here is the merged pull request.

Related

Neo4j Browser - Bug on create node?

I dont know if it cpuld be an issue/bug.
When you create a node without variable, server creates "zombie gray" nodes
Example: CREATE (Expedient {id_exp: 'MAPSAN_0004', name: 'contractual', dateCreation: '16/03/2022', dateExpiration:'16/03/2023'})
and run it 2 o 3 times.
You will see in console result for each command:
Created 1 node, set 4 properties, completed after 2 ms.
But when you want to match the nodes, per example:
MATCH (e:Expedient) RETURN e
you will not find de "zombie" nodes, only if run:
MATCH (e) RETURN e
you will see all nodes.
Is it a good behaviour?
Thx in advance community!
You are almost there, just missing a colon to define the node label.
CREATE (:Expedient
{id_exp: 'MAPSAN_0004', name: 'contractual', dateCreation: '16/03/2022', dateExpiration:'16/03/2023'})
If you don't include the colon, the Cypher treats it as a reference variable that you could refer to later in the same statement:
CREATE (e:Expedient {id_exp: 'MAPSAN_0004', name: 'contractual', dateCreation: '16/03/2022', dateExpiration:'16/03/2023'})
CREATE (e)-[:REL]->(:Node)
Another thing, you might want to store the dates as date format and not string.

unexpected failure on unique constraint in neo4j

I'm trying to load some data into neo4j from csv files, and it seems a unique constraint error is triggered when it shouldn't. In particular, I created a contraint using
CREATE CONSTRAINT ON (node:`researcher`) ASSERT node.`id_patstats` IS UNIQUE;
Then, after inserting some data in neo4j, if I run (in neo4j browser)
MATCH (n:researcher {id_patstats: "2789"})
RETURN n
I get no results (no changes, no records), but if I run
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM
'file:///home/manu/proyectos/PTL_RDIgraphs/rdigraphs/datamanager/tmp_patents/person906.csv' AS line
MERGE (n:researcher {`name` : line.`person_name`})
SET n.`id_patstats` = line.`person_id`;
I get
Neo.ClientError.Schema.ConstraintValidationFailed: Node(324016)
already exists with label researcher and property id_patstats =
'2789'
and the content of file person906.csv is
manu#cochi tmp_patents $cat person906.csv
person_id,person_name,doc_std_name,doc_std_name_id
2789,"li, jian",LI JIAN,2390
(this a minimum non working example extracted from a larger dataset; also, in the original "person906.csv" I made sure that "id_patstats" is really unique).
Any clue?
EDIT:
Still struggling with this...
If I run
MATCH (n)
WHERE EXISTS(n.id_patstats)
RETURN DISTINCT "node" as entity, n.id_patstats AS id_patstats
LIMIT 25
UNION ALL
MATCH ()-[r]-()
WHERE EXISTS(r.id_patstats)
RETURN DISTINCT "relationship" AS entity, r.id_patstats AS id_patstats
LIMIT 25
(clicking in the neo4j browser to get some examples of the id_patstats property) I get
(no changes, no records)
that is, id_patstats property is not set anywhere. Moreover
MATCH (n:researcher {`name` : "li, jian"})
SET n.`id_patstats` = XXX;
this will always trigger an error regardless of XXX, which (I guess) means the actual problem is that the name "li, jian" is already present. Although I didn't set any constraint on the name property, I'm guessing neo4j goes like this: you are trying to set a UNIQUE property on a node matching a property (name) that is not necessarily UNIQUE; hence that match could yield several nodes and I can't set the same UNIQUE property on all of them...so I won't even try
At least two of your researchers have the same name. You shouldn't MERGE by name and then add id as a property. You should MERGE by id and add the name as a property and it will work fine.
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM
'file:///home/manu/proyectos/PTL_RDIgraphs/rdigraphs/datamanager/tmp_patents/person906.csv' AS line
MERGE (n:researcher {`id_patstats`:line.`person_id`})
SET n.name`=line.`person_name`;

Not able to create a node with special characters "-" in neo4j

I am trying to create a node in neo4j(version 3.2.3). Below is the cypher query,
MERGE (`source-real-address`:SOURCE {Source:{`source-real-address`}})
I found in forums to create a node with special characters we should use
backticks `
in the query. But I couldn't able to create a node with backticks. No error were thrown in the logs.
Could you please help me to resolve this?
Please correct me if I am doing anything wrong in the cypher query. I am started
to understand neo4j cypher query language.
Note:- I am sending data to neo4j from graylog with the help of neo4j output plugin. I could able to create node without special character fields.
The syntax {Source:{`source-real-address`}}) means that you are trying to use a param named source-real-address as the value of the property Source. If this is your goal, you can set a param in the Neo4j Browser for test purposes with :params {"source-real-address":"Some value"}. If not, you can remove the extra { and } in the value and use "" instead of backticks, like this:
MERGE (source-real-address:SOURCE {Source:"source-real-address"})
Remeber that the value of a property should be Boolean, Integer, Float or String.
In Cypher backticks are used to create relationships, labels and variable names with special chars (not for property values).
Use the CREATE command to create Node with special characters
see this also: https://neo4j.com/docs/cypher-manual/current/syntax/naming/

Can't set attribute on relationship neo4j

I'm running the following cypher code. It used to work perfectly on previous versions of neo4j. Now it's not setting the attributes. The CSV file is intact and do possess the following fields. In the shell it's not even showing the line number on the last two rows, it's like it's completely ignoring the command.
using periodic commit
load csv with headers from "file:/relacionamento.csv" as row
match (p1:Pessoa {idpessoa: toint(row.idpessoa1)})
match (p2:Pessoa {idpessoa: toint(row.idpessoa2)})
create (p1)-[r:RELACIONAMENTO]->(p2) set r.peso_relacionamento = tofloat(row.peso_relacionamento),
r.peso_relacionamento_ponderado = tofloat(row.peso_relacionamento_fator)

Neo4j Cypher: How to stop duplicate SET if multiple CREATES

I have a complex cypher query that creates multiple nodes and increments some counters on those nodes. For sake of example here is a simplified version of what I am trying to do:
START a = node(1), e = node(2)
CREATE a-[r1]->(b {})-[r2]->(c {}), e-[r3]->b-[r4]->(d{})
SET a.first=a.first+1, e.second=e.second+1
RETURN b
The issue is that because there are two CREATE commands the SET commands run twice and the values are incremented by 2 instead of 1 as intended. I have looked to see if I can merge the multiple CREATE statements and I cannot.
My initial idea is to separate out the different creates into a batch query, however I was wondering if there is another option.
Where are you executing this query? What version of neo4j are you using?
I went to console.neo4j.org and successfully ran the following and it correctly added one to both a.first and e.second:
START a = node(1), e = node(2)
CREATE a-[r:KNOWS]->b-[r2:KNOWS]->c, e-[:KNOWS]->b-[:KNOWS]->d
SET a.first=a.first+1, e.second=e.second+1
RETURN b

Resources