Finding recursive loop among nodes how to avoid it

Hi Team ,

If you follow and execute these command .
In last command i see a product or self join in (Reliance Ltd and Bharat Forge)how to avoid it

Producer

Match (n) Detach Delete n

CREATE (Producer:Producer {name: 'Bengal Chemicals', location: 'Haldia , West Bengal'})
WITH range(1, 10) as ids
FOREACH (id in ids |
CREATE (Product:Product {id: id, name: 'Product ' + id}) )

MATCH (Product:Product), (Producer:Producer {name: 'Bengal Chemicals'})
CREATE (Producer)-[:Has_Below_Product_Line]->(Product)

match (n) -[r]-(m) Return n , r , m

---Subscriber 1 ----

CREATE (Reliance:Subscriber {name: 'Reliance Ltd', location: 'Jamnagar , Gujarat'})

MATCH (Product:Product ), (Reliance:Subscriber {name: 'Reliance Ltd'})
CREATE (Reliance)-[:SUBSCRIBED_TO]->(Product)

match (n) -[r]-(m) Return n , r , m

---Subscriber 2 ----

CREATE (Bharat:Subscriber {name: 'Bharat Forge', location: 'Pune , Maharashtra'})

MATCH (Product:Product ), (Bharat:Subscriber {name: 'Bharat Forge'})
CREATE (Bharat)-[:SUBSCRIBED_TO]->(Product)

match (n) -[r]-(m) Return n , r , m

----Subscriber also communicate to each other --

MATCH (Bharat:Subscriber ), (Reliance:Subscriber)
CREATE (Bharat)-[:Share_Finished_Product]->(Reliance)

Try this for the last code block:

MATCH (Bharat:Subscriber ), (Reliance:Subscriber)
where id(Bharat)<>id(Reliance)
CREATE (Bharat)-[:Share_Finished_Product]->(Reliance)

or

MATCH (Bharat:Subscriber ), (Reliance:Subscriber)
where Bharat<>Reliance
CREATE (Bharat)-[:Share_Finished_Product]->(Reliance)

This worked . I added one more subscriber 'OIL'

---Subscriber 3 ----

CREATE (OIL:Subscriber {name: 'Oil India', location: 'Mumbai , Maharashtra'})

MATCH (Product:Product ), (OIL:Subscriber {name: 'Oil India'})
CREATE (OIL)-[:SUBSCRIBED_TO]->(Product)

---Subscriber 4 Taking Input from Reliance and Oil and create new subscriber ----

CREATE (Export:Subscriber {name: 'Export Petroluem to Shell ', location: 'Dallas , Texas'})

Below two relationship created me with 12 node and 12 relationship OIL and Reliance to Export

My expecation is only to establish one way relationship from

MATCH (Export:Subscriber ), (Reliance:Subscriber)
where Export<>Reliance
CREATE (Reliance)-[:Export_Overseas]->(Export)

MATCH (OIL:Subscriber ), (Reliance:Subscriber)
where OIL<>Reliance
CREATE (OIL)-[:Export_Overseas]->(Export)

How to accomplish

Try this:

MATCH (Export:Subscriber ), (Reliance:Subscriber)
where id(Export)<id(Reliance)
CREATE (Reliance)-[:Export_Overseas]->(Export)

MATCH (OIL:Subscriber ), (Reliance:Subscriber)
where id(OIL)<id(Reliance)
CREATE (OIL)-[:Export_Overseas]->(Export)

MATCH (Export:Subscriber ), (Reliance:Subscriber)
where id(Export)<id(Reliance)
CREATE (Reliance)-[:Export_Overseas]->(Export)

Ideally it should create a relationship from Reliance to Export . (to and fro )

However this statement relationship amond... OIL , EXPORT AND Reliance nodes ( created 6 relationship and 6 node )

I only want one way relationship between Export and Reliance

The issue is you have no distinction identifying which node is Export and which is Reliance, as both have the same label and there are no property conditions in the match clause. As such, your query is getting all Subscriber nodes and forming rows of results that is the Cartesian product of the all Subscriber nodes.

The condition I added removes half of the Cartesian product results, so you will not get relationships in both directions.

I am not following what your business model is, but it seem to not carry much information if every node had a relationship to every other node with the same label and the relationship had no properties.