Over a million developers have joined DZone.

neo4j/cypher: CREATE with Optional Properties

· Java Zone

Learn more about the advantages of moving from a monolithic to microservices architecture.  Brought to you in partnership with IBM.

I’ve written before about using the cypher CREATE statement to add inferred information to a neo4j graph and sometimes we want to do that but have to deal with optional properties while creating our new relationships.

For example let’s say we have the following people in our graph with the ‘started’ and ‘left’ properties representing their tenure at a company:

CREATE (person1 { personId: 1, started: 1361708546 })
CREATE (person2 { personId: 2, started: 1361708546, left: 1371708646 })
CREATE (company { companyId: 1 })

We want to create a ‘TENURE’ link from them to the company including the ‘started’ and ‘left’ properties when applicable and might start with the following query:

START person = node:node_auto_index('personId:1 personId:2'), 
      company = node:node_auto_index('companyId:1') 
CREATE person-[:TENURE_AT { started: person.started, left: person.left }]-company 
RETURN person, company

which throws an exception because not all people have a ‘left’ property:

Error: org.neo4j.cypher.EntityNotFoundException: The property 'left' does not exist on Node[1]

We tweak our query a bit to make the property optional:

START person = node:node_auto_index('personId:1 personId:2'), 
      company = node:node_auto_index('companyId:1') 
CREATE person-[:TENURE_AT { started: person.started, left: person.left? }]-company 
RETURN person, company

which still doesn’t work:

Error: java.lang.IllegalArgumentException: Null parameter, key=left, value=null

After looking at this for a while Alistair pointed out that we should just split the updating of the optional property from the creation of the relationship so we end up with the following:

START person = node:node_auto_index('personId:1 personId:2'), 
      company = node:node_auto_index('companyId:1') 
CREATE person-[tenure:TENURE_AT { started: person.started }]-company 
WITH person, tenure, company
WHERE HAS(person.left)
SET tenure.left = person.left 
RETURN person, company

The code is on the console if you want to see how it works.

From Idea to Application gives you the architecture to quickly build, manage and run a range of applications (web, mobile, big data, new smart devices, etc.) on an open-standard, cloud-based platform. See why developers are using IBM Bluemix. Brought to you in partnership with IBM.

Topics:

Published at DZone with permission of Mark Needham, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}