10.4. Start

10.4.1. Get node or relationship from index
10.4.2. Get node or relationship by id
10.4.3. Get multiple or all nodes

Every query describes a pattern, and in that pattern one can have multiple starting points. A starting point is a relationship or a node where a pattern is anchored. You can either introduce starting points by id, or by index lookups. Note that trying to use an index that doesn’t exist will generate an error.

[Note]Note

START is optional. If you do not specify explicit starting points, Cypher will try and infer starting points from your query. This is done based on node labels and predicates contained in your query. See Chapter 13, Schema for more information. In general, the START clause is only really needed when using legacy indexes.

This is the graph the examples are using:

Figure 10.4. Graph


10.4.1. Get node or relationship from index

Node by index lookup

When the starting point can be found by using index lookups, it can be done like this: node:index-name(key = "value"). In this example, there exists a node index named nodes.

Query. 

START n=node:nodes(name = "A")
RETURN n

The query returns the node indexed with the name "A".

Result

n
1 row

Node[0]{name:"A"}


Relationship by index lookup

When the starting point can be found by using index lookups, it can be done like this: relationship:index-name(key = "value").

Query. 

START r=relationship:rels(name = "Andrés")
RETURN r

The relationship indexed with the name property set to "Andrés" is returned by the query.

Result

r
1 row

:KNOWS[0]{name:"Andrés"


Node by index query

When the starting point can be found by more complex Lucene queries, this is the syntax to use: node:index-name("query").This allows you to write more advanced index queries.

Query. 

START n=node:nodes("name:A")
RETURN n

The node indexed with name "A" is returned by the query.

Result

n
1 row

Node[0]{name:"A"}


10.4.2. Get node or relationship by id

Node by id

Binding a node as a starting point is done with the node(*) function.

[Note]Note

Neo4j reuses its internal ids when nodes and relationships are deleted, which means it’s bad practice to refer to them this way. Instead, use application generated ids.

Query. 

START n=node(0)
RETURN n

The corresponding node is returned.

Result

n
1 row

Node[0]{name:"A"}


Try this query live. create (_0 {`name`:"A"}) create (_1 {`name`:"B"}) create (_2 {`name`:"C"}) create _0-[:`KNOWS`]->_1 create _0-[:`KNOWS`]->_2 start n=node(0) return n

Relationship by id

Binding a relationship as a starting point is done with the relationship(*) function, which can also be abbreviated rel(*). See the section called “Node by id” for more information on Neo4j ids.

Query. 

START r=relationship(0)
RETURN r

The relationship with id 0 is returned.

Result

r
1 row

:KNOWS[0]{}


Try this query live. create (_0 {`name`:"A"}) create (_1 {`name`:"B"}) create (_2 {`name`:"C"}) create _0-[:`KNOWS`]->_1 create _0-[:`KNOWS`]->_2 start r=relationship(0) return r

Multiple nodes by id

Multiple nodes are selected by listing them separated by commas.

Query. 

START n=node(0, 1, 2)
RETURN n

This returns the nodes listed in the START statement.

Result

n
3 rows

Node[0]{name:"A"}

Node[1]{name:"B"}

Node[2]{name:"C"}


Try this query live. create (_0 {`name`:"A"}) create (_1 {`name`:"B"}) create (_2 {`name`:"C"}) create _0-[:`KNOWS`]->_1 create _0-[:`KNOWS`]->_2 start n=node(0, 1, 2) return n

10.4.3. Get multiple or all nodes

All nodes

To get all the nodes, use an asterisk. This can be done with relationships as well.

[Tip]Tip

The preferred way to do this is to use a MATCH clause, see the section called “Get all nodes” in Section 10.1, “Match” for how to do that.

Query. 

START n=node(*)
RETURN n

This query returns all the nodes in the graph.

Result

n
3 rows

Node[0]{name:"A"}

Node[1]{name:"B"}

Node[2]{name:"C"}


Try this query live. create (_0 {`name`:"A"}) create (_1 {`name`:"B"}) create (_2 {`name`:"C"}) create _0-[:`KNOWS`]->_1 create _0-[:`KNOWS`]->_2 start n=node(*) return n

Multiple starting points

Sometimes you want to bind multiple starting points. Just list them separated by commas.

Query. 

START a=node(0), b=node(1)
RETURN a,b

Both the nodes A and the B are returned.

Result

ab
1 row

Node[0]{name:"A"}

Node[1]{name:"B"}


Try this query live. create (_0 {`name`:"A"}) create (_1 {`name`:"B"}) create (_2 {`name`:"C"}) create _0-[:`KNOWS`]->_1 create _0-[:`KNOWS`]->_2 start a=node(0), b=node(1) return a,b