Ch HaXam
Ch HaXam

Reputation: 499

how to count the repetition of the relationships in neo4j

I am trying to calculate the number of relationships between two nodes which are repeated more then one time. As i am using MERGE clause . So, is there any possibility to calculate if the relationships repeated more than one time. Graphically i can not see because while using the MERGE clause all relationships merge if the nodes and relations are the same. But in my data repetitions does exist. For example, if node A calls node B and this call relationship is more then 10 times. when i will use the MERGE clause it will appear only one time which is good to avoid the repetitions in graph. But can i count this repetitions with cypher query or not? Thanks in advance

Upvotes: 1

Views: 2405

Answers (4)

Dave Cole
Dave Cole

Reputation: 558

If you want to see the results graphically...

MATCH p=(a)-[r]->(b)
WITH b, collect(p) AS pl
WHERE size(pl) > 1
UNWIND pl AS p
RETURN p

Upvotes: 0

ErnestoE
ErnestoE

Reputation: 1304

I use variations of the following in order to review duplicate relationships:

MATCH
  (a)-[r]->(b)
WITH
  id(a) AS id_a, id(b) AS id_b, type(r) AS type_r, count(*) AS count_r
WHERE
  count_r > 1
RETURN
  type_r, count_r
LIMIT 10

you need to be careful as it is also possible duplicates are valid as they might contain different property values contained within them. However as you've explained you are using MERGE when creating, you should not be getting duplicates. Can you make sure that you are using the right MERGE statement, which requires the relationship to be merged independently of any new or changed values in the nodes belonging in the relationship. You might want to post examples of your code in order to review it properly.

Upvotes: 0

Nadine
Nadine

Reputation: 1638

If you want to get the repeated relationships, you can use this query:

MATCH (a)-[r]->(b)
WITH 
    a, b, type(r) as typer, count(r) as countr
WHERE 
    countr>1
RETURN
    a, b, typer, countr

For example, if you had this data:

John - calls -> Sally

John - calls -> Sally

Sarah - calls -> Tom

Amy - calls -> Lily

Amy - calls -> Lily

Amy - calls -> Lily

The query would return:

a        b         typer        countr
John     Sally     calls     2 
Amy      Lily      calls     3

Upvotes: 1

Luanne
Luanne

Reputation: 19373

If you have (a)-[:CALLS]->(b) then the way to count the number of CALLS relationships between a and b would be

MATCH (a)-[r:CALLS]->(b) 
RETURN COUNT(r)

However, you also say that you're using MERGE which should only create one CALLS relation between a and b.

If you're sure that there are multiple CALLS relationships between a and b then either the usage of MERGE is incorrect, or these relationships existed earlier before you started using MERGE.

Either way, the Neo4j browser (post 2.2) will show you nice curved relationships between a and b if more than one exist, and the query above will confirm that fact.

Upvotes: 1

Related Questions