Search code examples
neo4jcypherpy2neo

How do I calculate all the relationship properties with py2neo or cypher query language


The model is for recording all the browse history

How could I summation/average/find_max/find_min for all the pages browse to page 2

The expected answers are

  • summation all the browsed time to page2 : 100+200+300+500
  • find_max browse time to page2 : 500
  • find_min browse time to page2 : 100
  • average browse time from page1 to page2 : 100

Could Anyone give me some queries to achieve the above questions.

The cypher query syntax is really pianful for me.

The code for creating nodes and relationhsips

page1 = Node("Page", name="page1")
page2 = Node("Page", name="page2")
page3 = Node("Page", name="page3")
graph_db.create(neo4j.Path(page1, ("LINKS_TO", {"browsed_time": 200}), page2))
graph_db.create(neo4j.Path(page1, ("LINKS_TO", {"browsed_time": 100}), page2))
graph_db.create(neo4j.Path(page1, ("LINKS_TO", {"browsed_time": 300}), page2))
graph_db.create(neo4j.Path(page3, ("LINKS_TO", {"browsed_time": 500}), page2))

Solution

  • A simple example, you'll need to adapt to match exactly your needs, test console here : http://console.neo4j.org/?id=rs4ado

    Example 1: Summing all the relationships properties keyed "time" :

    MATCH (p:Page { id:2 })<-[r:LINKS_TO]-(referer)
    RETURN sum(r.time) AS totalTime
    

    For average, replace sum by avg, same for min and max.

    Example 2: If you need to get the page node that is linked to Page2, here get the one with the most time, so sort relationship time property in descending order

    MATCH (p:Page { id:2 })
    MATCH (p)<-[r:LINKS_TO]-(referer)
    WITH r
    ORDER BY r.time DESC 
    LIMIT 1
    RETURN startNode(r), r.time