Databases Reference
In-Depth Information
Figure 5-2. Pattern to find colleagues who share a user's interests
The query works as follows:
•
START
looks up the subject of the query in the
user
index, and assigns the result to
the
subject
identifier.
•
MATCH
then matches this
person
with people who work for the same company, and
who share one or more of their interests. If the subject of the query is Sarah, who
works for Acme, then in the case of Ben,
MATCH
will match twice:
('Acme')<-
[:WORKS_FOR]-('Ben')-[:INTERESTED_IN]->('graphs')
and
('Acme')<-
[:WORKS_FOR]-('Ben')-[:INTERESTED_IN]->('REST')
. In the case of Charlie, it
will match once:
('Acme')<-[:WORKS_FOR]-('Charlie')-[:INTERESTED_IN]-
>('graphs')
.
•
RETURN
creates a projection of the matched data: for each matched colleague, we
extract their name, count the number of interests they have in common with the
subject of the query (aliasing this result as
score
), and, using
collect
, create a
comma-separated list of these mutual interests. Where a person has multiple
matches, as does Ben in our example,
count
and
collect
aggregate their matches
into a single row in the returned results (in fact, both
count
and
collect
can per‐
form this aggregating function independently of one another).
• Finally, we order the results based on each colleague's
score
, highest first.