Neo4j 密码查询 : using ORDER BY with COLLECT(S)

标签 neo4j cypher collect

我很难从两个不同的来源收集数据并合并这些集合,以便最后一个是一组按“dateCreated”排序的对象。

上下文

用户可以分组提问。 问题可以是一般性的,也可以是与特定视频游戏相关的。 如果群组中提出的问题与视频游戏相关,则该问题也会出现在视频游戏的问题页面中。

目前,我有两个一般性问题,一个是针对一款视频游戏的。 因此,在提取问题时,我应该有 3 个问题。

查询

这是查询:

START group = node(627)
MATCH generalQuestions-[?:GENERAL_QUESTION]->group
WITH group, generalQuestions
MATCH gamesQuestions-[?:GAME_QUESTION]->games<-[:GAMES]-group
WITH (collect(generalQuestions) + collect(gamesQuestions)) as questions
RETURN questions
ORDER BY questions.dateCreated

第一期:使用ORDER BY

Cached(questions of type Collection) expected to be of type Map but it is of type Collection - maybe aggregation removed it?

实现我想要做的事情的正确方法是什么?

第二个问题:错误的结果

如果我删除 ORDER BY 子句,我得到 14 个结果而不是 3 个结果......:

[
Node[641]{dateCreated:1380892636,dateUpdated:1380892636,title:"GENERAL TITLE 1",type:1,content:"GENERAL CONTENT 1"},
Node[641]{dateCreated:1380892636,dateUpdated:1380892636,title:"GENERAL TITLE 1",type:1,content:"GENERAL CONTENT 1"},
Node[641]{dateCreated:1380892636,dateUpdated:1380892636,title:"GENERAL TITLE 1",type:1,content:"GENERAL CONTENT 1"},
Node[641]{dateCreated:1380892636,dateUpdated:1380892636,title:"GENERAL TITLE 1",type:1,content:"GENERAL CONTENT 1"},
Node[641]{dateCreated:1380892636,dateUpdated:1380892636,title:"GENERAL TITLE 1",type:1,content:"GENERAL CONTENT 1"},
Node[641]{dateCreated:1380892636,dateUpdated:1380892636,title:"GENERAL TITLE 1",type:1,content:"GENERAL CONTENT 1"},
Node[642]{dateCreated:1380892642,dateUpdated:1380892642,title:"GENERAL TITLE 2",type:1,content:"GENERAL CONTENT 2"},
Node[642]{dateCreated:1380892642,dateUpdated:1380892642,title:"GENERAL TITLE 2",type:1,content:"GENERAL CONTENT 2"},
Node[642]{dateCreated:1380892642,dateUpdated:1380892642,title:"GENERAL TITLE 2",type:1,content:"GENERAL CONTENT 2"},
Node[642]{dateCreated:1380892642,dateUpdated:1380892642,title:"GENERAL TITLE 2",type:1,content:"GENERAL CONTENT 2"},
Node[642]{dateCreated:1380892642,dateUpdated:1380892642,title:"GENERAL TITLE 2",type:1,content:"GENERAL CONTENT 2"},
Node[642]{dateCreated:1380892642,dateUpdated:1380892642,title:"GENERAL TITLE 2",type:1,content:"GENERAL CONTENT 2"},
Node[632]{dateCreated:1380889484,dateUpdated:1380889484,title:"GTA5 TITLE",type:2,content:"GTA5 CONTENT"},
Node[632]{dateCreated:1380889484,dateUpdated:1380889484,title:"GTA5 TITLE",type:2,content:"GTA5 CONTENT"}
]

我收集结果的方式有问题吗?

编辑

获取游戏问题的扩展查询:

gamesQuestions-[:GAME_QUESTION]->()<-[:QUESTIONS]-games-[:INTERESTS]->()<-[:HAS_‌​INTEREST_FOR]-interests<-[:INTERESTS]-group

谢谢你的帮助,

最佳答案

“Order by”期望节点或关系上的属性。您查询中的“问题”是节点的集合,而不是节点/关系,您不能使用“排序依据”对集合进行排序,您只能对节点或关系的属性进行排序。

为了使用“Order by”,您需要将问题作为一列而不是一个集合返回。根据原始查询中指示的关系,以下查询应将一般和特定游戏问题作为一列行返回,并在属性“dateCreated”上对它们进行排序,

START group = node(627) 
Match question-[?:GENERAL_QUESTION|GAME_QUESTION]->()<-[:GAMES*0..1]-(group)
Return distinct question
Order by question.dateCreated

对于游戏问题通过关系序列“gamesQuestions-[?:GAME_QUESTION]->games<-[:GAMES]-group 与组相关的扩展案例,我有 gamesQuestions-[:GAME_QUESTION]->()<-[:QUESTIONS]-games-[:INTERESTS]->()<-[:HAS_‌ INTEREST_FOR]-interests<-[:INTERESTS]-group",您可以简单地扩展上一个查询中的模式如下,

START group = node(627) 
Match question-[:GENERAL_QUESTION|GAME_QUESTION]->()-[*0..4]-(group)
Return distinct question
Order by question.dateCreated

想法是匹配可以通过一步或 4 步以上到达组节点的问题。

另一种选择是在 where 子句中指定两种模式,

START group = node(627) 
MATCH question-[*]-group
Where question-[:GENERAL_QUESTION]->group or (question-[:GAME_QUESTION]->()<-[:QUESTIONS]-()-[:INTERESTS]->()<-[:HAS_INTERESTS_FOR]-()<-[:INTERESTS]-group)
Return distinct q
Order by q.dateCreated

关于Neo4j 密码查询 : using ORDER BY with COLLECT(S),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19183348/

相关文章:

neo4j - 如何在 Neo4j 中将 `with` 语句与 `call.apoc.do.when` 一起使用?

python - 如何累积/收集/求和列表?

java - 如何使用java8来采集这张图

neo4j - 跟踪 Neo4j 中的更改 - 在标准编程中实现诸如 "flag"变量之类的功能

neo4j - 我想在 Neo4j 中创建和操作属性数组

neo4j - Cypher:基于公共(public)属性键 id 创建节点之间的关系

java - 无法导出 Cypher 查询

nested - Pyspark - 使用 collect_list 时保留空值

indexing - Neo4j:在所有标签上创建索引

neo4j - 如何通过在 Neo4j 中对另一个属性进行分组来获取属性的最大值