cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

New relationship type as a subset of existing relationship type?

accounts4
Node Clone

Hi everyone!

I've created one specific relationship type based on the imported .csv file:

LOAD CSV WITH HEADERS FROM "file:///file.csv" AS csvLine
MATCH (c1:Country {CountryISO3Digit: csvLine.i}), (c2:Country {CountryISO3Digit: csvLine.j})
CREATE (c1)-[:EXPORTED {Year: datetime(csvLine.t), ProductHSCode: csvLine.k, ProductValue: toFloat(csvLine.v), ProductQuantity: toFloat(csvLine.q)}]->(c2)

Nodes are countries and relationship links are product trades between the countries. I would like to create a new relationship type based on a condition which will annotate a subset of links from the existing relationship type "EXPORTED". For example, i would like to generate a new relationship type based on the exported product code, but without creating a new relationships!

The following query isn't working. Please help!

MATCH (c1:Country)-[r:EXPORTED]->(c2:Country) WHERE r.ProductHSCode="123456"
SET (c1:Country)-[r:EXPORTED_PRODUCT_123456]->(c2:Country)
1 ACCEPTED SOLUTION

ameyasoft
Graph Maven
Try this: Add a new property 

MATCH (c1:Country)-[r:EXPORTED]->(c2:Country) WHERE r.ProductHSCode="123456"
SET r.exportedProduct = "123456"

To search on this:

MATCH (c1:Country)-[r:EXPORTED]->(c2:Country)
where r.exportedProduct is not null and r.exportedProduct = "123456"
RETURN c1, c2

View solution in original post

6 REPLIES 6

You'll want to use apoc.refactor.setType(). Although all it is doing under the hood is re-creating a new relationship with the name you pass it. So ID(r:EXPORTED) != ID(r:EXPORTED_PRODUCT_123456). Otherwise, there is no way to directly change the type of a relationship.

You'll want to use apoc.refactor.setType(). Although all it is doing under the hood is re-creating a new relationship with the name you pass it. So ID(r:EXPORTED) != ID(r:EXPORTED_PRODUCT_123456) . Otherwise, there is no way to directly change the type of a relationship.

@seankrobinson does that mean that the r:EXPORTED wouldn't consist anymore of the links which will be "transfered" into r:EXPORTED_PRODUCT_123456?

Try this: Add a new property

MATCH (c1:Country)-[r:EXPORTED]->(c2:Country) WHERE r.ProductHSCode="123456"
SET r.exportedProduct = "123456"

To search on this:

MATCH (c1:Country)-[r:EXPORTED]->(c2:Country)
where r.exportedProduct is not null and r.exportedProduct = "123456"
RETURN c1, c2

@ameyasoft, i managed to get this, thanks. May i ask why are you using "not null", there shouldn't be any nulls either way?

Do you guys think that the first solution (ie. creating new relationship type) would yield better querying performance?

May i ask why are you using "not null", there shouldn't be any nulls either way?

This new property will be added to "EXPORTED" relationship only under certain conditions like "123456". Otherwise this new property will not be available in all other 'EXORTED' relationships. Where it is not null syntax is a new version of old 'where exists'. By using this you will be filtering out the relationships where this new property exists.

Hi @ameyasoft thanks for the clarification! One more question though:

MATCH (c1:Country)-[r:EXPORTED]->(c2:Country)
where r.exportedProduct = "123456"
RETURN c1, c2

Wouldn't this filter imply by itself that only "exportedProduct" property is selected and therefore null values aren't selected by default?

ameyasoft
Graph Maven
Try this: Add a new property 

MATCH (c1:Country)-[r:EXPORTED]->(c2:Country) WHERE r.ProductHSCode="123456"
SET r.exportedProduct = "123456"

To search on this:

MATCH (c1:Country)-[r:EXPORTED]->(c2:Country)
where r.exportedProduct is not null and r.exportedProduct = "123456"
RETURN c1, c2