我需要从Neo4j中的不同起始节点查询多个路径(在UI中绘制)(可能是数百条路径),我正在使用UNION
来组合查询,但我怀疑它不是检索数据的最有效方法(我也在启动节点上使用索引)。
在创建大型查询时,会发生各种令人讨厌的事情。
查询看起来像这样:
MATCH path = ((n:actor)-[*0..2]->(g)-[p:was_filmed_at]->(k:movie_set)-[l:located]->(i:state))
WHERE n.name=’john doe1' AND (g:acted_in OR g:produced)
AND p.addDate >= '2017-12-10T00:00:00+00:00'
AND p.addDate <= '2017-12-12T08:08:06+00:00'
AND l.addDate >= '2017-12-10T00:00:00+00:00'
AND l.addDate <= '2017-12-12T08:08:06+00:00'
RETURN nodes(path), relationships(path)
LIMIT 1000
UNION
MATCH path = ((n:actor)-[*0..2]->(g)-[p:was_filmed_at]->(k:movie_set)-[l:located]->(i:state))
WHERE n.name=’john doe2' AND (g:acted_in OR g:produced)
AND p.addDate >= '2017-12-10T00:00:00+00:00'
AND p.addDate <= '2017-12-12T08:08:06+00:00'
AND l.addDate >= '2017-12-10T00:00:00+00:00'
AND l.addDate <= '2017-12-12T08:08:06+00:00'
RETURN nodes(path), relationships(path)
LIMIT 1000
UNION
MATCH path = ((n:actor)-[*0..2]->(g)-[p:was_filmed_at]->(k:movie_set)-[l:located]->(i:state))
WHERE n.name=’john doe3' AND (g:acted_in OR g:produced)
AND p.addDate >= '2017-12-10T00:00:00+00:00'
AND p.addDate <= '2017-12-12T08:08:06+00:00'
AND l.addDate >= '2017-12-10T00:00:00+00:00'
AND l.addDate <= '2017-12-12T08:08:06+00:00'
RETURN nodes(path), relationships(path)
LIMIT 1000
这是一个只有3条路径的查询,它不能很好地扩展到数百个路径。有没有一种简单,更有效的方法来做到这一点?
答案 0 :(得分:1)
由于您只更改n.name
比较,因此可以使用IN operator重写查询,并将名称列表作为数组传递。这样:
MATCH path = ((n:actor)-[*0..2]->(g)-[p:was_filmed_at]->(k:movie_set)-[l:located]->(i:state))
WHERE n.name IN ['john doe1', 'john doe2', 'john doe3']
AND (g:acted_in OR g:produced)
AND '2017-12-10T00:00:00+00:00' <= p.addDate <= '2017-12-12T08:08:06+00:00'
AND '2017-12-10T00:00:00+00:00' <= l.addDate <= '2017-12-12T08:08:06+00:00'
RETURN nodes(path), relationships(path)
LIMIT 1000
你也可以将不平等链接起来:AND '2017-12-10T00:00:00+00:00' <= p.addDate <= '2017-12-12T08:08:06+00:00'
。确保:actor(name)
上有an index,以便快速索引查找起始节点。 (感谢@InverseFalcon)