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.

Return only one of multiple relationships

kkra
Node Link

Hi everybody,

I am looking for a way to only get a single Relationship between two nodes for visualization in R using neo4r and visNetwork. Here´s a small example of my network:

CREATE (SUP0001: Supplier {name: 'Answer Security', lat: 34.155834, long: -119.202789})
CREATE(L0001:Location {lid: 'L0001', name:'Plant San Francisco', type: 'Production site', owner: 'Ownership Ltd.', lat: 37.773972, long: -122.431297})
CREATE (SUP0001)-[:DELIVERED_TO {ordernumber: 'SO0000001', material: 'SP0001', quantity: 1000, date: date('2019-01-01')}]->(L0001)
CREATE (SUP0001)-[:DELIVERED_TO {ordernumber: 'SO0000002', material: 'SP0001', quantity: 1000, date: date('2019-01-01')}]->(L0001)

My whole network (supply chain) is a little more complex with suppliers being able to deliver to suppliers as well as the locations delivering to the customers.
By showing all the deliveries the graph can get confusing to look at. For that reason, I just want to display only one relationship per two different nodes.

// RETURN unique relationship between to nodes ??
MATCH q=()-[d:DELIVERED_TO]->() RETURN q

All help is highly appreciated. Thanks in advance!

5 REPLIES 5

MuddyBootsCode
Graph Steward

Hey Welcome to the community! I think there are two ways for you to accomplish this:

  1. You can specify id's for two nodes that match the supplier, recipient nodes and that would return a single relationship
  2. Or you can limit the number of responses by depth, doing something like this:
MATCH p = ()<-[d:DELIVERED_TO*1]-() RETURN p

You can find info about variable depth queries here https://neo4j.com/docs/cypher-manual/current/clauses/match/#varlength-rels

Thanks for the response and the warm welcome!

I think my problem description was not clear enough.

I have a network of multiple suppliers, locations and customers which are all connected by their deliveries. There can be multiple deliveries between two nodes (e.g. SUP0001 delivers 10 times to L0001, the delivery date and odernumber are different).
But when visualising this network, having 10 relationsships between two nodes makes the network unnecceassrily complex (espacially if I want to visalize all suppliers, locations and customers). So I just want one relationship retuned, representing all the relationships between two distinct nodes.

I´m not sure if using depth can help me finding a solution for this problem.

MuddyBootsCode
Graph Steward

Then what you're asking about is how to model that data, not how to return it, correct? If I were you I'd have my deliveries as separate nodes and have those display to and from relationships i.e. from this distribution point, to this recipient and then you're not dealing with however many relationships between those two nodes. Then if you want to display a relationship between the suppliers and customers you could do that independently.

Something like this, where you could have as many orders as you want and it doesn't create multiple relationships of the same type over and over on each node.

My initial questions was how to return the data, but feedback to the data model is always welcome. Would your proposal also be more efficient? Is it not good to have multiple relationsships of the same type between two nodes?
My current data model looks like this:

So should I model the orders as nodes instead of relationships? So far I made good progress with this data model.

MuddyBootsCode
Graph Steward

One of the benefits of modeling them as nodes vs relationships is that you can then place index constraints on the nodes where as you cannot on relationships. I think you'll find it's easier to treat things like orders to be much easier and efficient as nodes.