newBike
newBike

Reputation: 15002

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

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))

Upvotes: 0

Views: 314

Answers (1)

Christophe Willemsen
Christophe Willemsen

Reputation: 20185

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

Upvotes: 1

Related Questions