0

I would like to query for the following subgraph in my Neo4J database:

(a)-->(b)-->(c)-->(d)
       |
       | -->(e)

Note: a, b, c, d, e are attribute values (non-unique values) for each of the nodes. There are thousands for these nodes with similar attribute values (a to e) but they are randomly connected to one another.

How can I write the Cyper query to specifically find the particular subgraph (akin to subgraph isomorphism problem) I seek and return (a)? I've tried the following Cyper query but other subgraphs pop up:

START n1=node:SomeIndex(AttrVal="a")
MATCH n1-[]->n2-[]->n3-[]->n4
WHERE n2.AttrVal="b" AND n3.AttrVal="c" and n4.AttrVal="d"
WITH  n1, n2
MATCH n2-[]->n5
WHERE n5.AttrVal="e"
RETURN n1

Am I using the WITH and 2nd MATCH clause wrongly?

Thanks!

4

1 回答 1

1

您可以使用逗号在单个匹配子句中组合多个路径:

START n1=node:SomeIndex(AttrVal="a")
MATCH n1-[]->n2-[]->n3-[]->n4, n2-[]->n5
WHERE n2.AttrVal="b" AND n3.AttrVal="c" and n4.AttrVal="d" and n5.attrVal='e'
RETURN n1

旁注1: 您还可以像这样重构语句:

START n1=node:SomeIndex(AttrVal="a"), n2=node:SomeIndex(AttrVal="b")
n3=node:SomeIndex(AttrVal="c"), n4=node:SomeIndex(AttrVal="d"),
n5=node:SomeIndex(AttrVal="e")
MATCH n1-[]->n2-[]->n3-[]->n4, n2-[]->n5
RETURN n1

根据图表的结构,第二个可能会更快。

旁注 2: 当您匹配任意关系类型时,n1-[]->n2您可以使用更短且更易读的符号:n1-->n2

于 2013-07-04T20:12:46.103 回答