Neo4j 如何使用或匹配节点标签?

Neo4j 如何使用或匹配节点标签?,neo4j,cypher,Neo4j,Cypher,如何在这样的查询中匹配两个可能的节点标签 根据赛博萨姆的建议,我改为: match (p:Product {id:'5116003'})-[r]->(o:Attributes|ExtraAttribute) return p, o 现在我需要添加第二个where子句。如何修改它?您可以尝试使用以下函数: MATCH (p:Product {id:'5116003'})-[r]->(o) WHERE o:Attributes OR o:ExtraAttributes **WHERE

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

根据赛博萨姆的建议,我改为:

match (p:Product {id:'5116003'})-[r]->(o:Attributes|ExtraAttribute) return p, o
现在我需要添加第二个where子句。如何修改它?

您可以尝试使用以下函数:

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
此外,如果有,您可以使用apoc.path.expand path expander过程,该过程根据标签过滤器从最小到最大的给定关系从开始节点展开

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

.

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

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]
生成相同的执行计划,如下所示。我假设:Productid上有一个索引:

这是上面第二个查询的两个标签形式:

+-----------------+----------------+------+---------+------------------+--------------+
| 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;
顺便说一句,@BrunoPeres的答案中的第一个查询也有类似的执行计划,但是过滤操作非常不同。现在还不清楚哪一个会更快

[更新]

要回答您更新的问题:由于您不能有2个背对背的WHERE子句,您只需在现有WHERE子句中添加更多术语,如下所示:

+-----------------+----------------+------+---------+------------------+-------------------------------------+
| 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)                        |
+-----------------+----------------+------+---------+------------------+-------------------------------------+

第二种似乎更有效。“路径”返回什么?是否可以直接返回“o”节点?@user697911 return path返回整个路径。要返回p和o,请查看我的编辑。我当前的查询有很多这样的约束:其中,keyso中的任意键toLowerkey包含“+attrKey+”,其中attrKey或attrValue是变量。是否可以使用“apoc”向上述查询添加类似的约束?@user697911我不这么认为,因为路径扩展器仅基于标签和关系类型工作。但我认为这是一个好问题。也许你应该提出一个新的问题。。。
+-----------------+----------------+------+---------+------------------+-------------------------------------+
| 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)                        |
+-----------------+----------------+------+---------+------------------+-------------------------------------+
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;