The document discusses building graph queries in layers from basic graph operations to complex compositions:
- Layer 1 covers basic graph foundations like moving through a graph and filtering vertices and edges.
- Layer 2 introduces vertex and edge types to build domain-specific movements.
- Layer 3 chains these together into meaningful queries over the domain.
- Layer 4 composes queries to extract deeper meanings through patterns over multiple hops.
- Layer 5 shows how repeating patterns can extract variable-length structures from the graph.
The approach isolates logic from the schema, allowing complex queries to be built from simple reusable components.
1 of 37
Downloaded 10 times
More Related Content
Abstracting with pacer
1. 息 All Rights Reserved 2013 | xnlogic.com
Abstracting
7. 息 All Rights Reserved 2013 | xnlogic.com
How would you query Twitter?
Find conversations that are relevant to me, happened recently and were popular.
70. Lookaheads
Person followed by at least 1000 people
def popular_only
lookahead(min: 1000) { |person| person.followed_by }
end
Tweet retweeted at least 100 times
def impactful_only
lookahead(min: 100) { |tweet| tweet.retweets }
end
75. Lookaheads
Have these people tweeted recently?
def active_only
lookahead { |person| person.tweets.recent_only }
end
Reversing a 鍖lter (lookahead Inception!)
def inactive_only
neg_lookahead { |person| person.active_only }
end
81. 油珂艶温稼庄稼乙
A shy twitterer retweets and stars stuff, follows people but never tweets:
def shy_people_only
has_retweeted.
has_starred.
is_following.
neg_lookahead(has_tweeted)
end
82. 息 All Rights Reserved 2013 | xnlogic.com
Reality
86. Itself
We are marking an element and circling back to it
def relevant_conversation_tweets
as(:me).following.tweets.lookahead do |tweet|
tweet.responded_to.authors.followed_by.is(:me)
end
end
This pattern is incredibly powerful!
87. 息 All Rights Reserved 2013 | xnlogic.com
Exercise:
92. Code?
How did we build such a sophisticated query out of such simple pieces?
def recent_popular_relevant_conversations!
recent_only.popular_only.relevant_conversation_tweets
end
93. 息 All Rights Reserved 2013 | xnlogic.com
These methods are extremely simple.
Q: Why even bother with them?
A: Names, names, names
94. 息 All Rights Reserved 2013 | xnlogic.com
Youve just isolated your logic from the schema.
Most of your business logic is at Layer 4+.
Even major schema changes rarely impact beyond Layer 3.
103. Length
The tweet this responded to, then the tweet that it responded to, etc, etc...
def conversations
all { |tweet| tweet.responded_to }
end
Any pattern can be repeated, no matter how complex
def popular_conversations
all { |tweet| tweet.responded_to.popular_only }
end
104. 息 All Rights Reserved 2013 | xnlogic.com
Ignoring
109. 油遺看馨恢庄稼温岳庄看稼s
A lookahead with a loop with a lookahead, emerging with a loop.
def conversations_with_a_recent_popular_exchange
lookahead do |tweet|
tweet.popular_conversations.recent_only
end.conversations
end
110. 息 All Rights Reserved 2013 | xnlogic.com
Specialized
112. Logic
Make common operations simple, uncommon operations possible...
def rapid_conversations
loop do |tweet|
tweet.responded_to
end.while do |tweet, depth, path|
if depth = 1
prev_tweet = path[-2]
minutes = tweet.minutes_after(prev_tweet)
if minutes 2
:loop_and_emit
if minutes 5
:loop
end
else
:loop
end
end
end
113. 息 All Rights Reserved 2013 | xnlogic.com
Sometimes the data is like this.
Sometimes the data is like that...
In the real world, data isnt so clean. Weve got you covered.
114. 息 All Rights Reserved 2013 | xnlogic.com
Branching
116. Merging
Youve got the power to encode alternate routes
def good_tweets
branch do |person|
person.starred_tweets
end.branch do |person|
person.tweets.retweeted
end
end
Thats often all it takes to handle messy data from multiple sources.