Skip to content

MATCH clause

Marijn van Wezel edited this page Dec 13, 2022 · 8 revisions

The MATCH clause is used to search for the pattern described in it. It accepts a list of patterns to match on.

Query::match(CompletePattern|CompletePattern[] $patterns): Query

Parameters

  • $patterns : A single pattern to match, or a non-empty list of patterns to match.

Relevant methods

  • addPattern(CompletePattern ...$pattern): self : Add one or more patterns.

Examples

Get all nodes

$n = node();
$query = query()
    ->match($n)
    ->returning($n)
    ->build();

$this->assertStringMatchesFormat("MATCH (%s) RETURN %s", $query);

Get all nodes with a label

$movie = node("Movie");
$query = query()
    ->match($movie)
    ->returning($movie->property("title"))
    ->build();

$this->assertStringMatchesFormat("MATCH (%s:Movie) RETURN %s.title", $query);

Related nodes

$movie = node();
$query = query()
    ->match(node()->withProperties(['name' => 'Oliver Stone'])->relationshipUni($movie))
    ->returning($movie->property("title"))
    ->build();

$this->assertStringMatchesFormat("MATCH ({name: 'Oliver Stone'})--(%s) RETURN %s.title", $query);

Match with labels

$movie = node('Movie');
$query = query()
    ->match(node('Person')->withProperties(['name' => 'Oliver Stone'])->relationshipUni($movie))
    ->returning($movie->property("title"))
    ->build();

$this->assertStringMatchesFormat("MATCH (:Person {name: 'Oliver Stone'})--(%s:Movie) RETURN %s.title", $query);

Match with a label expression for the node labels

The more complex label expression syntax added in Neo4j 5.0 is not yet supported.

$n = node()->addLabel('Movie', 'Person');
$query = query()
    ->match($n)
    ->returning(['name' => $n->property("name"), 'title' => $n->property("title")])
    ->build();

$this->assertStringMatchesFormat("MATCH (%s:Movie:Person) RETURN %s.name AS name, %s.title AS title", $query);

Outgoing relationships

$movie = node();
$query = query()
    ->match(
        node('Person')->withProperties(['name' => 'Oliver Stone'])->relationshipTo($movie)
    )
    ->returning($movie->property('title'))
    ->build();

$this->assertStringMatchesFormat("MATCH (:Person {name: 'Oliver Stone'})-->(%s) RETURN %s.title", $query);

Directed relationships with variable

$r = relationshipTo();
$query = query()
    ->match(node('Person')->withProperties(['name' => 'Oliver Stone'])->relationship($r, node()))
    ->returning($r)
    ->build();

$this->assertStringMatchesFormat("MATCH (:Person {name: 'Oliver Stone'})-[%s]->() RETURN %s", $query);

Match on relationship type

$actor = node();
$query = query()
    ->match(
        node('Movie')
            ->withProperties(['title' => 'Wall Street'])
            ->relationshipFrom($actor, 'ACTED_IN')
    )
    ->returning($actor->property('name'))
    ->build();

$this->assertStringMatchesFormat("MATCH (:Movie {title: 'Wall Street'})<-[:ACTED_IN]-(%s) RETURN %s.name", $query);

Match on multiple relationship types

$person = node();
$relationship = relationshipFrom()->withTypes(['ACTED_IN', 'DIRECTED']);
$query = query()
    ->match(
        node('Movie')
            ->withProperties(['title' => 'Wall Street'])
            ->relationship($relationship, $person)
    )
    ->returning($person->property('name'))
    ->build();

$this->assertStringMatchesFormat("MATCH (:Movie {title: 'Wall Street'})<-[:ACTED_IN|DIRECTED]-(%s) RETURN %s.name", $query);

Match on relationship type and use a variable

$actor = node();
$relationship = relationshipFrom()->withTypes(['ACTED_IN']);
$query = query()
    ->match(
        node('Movie')
            ->withProperties(['title' => 'Wall Street'])
            ->relationship($relationship, $actor)
    )
    ->returning($relationship->property('role'))
    ->build();

$this->assertStringMatchesFormat("MATCH (:Movie {title: 'Wall Street'})<-[%s:ACTED_IN]-() RETURN %s.role", $query);

Relationship types with uncommon characters

$charlie = node('Person')->withProperties(['name' => 'Charlie Sheen']);
$rob = node('Person')->withProperties(['name' => 'Rob Reiner']);

$query = query()
    ->match([$charlie, $rob])
    ->create(
        node()
            ->withVariable($rob->getVariable())
            ->relationshipTo(
                node()->withVariable($charlie->getVariable()),
                'TYPE INCLUDING A SPACE'
            )
    )
    ->build();

$this->assertStringMatchesFormat("MATCH (%s:Person {name: 'Charlie Sheen'}), (%s:Person {name: 'Rob Reiner'}) CREATE (%s)-[:`TYPE INCLUDING A SPACE`]->(%s)", $query);

Multiple relationships

$movie = node();
$director = node();

$query = query()
    ->match(
        node()
            ->withProperties(['name' => 'Charlie Sheen'])
            ->relationshipTo($movie, 'ACTED_IN')
            ->relationshipFrom($director, 'DIRECTED')
    )
    ->returning([$movie->property('title'), $director->property('name')])
    ->build();

$this->assertStringMatchesFormat("MATCH ({name: 'Charlie Sheen'})-[:ACTED_IN]->(%s)<-[:DIRECTED]-(%s) RETURN %s.title, %s.name", $query);

Variable length relationships

$movie = node('Movie');
$r = relationshipUni()->addType('ACTED_IN')->withMinHops(1)->withMaxHops(3);

$query = query()
    ->match(node()->withProperties(['name' => 'Charlie Sheen'])->relationship($r, $movie))
    ->returning($movie->property('title'))
    ->build();

$this->assertStringMatchesFormat("MATCH ({name: 'Charlie Sheen'})-[:ACTED_IN*1..3]-(%s:Movie) RETURN %s.title", $query);

Named paths

$p = node()->withProperties(['name' => 'Michael Douglas'])->relationshipTo(node());

$query = query()
    ->match($p)
    ->returning($p)
    ->build();

$this->assertStringMatchesFormat("MATCH %s = ({name: 'Michael Douglas'})-->() RETURN %s", $query);

External links