neo4jnodesrelationshipcounting

Neo4j cypher queries, counting nodes and two way relationships


I’m trying to run a cypher query on nodes with two way relationships and would like to count each of those relationships as well.

Example:

Nodes: store + customer 
Relationships: sold + bought

In an ideal scenario, I’d want to see both relationships connecting the store and customer. However, there are cases when only the customer is reporting they bought from the store, and vice versa when only the store is reporting they sold to the customer, but the customer hasn’t verified that sale.

I’d like to return the following:

*** Clarification:

for example, it would be 10 stores with 7-10 customers each, and yes in Neo it would be two different arcs connecting:

store-customer r/ships

the goals is to look at the different reporting practices for each store, as some stores may say they sold when they haven't, and customers may say they bought items. How often do each of those scenarios occur?


Solution

  • It's as easy as this :

    Neo4j 4

    MATCH (n:Customer)
    RETURN 
    size((n)-[:BOUGHT|:SOLD]-()) AS bothRels,
    size((n)-[:BOUGHT]-()) AS boughtRels,
    size((n)-[:SOLD]-()) AS soldRels
    

    Neo4j 5

    RETURN 
    COUNT { (n)-[:BOUGHT|:SOLD]-() } AS bothRels,
    COUNT { (n)-[:BOUGHT]-() } AS boughtRels,
    COUNT { (n)-[:SOLD]-() } AS soldRels
    
    ╒══════════╤════════════╤══════════╕
    │"bothRels"│"boughtRels"│"soldRels"│
    ╞══════════╪════════════╪══════════╡
    │2         │1           │1         │
    └──────────┴────────────┴──────────┘