首页 文章

cypher查询在可选节点上匹配

提问于
浏览
2

我正在尝试改进已发布的查询:neo4j improving cypher query performace

再说一遍,我有 items graph db . 每个 item 连接到多个 properties ,可以由多个 items 共享 . 但是,这次我想从少数 property 节点开始搜索 items 并查找连接最多的项目 .

所以,我有一组属性 pr=["pr1","pr2","pr3"] ,我想找到最相关的 items

match (pr)-[r]-(item)
return item, count(r) as matching_properties
order by matching_properties

但是,我也希望找到类似的项目 . properties 之间有 SIMILAR 关系,我想找到:

MATCH (pr) -[:SIMILAR]->(pr2)-[r]-(item)

我尝试通过执行以下操作检索所有项目:

MATCH (pr) -[r*1..2]-(item)

问题是我只想通过 property 节点获得 SIMILAR 关系的路径,不幸的是我有很多路径通过其他关系的其他节点 . 所以我可以这样做:

MATCH (pr) -[r*1..2]-(item)
where (length(r)= 1 or ANY (x in r where (type(x)="SIMILAR")))

但这真的没有效率,因为它只是路径的一小部分 . 我真的想要做一个类似这样的可选匹配:

MATCH (pr) -[r2?:SIMILAR]->(pr2?)-[r]-(item)
where pr2.type = "property"

或者,在两个路径选项之间使用“OR”操作数(我知道它不存在..)

有没有办法做到这一点?目前我正在使用neo4j 1.9.2,但我打算转向2.0 . 所以2.0的答案对我也有好处 .

1 回答

  • 4

    像这样的东西?

    从0开始的路径( *0.. )还包括到起始节点的零长度路径 .

    MATCH (pr:property)-[:SIMILAR*0..1]->(pr2:property)<-[:HAS_PROPERTY]-(item:item)
    RETURN pr2, item
    

相关问题