0

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))
newBike
  • 14,385
  • 29
  • 109
  • 192
  • SO is not a code writing service. Try to solve the problem on your own and if you experience any difficulties, post it on here. – sgp Jun 02 '15 at 09:25

1 Answers1

1

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
Christophe Willemsen
  • 19,399
  • 2
  • 29
  • 36