Databases Reference
In-Depth Information
The starting locations—the anchor points in the real graph, to which
some parts of the pattern are bound—are discovered in one of two ways.
The most common method is to use an index. Neo4j uses indexes as
naming services; that is, as ways of finding starting locations based on
one or more indexed property values.
Like most query languages, Cypher is composed of clauses. The simplest queries consist
of a START clause followed by a MATCH and a RETURN clause (we'll describe the other
clauses you can use in a Cypher query later in this chapter). Here's an example of a
Cypher query that uses these three clauses to find the mutual friends of user named
Michael :
START a= node :user(name= 'Michael' )
MATCH (a)-[:KNOWS]->(b)-[:KNOWS]->(c), (a)-[:KNOWS]->(c)
RETURN b, c
Let's look at each clause in more detail.
START
START specifies one or more starting points—nodes or relationships—in the graph.
These starting points are obtained via index lookups or, more rarely, accessed directly
based on node or relationship IDs.
In the example query, we're looking up a start node in an index called user . We ask the
index to find a node with a name property whose value is Michael . The return value
from this lookup is bound to an identifier , which we've here called a . This identifier
allows us to refer to this starting node throughout the rest of the query.
MATCH
This is the specification by example part. Using ASCII characters to represent nodes and
relationships, we draw the data we're interested in. We use parentheses to draw nodes,
and pairs of dashes and greater-than and less-than signs to draw relationships ( --> and
<-- ). The < and > signs indicate relationship direction. Between the dashes, set off by
square brackets and prefixed by a colon, we put the relationship name.
At the heart of our example query is the simple pattern (a)-[:KNOWS]->(b)-[:KNOWS]-
>(c), (a)-[:KNOWS]->(c) . This pattern describes a path comprising three nodes, one
of which we've bound to the identifier a , the others to b and c . These nodes are connected
by way of several KNOWS relationships, as per Figure 3-1 .
This pattern could, in theory, occur many times throughout our graph data; with a large
user set, there may be many mutual relationships corresponding to this pattern. To
localize the query, we need to anchor some part of it to one or more places in the graph.
Search WWH ::




Custom Search