Create a relation by searching all nodes properties and when both from , to exists

I am exploring Neo4j, to prototype few functions to understand how it helps.

I have three node types A, B, C and have name on their properties. Names are all unique in the entire graph.

I also have relations.csv and it will have only name to name connections.

Now, need to create connection based on names where I don't know which Node label it belongs to. So, I thought of repeating query only if two match are available.

LOAD CSV WITH HEADERS FROM 'file:///{}' AS row  
//look up the two nodes we want to connect up  
MATCH (p1:A {name:row.from}), (p2:B {name:row.to}) 
WHERE p1.name=row.from and p2.name= row.to 
// now create a relationship between them  
CREATE (p1)-[c:CONNECT]->(p2) return type(c);"

There is no guarantee that the name is present in A to B. It could also be B to A, or B to C or C to A or C to B.

What would be the efficient way to create connections. Thanks for your support.

When you ‘match’ and you provide a map of property values, those match will find nodes t have those property values, so there is no need to repeat the conditions in a ‘where’ clause.

You stated the name property values are unique in the entire database, so is should not be necessary to specify the label to match on these nodes. The performance may be an issue in a large database, since you will not be able to leverage an index on these searches. Indexes are defined on a property for a specific label. To get around this and probably improve your data model, you can add a more generic label to these nodes that indicate what type of node these nodes are with labels A, B, and C are. For example, if the specific labels were Apple, Pear, and Banana, you could label all of these nodes with another label called Fruit. By doing so, you can achieve two things. One, you can search over all fruits earlier without regard to the type. Two, you can create an index on the name property for the Fruit label. Your match statements in your relationship import cypher can then match on Fruit nodes with the name value, which will use and index.

Hello @glilienfield , Thanks for your comment. I exploring possible solutions, where I need to run certain algorithms and visualise. For the visualisation part and NEuler experimenting is possible by node labels. Where I couldn't differentiate by node properties. I will understand more about it to explore possibilities.