Re: [Neo4j] Cypher returning values from two match queries as one result

418 views
Skip to first unread message

Michael Hunger

unread,
Oct 30, 2012, 8:10:25 PM10/30/12
to ne...@googlegroups.com
Try to use better identifier-names :)

In general you can use collect:

return collect(b) as user_posts, collect(d) as other_posts

if you want to combine them

return collect(b) + collect(d) as posts

Am 31.10.2012 um 00:20 schrieb Aran Mulholland <aranmul...@gmail.com>:

I have a simple social network graph db model. Users can follow other users and post posts. I am trying to get a list of all posts that a user has posted along with any that anyone the user follows has posted

START a=node:node_auto_index(UserIdentifier = "USER0") 
MATCH (a)-[:POSTED]->(b), (a)-[:FOLLOWS]->(c)-[:POSTED]->(d) 
RETURN b, d;

It is returning the cross product of the two, a tuple of all the values in b joined with all the values in d. (b x d) I would like just a straight list of posts. How do I do this? Do I need to do two separate queries?


Neo4j 1.8RC1, Windows, Java 7


--
 
 

Aran Mulholland

unread,
Oct 30, 2012, 9:01:33 PM10/30/12
to ne...@googlegroups.com
Thanks Michael,

So the query: (with better identifier names:)

START me=node:node_auto_index(UserIdentifier = "USER0")
MATCH (me)-[:POSTED]->(myposts),
(me)-[:FOLLOWS]->(friends)-[:POSTED]->(myfriendsposts)
RETURN collect(myposts) + collect(myfriendsposts) as posts;

Should work? Because this never returns. The console just sits there,
dreaming perhaps.

Also do I need the "as posts"?
> --
>
>

Michael Hunger

unread,
Oct 30, 2012, 9:34:35 PM10/30/12
to ne...@googlegroups.com
What's your datasize?

> RETURN count(myposts) + count(myfriendsposts) as posts;
> --
>
>

Aran Mulholland

unread,
Oct 30, 2012, 9:51:10 PM10/30/12
to ne...@googlegroups.com
1215 nodes 660 properties 1809 relationships.

(so not big, i must be doing something else that's silly)
> --
>
>

Aran Mulholland

unread,
Nov 1, 2012, 2:09:48 AM11/1/12
to ne...@googlegroups.com
If I read the doco properly the collect operator returns all nodes as
a single row.
Is there another way that returns a set of rows that are the collected
by following two different paths?

Is there a Cypher function that breaks the return value of collect
into separate values?

Thanks

Aran.

Wes Freeman

unread,
Nov 1, 2012, 2:12:49 AM11/1/12
to ne...@googlegroups.com
I have a feature request for breaking up collections in github. https://github.com/neo4j/community/issues/898

--



Michael Hunger

unread,
Nov 1, 2012, 2:37:42 AM11/1/12
to ne...@googlegroups.com
I mean the number of followed paths.


You can use the collection created by collect twice with filter with 2 different conditions

Sent from mobile device
> --
>
>

Andres Taylor

unread,
Nov 1, 2012, 4:56:26 AM11/1/12
to ne...@googlegroups.com
Hi Aran,

This is how MATCH is supposed to work. If you need two collections, one with all the posts by the user, and one with all the posts of his followers, I would recommend you do it like this:

START a=node:node_auto_index(UserIdentifier = "USER0") 
MATCH (a)-[:POSTED]->(b)
WITH collect(b) as posts
MATCH (a)-[:FOLLOWS]->(c)-[:POSTED]->(d) 
RETURN posts, collect(d) as followersPosts;

HTH,

Andrés

On Wed, Oct 31, 2012 at 12:20 AM, Aran Mulholland <aranmul...@gmail.com> wrote:

I have a simple social network graph db model. Users can follow other users and post posts. I am trying to get a list of all posts that a user has posted along with any that anyone the user follows has posted

START a=node:node_auto_index(UserIdentifier = "USER0") 
MATCH (a)-[:POSTED]->(b), (a)-[:FOLLOWS]->(c)-[:POSTED]->(d) 
RETURN b, d;

It is returning the cross product of the two, a tuple of all the values in b joined with all the values in d. (b x d) I would like just a straight list of posts. How do I do this? Do I need to do two separate queries?


Neo4j 1.8RC1, Windows, Java 7

--
 
 



--
The best way to ask for Cypher help: http://console.neo4j.org/usage.html 

Andres Taylor

unread,
Nov 1, 2012, 5:01:56 AM11/1/12
to ne...@googlegroups.com
Sorry, I messed up. You need to pass on a through WITH as well.

START a=node:node_auto_index(UserIdentifier = "USER0") 
MATCH (a)-[:POSTED]->(b)
WITH a, collect(b) as posts
MATCH (a)-[:FOLLOWS]->(c)-[:POSTED]->(d) 
RETURN posts, collect(d) as followersPosts;
Reply all
Reply to author
Forward
0 new messages