如何使用OR匹配节点标签?

时间:2018-04-27 16:33:57

标签: neo4j cypher

match (p:Product {id:'5116003'})-[r]->(o:Attributes|ExtraAttribute) return p, o

如何在这样的查询中匹配两个可能的节点标签?

根据cybersam的建议,我改为以下:

MATCH (p:Product {id:'5116003'})-[r]->(o) 
WHERE o:Attributes OR o:ExtraAttributes
**WHERE any(key in keys(o) WHERE toLower(key) contains 'weight')** 
return o

现在我需要添加第二个'where'子句。如何修改?

2 个答案:

答案 0 :(得分:1)

您可以尝试使用any()功能:

match (p:Product {id:'5116003'})-[r]->(o)
where any (label in labels(o) where label in ['Attributes', 'ExtraAttribute'])
return p, o

此外,如果您有APOC procedures,则可以使用apoc.path.expand路径扩展程序,该程序会根据从最小到最大级别的给定关系从开始节点扩展到标签过滤器。

match (p:Product {id:'5116003'})
call apoc.path.expand(p, null,"+Attributes|ExtraAttribute",0,1) yield path
with nodes(path) as nodes
// return p and o nodes
return nodes[0], nodes[1]

See more here

答案 1 :(得分:0)

您查询的这两种单标签形式:

MATCH (p:Product {id:'5116003'})-->(o:Attributes) RETURN p, o;

MATCH (p:Product {id:'5116003'})-->(o) WHERE o:Attributes RETURN p, o;

生成相同的执行计划,如下所示(我假设:Product(id)上有索引):

+-----------------+----------------+------+---------+------------------+--------------+
| Operator        | Estimated Rows | Rows | DB Hits | Variables        | Other        |
+-----------------+----------------+------+---------+------------------+--------------+
| +ProduceResults |              0 |    0 |       0 | o, p             | p, o         |
| |               +----------------+------+---------+------------------+--------------+
| +Filter         |              0 |    0 |       0 | anon[33], o, p   | o:Attributes |
| |               +----------------+------+---------+------------------+--------------+
| +Expand(All)    |              0 |    0 |       0 | anon[33], o -- p | (p)-->(o)    |
| |               +----------------+------+---------+------------------+--------------+
| +NodeIndexSeek  |              0 |    0 |       1 | p                | :Product(id) |
+-----------------+----------------+------+---------+------------------+--------------+

上面第二个查询的这种双标签形式:

MATCH (p:Product {id:'5116003'})-->(o) WHERE o:Attributes OR o: ExtraAttribute RETURN p, o;

生成一个非常相似的执行计划(因此可能不会更昂贵):

+-----------------+----------------+------+---------+------------------+-------------------------------------+
| Operator        | Estimated Rows | Rows | DB Hits | Variables        | Other                               |
+-----------------+----------------+------+---------+------------------+-------------------------------------+
| +ProduceResults |              0 |    0 |       0 | o, p             | p, o                                |
| |               +----------------+------+---------+------------------+-------------------------------------+
| +Filter         |              0 |    0 |       0 | anon[33], o, p   | Ors(o:Attributes, o:ExtraAttribute) |
| |               +----------------+------+---------+------------------+-------------------------------------+
| +Expand(All)    |              0 |    0 |       0 | anon[33], o -- p | (p)-->(o)                           |
| |               +----------------+------+---------+------------------+-------------------------------------+
| +NodeIndexSeek  |              0 |    0 |       1 | p                | :Product(id)                        |
+-----------------+----------------+------+---------+------------------+-------------------------------------+

顺便说一下,@ BrunoPeres在答案中的第一个查询也有类似的执行计划,但Filter操作非常不同。目前尚不清楚哪个会更快。

[UPDATE]

要回答您更新的问题:由于您不能拥有2个背靠背WHERE条款,因此您可以在现有的WHERE子句中添加更多字词,如下所示:

MATCH (p:Product {id:'5116003'})-[r]->(o)
WHERE
  (o:Attributes OR o:ExtraAttributes) AND
  ANY(key in KEYS(o) WHERE TOLOWER(key) CONTAINS 'weight')
RETURN o;