Neo4j 密码查询:使用 ORDER BY 和 COLLECT(S) [英] Neo4j cypher query : using ORDER BY with COLLECT(S)
问题描述
我很难从两个不同的来源收集数据并合并这些集合,以便最后一个是一组按dateCreated"排序的对象.
背景
用户可以分组提问.问题可以是一般性的,也可以是与特定视频游戏相关的问题.如果小组中提出的问题与视频游戏相关,则该问题也会出现在视频游戏的问题页面中.
目前,我有两个一般性问题,一个是针对一款视频游戏的.因此,在获取问题时,我应该有 3 个问题.
查询
这是查询:
START group = node(627)MATCH 一般问题-[?:GENERAL_QUESTION]-> 组WITH组,一般问题MATCH gamesQuestions-[?:GAME_QUESTION]->games<-[:GAMES]-groupWITH (collect(generalQuestions) + collect(gamesQuestions)) 作为问题返回问题ORDER BY questions.dateCreated
第一个问题:使用 ORDER BY
Cached(questions of type Collection) 应该是 Map 类型,但它是 Collection 类型 - 也许聚合删除了它?
实现我想要做的事情的正确方法是什么?
第二个问题:错误的结果
如果我删除 ORDER BY 子句,我得到的结果不是 3 个,而是 14 ... :
<预><代码>[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"},节点[632]{dateCreated:1380889484,dateUpdated:1380889484,title:"GTA5 TITLE",type:2,content:"GTA5 CONTENT"}]我收集结果的方式有问题吗?
编辑
扩展查询以获取游戏问题:
gamesQuestions-[:GAME_QUESTION]->()<-[:QUESTIONS]-games-[:INTERESTS]->()<-[:HAS_ INTEREST_FOR]-兴趣<-[:兴趣]-组
感谢您的帮助,
Order by"需要节点或关系上的属性.查询中的问题"是节点的集合,而不是节点/关系,您不能使用排序依据"对集合进行排序,您只能根据其属性对节点或关系进行排序.
为了使用Order by",您需要将问题作为一列行而不是一个集合返回.根据您原始查询中指示的关系,以下查询应将一般和特定游戏问题作为一列行返回,并根据属性dateCreated"对它们进行排序,
START group = node(627)匹配问题-[?:GENERAL_QUESTION|GAME_QUESTION]->()<-[:GAMES*0..1]-(组)返回不同的问题按 question.dateCreated 排序
对于游戏问题通过关系序列gamesQuestions-[?:GAME_QUESTION]->games<-[:GAMES]-group 与组相关的扩展情况,我有 gamesQuestions-[:GAME_QUESTION]->()<-[:QUESTIONS]-games-[:INTERESTS]->()<-[:HAS_ INTEREST_FOR]-interests<-[:INTERESTS]-group",你可以简单地在之前的查询如下,
START group = node(627)匹配问题-[:GENERAL_QUESTION|GAME_QUESTION]->()-[*0..4]-(组)返回不同的问题按 question.dateCreated 排序
这个想法是将可以到达组节点的问题与一步或四步匹配.
另一种选择是在 where 子句中指定两种模式,
START group = node(627)匹配问题-[*]-组其中 question-[:GENERAL_QUESTION]->group or (question-[:GAME_QUESTION]->()<-[:QUESTIONS]-()-[:INTERESTS]->()<-[:HAS_INTERESTS_FOR]-()<-[:INTERESTS]-组)返回不同的 q按 q.dateCreated 排序
I'm having a hard time collecting data from two distinct sources and merge the collections so that the final one is a set of objects ordered by 'dateCreated'.
Context
Users can ask questions in groups. A question can be either general or related to a specific video-game. If the question asked in a group is video-game related, this question also appears in the video-game's questions page.
Currently, I have two general questions and one specific to one video-game. Hence, when fetching the questions, I should have 3 questions.
Query
Here's the query :
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
First issue : using ORDER BY
Cached(questions of type Collection) expected to be of type Map but it is of type Collection - maybe aggregation removed it?
What's the proper way of achieving what I'm trying to do?
Second issue : wrong results
If I remove the ORDER BY clause, instead of having 3 results, I get 14 ... :
[
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"}
]
Is there something wrong with the way I collect the results ?
EDIT
Expanded query to get the gamesQuestion :
gamesQuestions-[:GAME_QUESTION]->()<-[:QUESTIONS]-games-[:INTERESTS]->()<-[:HAS_INTEREST_FOR]-interests<-[:INTERESTS]-group
Thanks for your help,
The "Order by" expects a property on a node or a relationship. The "questions" in your query is a collection of nodes instead of a node/relationship, you can't sort a collection using "Order by", you can only sort nodes or relationships on their properties.
In order to use "Order by", you need to return the questions as a column of rows rather than a collection. In terms of the relationships indicated in your original query, the following query should return the general and the specific game questions as a column of rows and sort them on the property "dateCreated",
START group = node(627)
Match question-[?:GENERAL_QUESTION|GAME_QUESTION]->()<-[:GAMES*0..1]-(group)
Return distinct question
Order by question.dateCreated
For the expanded case where the game questions are related to the group via the sequence of relationships "gamesQuestions-[?:GAME_QUESTION]->games<-[:GAMES]-group, I have gamesQuestions-[:GAME_QUESTION]->()<-[:QUESTIONS]-games-[:INTERESTS]->()<-[:HAS_INTEREST_FOR]-interests<-[:INTERESTS]-group", you can simply extend the pattern in the previous query as follows,
START group = node(627)
Match question-[:GENERAL_QUESTION|GAME_QUESTION]->()-[*0..4]-(group)
Return distinct question
Order by question.dateCreated
The idea is to match the questions that can reach the group node with either one step, or 4 more steps.
Another option is to specify the two patterns in the where clause,
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 密码查询:使用 ORDER BY 和 COLLECT(S)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!