Neo4j匹配节点属性或关系属性 [英] Neo4j Match Node Property OR Relationship Property

查看:1332
本文介绍了Neo4j匹配节点属性或关系属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个查询,该查询将返回与节点属性或关系属性匹配的节点.

I'm trying to write a query that will return nodes that either match a node property or a relationship property.

例如,如果关系属性状态为良好",我希望名称属性为George OR的所有节点.我有两个查询,将为每个查询获取节点:

For instance, I want all nodes where the name property is George OR if the relationship property status is "good". I have two queries that will get the nodes for each of these:

MATCH (n) where n.name = 'George' return n

MATCH (n)-[r]-() where r.status = 'good' return n 

是否可以编写一个单查询来获得这些组合的结果?我以为我可以使用此可选查询(如下),但是由于我只是从第一个查询中获取节点,所以我似乎误解了可选match子句.

Is there a singe query I could write to get these combined results? I thought I could use this optional query (below), but I seemed to have misunderstood the optional match clause because I'm only getting nodes from the first query.

MATCH (n) where n.name = 'George' 
Optional MATCH (n)-[r]-() where r.status = 'good' return distinct n 

推荐答案

在进行可选匹配时,唯一可以与之匹配的n节点就是已经与第一个条件匹配的节点.你可以做

By the time the optional match happens, the only n nodes that are around to make the optional match from are the ones that already match the first criteria. You can do

MATCH (n)
WHERE n.name = 'George' OR n-[{ status:"good" }]->()
RETURN n

但是对于较大的图,请记住,这将无法有效利用索引.

but for larger graphs remember that this will not make efficient use of indices.

另一种方式是

MATCH (n {name:"George"})
RETURN n
UNION MATCH (n)-[{status:"good"})->()
RETURN n

如果您使用标签并设置了相关的索引,那么这对于第一次匹配的索引应该会做得更好(但是第二部分仍然可能效率很低).

This should do better with indices for the first match, assuming you use a label and have the relevant index set up (but the second part would still potentially be very inefficient).

修改
再说一遍,关系索引将使该部分变得更快,更正确,但在我看来,最好将其慢一点,因为该模式未充分确定.第二个匹配模式的作用类似于

Edit
Re comment, relationship indexing would make that part faster, correct, but to my mind it would be better to say it is slow because the pattern is underdetermined. The second match pattern does something like

  • 将图中的每个节点绑定到(n)
  • (n)
  • 获取所有传出关系(无论类型如何)
  • 检查status="good"
  • 的关系
  • bind every node in the graph to (n)
  • get all outgoing relationships (regardless of type) from (n)
  • check relationship for status="good"

您可以使用关系索引来提高性能,但是由于关系仅存在于它所关联的两个节点之间,因此您可以认为它是由这些节点索引的.即,通过排除关系不相关的节点来修复第一个项目符号点.这两个match子句可能看起来像

You could improve performance with relationship indexing, but since a relationship exists only between the two nodes it relates, you can think of it instead as indexed by those nodes. That is, fix the first bullet point by excluding nodes whose relationships are not relevant. The two match clauses could look like

MATCH (n:Person {name:"George"})  
       // add label to use index
MATCH (n:Person)-[{status:"good"}]->() 
       // add label to limit (n) -no indexing, but better than unlimited (n)
MATCH (n:Person {name:"Curious"})-[{status:"good"}]->()
       // add label to use index -now the relationships are sort-of-indexed

和/或键入关系

MATCH (n)-[:REL {status:"good"}]->() // add type to speed up relationship retrieval

实际上,使用匿名关系和rel属性,将属性设置为类型可能是有意义的(项目符号三),所以

in fact, with anonymous relationships and rel property, it would probably make sense (bullet point three) to make the property the type, so

MATCH (n)-[:GOOD]->() // absent type, it would make sense to use the property as type instead

您的实际查询可能看起来有很大的不同,您的问题根本不是关于查询性能的:)哦.

Your actual queries may look very different, and your question wasn't really about query performance at all :) oh well.

这篇关于Neo4j匹配节点属性或关系属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆