-
Notifications
You must be signed in to change notification settings - Fork 5
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
-
$patterns
: A single pattern to match, or a non-empty list of patterns to match.
-
addPattern(CompletePattern ...$pattern): self
: Add one or more patterns.
$n = node();
$query = query()
->match($n)
->returning($n)
->build();
$this->assertStringMatchesFormat("MATCH (%s) RETURN %s", $query);
$movie = node("Movie");
$query = query()
->match($movie)
->returning($movie->property("title"))
->build();
$this->assertStringMatchesFormat("MATCH (%s:Movie) RETURN %s.title", $query);
$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);
$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);
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);
$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);
$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);
$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);
$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);
$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);
$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);
$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);
$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);
$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);