Match

本页描述了如何使用 Cypher®.Match 类创建 MATCH 子句。

要添加 MATCH 子句,首先使用 Pattern 类创建一个有效的模式,并将其传递给 Match 构造函数。

const movie = new Cypher.Node();
const pattern = new Cypher.Pattern(movie, { labels: ["Movie"] });

const matchQuery = new Cypher.Match(pattern);
const { cypher, params } = matchQuery.build()

这将生成以下 MATCH 子句

MATCH (this:Movie)

随后,可以添加其他子句。例如

const matchQuery = new Cypher.Match(pattern)
    .where(Cypher.eq(movie.property("name"), new Cypher.Param("my-movie")))
    .return(movie);

您也可以链式添加额外的 MATCH 子句

const matchQuery = new Cypher.Match(pattern).match(pattern2).return(movie)

关系

您可以通过创建相关的 Pattern 在 MATCH 子句中使用关系。

const actedInPattern = new Cypher.Pattern(personNode, { labels: ["Person"] })
    .related({ type: "ACTED_IN" })
    .to(movieNode, { labels: ["Movie"] });

const matchQuery = new Cypher.Match(actedInPattern)
MATCH (this0:Person)-[:ACTED_IN]->(this1:Movie)

多个模式

您可以在单个 MATCH 子句中匹配多个模式,只需将多个模式传递给 Match 构造函数。

const actor = new Cypher.Node();
const moreActors = new Cypher.Node();
const movie = new Cypher.Node();

const pattern1 = new Cypher.Pattern(actor, { labels: ["Person"] })
    .related({ type: "ACTED_IN", direction: "undirected" })
    .to(movie, { labels: ["Movie"] });

const pattern2 = new Cypher.Pattern(moreActors, { labels: ["Person"] })
    .related({ type: "ACTED_IN", direction: "undirected" })
    .to(movie);

const matchQuery = new Cypher.Match(pattern1, pattern2).return(actor, moreActors, movie);
MATCH
  (this0:Person)-[:ACTED_IN]-(this1:Movie),
  (this2:Person)-[:ACTED_IN]-(this1)
RETURN this0, this2, this1

使用 WHERE 进行过滤

可以使用 .where 方法附加 WHERE 子句,该方法接受任意谓词。

new Cypher.Match(new Cypher.Pattern(movieNode, { labels: ["Movie"] }))
   .where(Cypher.eq(node.property("title"), new Cypher.Param("Matrix")));
MATCH (this0:Movie)
WHERE title = $param0

您可以在 .where 之后使用 .and.where 方法来链式连接多个谓词。

new Cypher.Match(new Cypher.Pattern(movieNode, { labels: ["Movie"] }))
   .where(Cypher.eq(node.property("title"), new Cypher.Param("Matrix")))
   .and(Cypher.neq(node.property("year"), new Cypher.Param(2001)));
MATCH (this0:Movie)
WHERE this0.title = $param0
AND this0.year <> $param1

逻辑过滤器

对于更复杂的逻辑过滤器,请在 where 中使用 Cypher.andCypher.orCypher.notCypher.xor 谓词。

new Cypher.Match(new Cypher.Pattern(movieNode, { labels: ["Movie"] }))
    .where(
        Cypher.and(
            Cypher.or(Cypher.eq(movieNode.property("title"), new Cypher.Param("Matrix")), Cypher.gt(movieNode.property("year"), new Cypher.Param(1990))),
            Cypher.neq(movieNode.property("year"), new Cypher.Param(2001))
        )
    )
MATCH (this0:Movie)
WHERE ((this0.title = $param0 OR this0.year > $param1) AND this0.year <> $param2)

属性过滤简写

.where 还支持传入变量以及包含变量的对象,以更简洁的方式检查多个值的相等性。

const matchQuery = new Cypher.Match(new Cypher.Pattern(movieNode, { labels: ["Movie"] }))
    .where(movieNode, { id: idParam, name: nameParam })
MATCH (this0:Movie)
WHERE (this0.id = $param0 AND this0.name = $param1)

可选匹配

您可以使用 Cypher.OptionalMatch 类,类似于 MATCH,创建 OPTIONAL MATCH 子句。

const movie = new Cypher.Node();
const pattern = new Cypher.Pattern(movie, { labels: ["Movie"] });

const matchQuery = new Cypher.OptionalMatch(pattern);

或者,您可以使用 .optional() 方法将现有的 Match 实例转换为 OptionalMatch

const movie = new Cypher.Node();
const pattern = new Cypher.Pattern(movie, { labels: ["Movie"] });

const matchQuery = new Cypher.Match(pattern).optional();

最短路径

您可以在 MATCH 子句中使用 Cypher.Match 的以下方法添加 最短路径 及其变体。

  • .shortest(k)

  • .shortestGroups(k)

  • .allShortest

  • .any

例如:

const movieNode = new Cypher.Node();

const matchQuery = new Cypher.Match(
    new Cypher.Pattern(movieNode, {
        labels: ["Movie"]
    })
        .related()
        .to(new Cypher.Node(), {
            labels: ["Person"],
        })
)
    .shortestGroups(2)
    .return(movieNode);
MATCH SHORTEST 2 GROUPS (this0:Movie)-[this1]->(this2:Person)
RETURN this0
© . This site is unofficial and not affiliated with Neo4j, Inc.