Skip to content

Instantly share code, notes, and snippets.

@andreasronge
Created January 5, 2012 10:19
Show Gist options
  • Save andreasronge/1564598 to your computer and use it in GitHub Desktop.
Save andreasronge/1564598 to your computer and use it in GitHub Desktop.
recommendation example
recommend = Neo4j.query do
node(alice).where_not{|me| me > ':follows' > :user} > ':used_tag' > :tag < ':used_tag' < :user
ret(node(:user), count(:tag).desc(count(:tag)))
end.first[:user]
require 'rubygems'
require 'neo4j-core'
tx = Neo4j::Transaction.new
alice = Neo4j::Node.new(:name => 'Alice')
ted = Neo4j::Node.new(:name => 'Ted')
carol = Neo4j::Node.new(:name => 'Carol')
bob = Neo4j::Node.new(:name => 'Bob')
alice.outgoing(:follows) << ted
neo4j = Neo4j::Node.new(:name => '#neo4j')
rails = Neo4j::Node.new(:name => '#rails')
jruby = Neo4j::Node.new(:name => '#jruby')
rails.incoming(:used_tag) << alice << carol << bob
neo4j.incoming(:used_tag) << alice << ted << bob
jruby.incoming(:used_tag) << ted << carol << bob
tx.success
tx.finish
puts "query"
recommend = Neo4j.query do
node(alice).where_not{|me| me > ':follows' > :user} > ':used_tag' > :tag < ':used_tag' < :user
ret(node(:user), count(:tag).desc(count(:tag)))
end.first[:user]
puts "Recommend #{recommend[:name]}"
#puts "result.columns=#{result.columns.inspect}"
#x = result.to_a
#puts "X=#{x.size}"
#x.each do |r|
# puts "user name #{r[:'user.name']}"
#end
tags = alice.outgoing(:used_tags).to_a
my_followers = alice.outgoing(:follows).to_a
# find other users which also used the same tags as me but are not already my followers
other_users =alice.outgoing(:used_tags).incoming(:used_tags).depth(2).filter{|path| path.length == 2 && !my_followers.include?(path.end_node)}
# for all those users, find the user who has the maximum number of similar tags as I have
recommend = other_users.max_by{|friend| (friend.outgoing(:used_tags).to_a & tags).size }
(1) {"name":"Alice"}
(2) {"name":"Ted"}
(3) {"name":"Carol"}
(4) {"name":"Bob"}
(5) {"name":"#neo4j"}
(6) {"name":"#rails"}
(7) {"name":"#jruby"}
(1)-[:used_tag]->(6)
(4)-[:used_tag]->(6)
(3)-[:used_tag]->(6)
(1)-[:used_tag]->(5)
(2)-[:used_tag]->(5)
(4)-[:used_tag]->(5)
(3)-[:used_tag]->(7)
(2)-[:used_tag]->(7)
(4)-[:used_tag]->(7)
(1)-[:follows]->(2)
start alice=node(1) MATCH alice-[:used_tag]->(tag)<-[:used_tag]-user WHERE not(alice-[:follows]-user) RETURN user.name, count(tag) ORDER BY count(tag) DESC
http://console.neo4j.org/?init=%281%29%20{%22name%22%3A%22Alice%22}%0A%282%29%20{%22name%22%3A%22Ted%22}%0A%283%29%20{%22name%22%3A%22Carol%22}%0A%284%29%20{%22name%22%3A%22Bob%22}%0A%285%29%20{%22name%22%3A%22%23neo4j%22}%0A%286%29%20{%22name%22%3A%22%23rails%22}%0A%287%29%20{%22name%22%3A%22%23jruby%22}%0A%281%29-[%3Aused_tag]-%3E%286%29%0A%284%29-[%3Aused_tag]-%3E%286%29%0A%283%29-[%3Aused_tag]-%3E%286%29%0A%281%29-[%3Aused_tag]-%3E%285%29%0A%282%29-[%3Aused_tag]-%3E%285%29%0A%284%29-[%3Aused_tag]-%3E%285%29%0A%283%29-[%3Aused_tag]-%3E%287%29%0A%282%29-[%3Aused_tag]-%3E%287%29%0A%284%29-[%3Aused_tag]-%3E%287%29%0A%281%29-[%3Afollows]-%3E%282%29&query=start%20alice%3Dnode%281%29%20MATCH%20alice-[%3Aused_tag]-%3E%28tag%29%3C-[%3Aused_tag]-user%20WHERE%20not%28alice-[%3Afollows]-user%29%20RETURN%20user.name%2C%20count%28tag%29%20ORDER%20BY%20count%28tag%29%20DESC&version=&no_root=true
http://tinyurl.com/7zzad7t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment