Sasi
Sasi

Reputation: 21

Combining multiple nodes into one and map all the relations to that node neo4j

I have a sellers node and buyers node with the same business name.

When I try to match using the code:

MATCH (p:Sellers)-[rel:sells]->(o:Buyers)

RETURN p, rel, o; 

It returns the graph : 01AAAC is present twice(As a seller and a buyer) in this graph

and the data :

p,rel,o

{"Seller":01AAAC}","{""invoices":5}","{"Buyer":03AAAG}

{"Seller":01AAXP}","{"invoices":8}","{"Buyer":01AAAC}

{"Seller":27AAFF}","{"invoices":2}","{"Buyer":01AAAC}

But I'm trying to create rather a graph like this :

01AAAC is present only once in this graph

Can you kindly help me with an appropriate cypher query to create the above visualisation. Thanks a lot in advance.

Upvotes: 0

Views: 542

Answers (2)

Marj
Marj

Reputation: 485

I've given this a bit more thought and ideally, your model wouldn't differentiate between :Buyer and :Seller nodes, you'd just have one node label, say :Entity which sells to another node of the same label.

Single node type Entity

However, we live in the real world and you may need to label nodes as :Buyer and :Seller, in which case, your only option would be to add a :Seller label to any :Buyers that sell and a :Buyer label to any :Sellers that buy. This would mean you end up with some nodes having two labels, one for each status they can be in, but it would enable you to visualise what you want to: Node with two labels

As you can see, the highlighted node has two labels.

Unfortunately, you will not be able to generate this representation from the model you have as your :Buyer node 01AAAC is a completely different node to :Seller 01AAAC and they have no shared relationship between them.

Upvotes: 1

cechode
cechode

Reputation: 1022

if you remove the label constraint from your nodes, you should get all nodes and relationships between any two nodes that are have a sells relationship

MATCH (a)-[:sells]-(b)
return *

Upvotes: 0

Related Questions