-1

How do I create and update nodes and property using plain cypher query?

Below is my query:

MERGE (c:contact {guid : '500010'})
ON CREATE SET
    c.data_source = '1',
    c.guid = '500010',
    c.created = timestamp()
ON MATCH SET
    c.lastUpdated = timestamp()
MERGE (s:speciality {specialtygroup_desc : 'cold'})
ON CREATE SET s.data_source = '1',
    s.specialtygroup_desc = 'fever',
    s.created = timestamp()
ON MATCH SET s.data_source = '1',
    s.specialtygroup_desc = 'comman cold',
    s.lastUpdated = timestamp()
MERGE (c)-[r:is_specialised_in]->(s)
ON CREATE SET
    r.duration = 1
ON MATCH SET
    r.duration = r.duration + 1

On the first run, node is created as "fever". On the second run, I have updated the specialty_group to "common cold". But it is creating new node with "fever". I am not able to update the "fever" to "common cold". What changes should I make to the above query?

terahertz
  • 2,915
  • 1
  • 21
  • 33
Amith.n
  • 1
  • 2
  • I think you need to add a better description of what you're trying to do. Is your intent to create contact-specific instances of :specialty nodes, so one contact might have a cold specialty, which is a different node than another :specialty connected to a separate contact? Or do you want to have common specialty node shared across :contact nodes, and your intent is to change which node the contacts are pointing at? – InverseFalcon Apr 13 '20 at 22:49

2 Answers2

0

The MERGE (s:speciality {specialtygroup_desc : 'cold'}) clause looks for a specialtygroup_desc value of "cold".

During the first execution, that MERGE clause finds no "cold" node -- so it creates one, and the subsequent ON CREATE clause changes it to "fever".

During the second execution, that MERGE again finds no "cold" node (since it is now a "fever" node), so it again creates a "cold" node and the ON CREATE clause yet again changes it to "fever". The ON MATCH clause is never used. This is why you end up with another "fever" node.

Unfortunately, you have not explained your use case in enough detail to offer a recommendation for how to fix your code.

cybersam
  • 63,203
  • 6
  • 53
  • 76
  • Actually am doing incremental load . for the first time m loading the customer along with the specialty group "cold". Then i have changed the speccialty group of that customer to "fever". Now If i run the query specialty group of that customer should update to the "fever". M trying to update the specialty group of that custome. Create or update. – Amith.n Mar 31 '20 at 02:34
  • Can you update your question to show sample data, the sequence of invocations, and what you expect as the result of each invocation? – cybersam Mar 31 '20 at 03:24
  • in the front end app for the for each customer there will be one many specialty group .On the first run contactid specialty 500010 cold – Amith.n Mar 31 '20 at 04:33
  • in the front end app for the for each customer there will be one many specialty group .On the first run contactid,specialty and the values is 500010,cold..On the second run i have updated specialty to "cold" to --> "fever" for that customer. I m trying to update the specialty node for that customer from "cold" to "Fever". In my case i have to update the "cold" specialty to "fever". result shuld be node(customer)-->updated specialty("fever"). In my above query trying to create and update the relatioonship node for that customer. – Amith.n Mar 31 '20 at 04:45
  • MERGE (c:contact {guid : '500010'}) ON CREATE SET c.data_source = '1', c.guid = '500010', c.created = timestamp() ON MATCH SET c.lastUpdated = timestamp() MERGE (s:speciality {specialtygroup_desc : 'cold'}) -----> need to updated "cold" to "fever" as specialty group ON CREATE SET s.data_source = '1', s.specialtygroup_desc = 'fever', s.created = timestamp() ON MATCH SET s.data_source = '1', s.specialtygroup_desc = 'fever', s.lastUpdated = timestamp() MERGE (c)-[r:is_specialised_in]->(s) ON CREATE SET r.duration = 1 ON MATCH SET r.duration = r.duration + 1 – Amith.n Mar 31 '20 at 04:52
  • please let me know is there any other code fix for this – Amith.n Apr 01 '20 at 02:38
  • 1
    Sorry, I'm not able to understand your comments. – cybersam Apr 01 '20 at 02:40
  • I have loaded fs202 as a specialty node for the customer 500010 as shown in the picture. Now i have updated 'fs202' as new specialty 'cold02' . Please let me know is there any cypher query to update speacilty for that customer – Amith.n Apr 01 '20 at 16:42
0

I think you want to update all node "cold" to "common cold" and if not exists "cold" or "common cold", create new "fever" ? My suggestion:

OPTIONAL MATCH (ss:speciality {specialtygroup_desc : 'cold'}
SET ss.specialtygroup_desc='common cold', ss.lastUpdated = timestamp()
MERGE (c:contact {guid : '500010'})
ON CREATE SET
    c.data_source = '1',
    c.guid = '500010',
    c.created = timestamp()
ON MATCH SET
    c.lastUpdated = timestamp()
MERGE (s:speciality {specialtygroup_desc : 'common cold'})
ON CREATE SET s.data_source = '1',
    s.specialtygroup_desc = 'fever',
    s.created = timestamp()

MERGE (c)-[r:is_specialised_in]->(s)
ON CREATE SET
    r.duration = 1
ON MATCH SET
    r.duration = r.duration + 1
nhanlam
  • 36
  • 1