Search code examples
neo4jcyphergraph-databases

Excluding "symmetric" results in Neo4j


I want to query a Neo4j graph for a structure that includes two interchangeable nodes, but I don't want two unique responses for each of the "symmetric" responses.

How do I express in Cypher that two nodes are interchangeable?

An example:

I want to look for the following structure in the graph with the following query:

MATCH (c:Customer)-[]->(p:Purchase)
MATCH (c:Customer)-[]->(q:Purchase)
MATCH (p)-[]->(m:Company)
MATCH (q)-[]->(m:Company)
RETURN DISTINCT c, p, q, m

the query

The default behavior would be for Neo4j to return the following two graphs:

A symmetric graph Another symmetric graph

(i.e. The assignment of p and q to Purchase1 and Purchase2 are reversed)

How do I express that the elements p and q in my query are interchangeable, and I only need one of the above responses?


Solution

  • To prevent those kinds of results, you would typically have an inequality based on the node ids:

    WHERE id(p) < id(q)

    That said, you may be able to form this query a little cleaner like this (provided you want all purchases between a customer and a company with at least two purchases made from that customer to the company):

    MATCH (c:Customer)-->(p:Purchase)-->(m:Company)
    WITH c, m, collect(p) as purchases, count(p) as purchaseCount
    WHERE purchaseCount >= 2
    RETURN c, m, purchases