Neo4j - 查询:将不同的查询结果组合在一个具有特殊顺序的集合中

时间:2017-07-25 08:42:56

标签: neo4j cypher

我有这样的查询

START a = node:lucene_index('name: "actor1"')
MATCH (a:Actor)--(b:Movies)
WHERE b.director = 'director1'
WITH b LIMIT 4
RETURN 'Movies' AS resultType, collect (DISTINCT b) as resultList

UNION ALL 

START a = node:lucene_index('name: "actor1"')
MATCH (a:Actor)--(b:Movies)
WHERE b.director = 'director2'
WITH b LIMIT 4
RETURN 'Movies' AS resultType, collect (DISTINCT b) as resultList

UNION ALL 

START a = node:lucene_index('name: "actor1"')
MATCH (a:Actor)-[r]-(b:Movies)
WITH b
MATCH (b)-[r2]-(b2:Actor)
WITH b, count(r2) as relations
ORDER BY toInt(relations) DESC
LIMIT 2
RETURN 'Movies' AS resultType, collect (DISTINCT b) as resultList

是否可以将这些查询合并为一个? 重要的是,resultList按照查询

中所示的顺序包含电影
  • 1 - 4 of director1
  • 5 - 8 of director2
  • 9 - 10与演员关系最多

谢谢!

1 个答案:

答案 0 :(得分:0)

您可能需要查看post-union processing上的Neo4j知识库文章。这提供了一些方法来执行您的工会,但之后能够执行排序操作。

当然,你需要一些东西来解决这个问题。我会为每个(例如order)添加第三个字段,并为第一个子查询显式设置为1,第二个子查询显式设置为2,第三个子查询显式设置为3。然后在联合后处理时对该字段进行排序。