Neo4j密码查询:将ORDER BY与COLLECT(S)一起使用 [英] Neo4j cypher query : using ORDER BY with COLLECT(S)

查看:150
本文介绍了Neo4j密码查询:将ORDER BY与COLLECT(S)一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难从两个不同的来源收集数据并合并这些集合,以便最后一个是按"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'.

上下文

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

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.

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

Currently, I have two general questions and one specific to one video-game. Hence, when fetching the questions, I should have 3 questions.

查询

这是查询:

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?

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

What's the proper way of achieving what I'm trying to do?

第二个问题:错误的结果

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

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 ?

编辑

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

Expanded query to get the gamesQuestion :

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

感谢您的帮助,

推荐答案

排序依据"期望节点或关系上的属性.查询中的问题"是节点的集合,而不是节点/关系,您不能使用排序依据"对集合进行排序,只能对节点或它们的属性上的关系进行排序.

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.

为了使用排序依据",您需要将问题作为一列行而不是一个集合返回.根据原始查询中指示的关系,以下查询应以行列的形式返回常规和特定游戏问题,并在属性"dateCreated"上对其进行排序,

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

对于扩展的情况,其中游戏问题通过"gamesQuestions-[?: GAME_QUESTION]-> games<-[:GAMES] -group"之间的关系序列与组相关,我有游戏问题-[:GAME_QUESTION] >()<-[:: QUESTIONS] -games-[:: INTERESTS]->()<-::: HAS_‌INTEREST_FOR] -interests<-[:INTERESTS] -group,您可以在上一个查询如下,

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.

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

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆