Cypher returning values from two match queries as one result

Showing 1-11 of 11 messages
Cypher returning values from two match queries as one result Aran Mulholland 10/30/12 4:20 PM

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

Re: [Neo4j] Cypher returning values from two match queries as one result Michael Hunger 10/30/12 5:07 PM
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>:

--
 
 

Re: [Neo4j] Cypher returning values from two match queries as one result Aran Mulholland 10/30/12 6:01 PM
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"?
> --
>
>
Re: [Neo4j] Cypher returning values from two match queries as one result Michael Hunger 10/30/12 6:34 PM
What's your datasize?

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

Am 31.10.2012 um 02:01 schrieb Aran Mulholland:
> --
>
>

Re: [Neo4j] Cypher returning values from two match queries as one result Aran Mulholland 10/30/12 6:51 PM
1215 nodes 660 properties 1809 relationships.

(so not big, i must be doing something else that's silly)
> --
>
>
Re: [Neo4j] Cypher returning values from two match queries as one result Aran Mulholland 10/31/12 11:09 PM
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.
Re: [Neo4j] Cypher returning values from two match queries as one result Wes Freeman 10/31/12 11:13 PM
I have a feature request for breaking up collections in github. https://github.com/neo4j/community/issues/898

--



Re: [Neo4j] Cypher returning values from two match queries as one result Michael Hunger 10/31/12 11:37 PM
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

Am 01.11.2012 um 07:09 schrieb Aran Mulholland <aranmul...@gmail.com>:
> --
>
>
Re: [Neo4j] Cypher returning values from two match queries as one result Andres Taylor 11/1/12 1:56 AM
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

--
 
 



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

Re: [Neo4j] Cypher returning values from two match queries as one result Andres Taylor 11/1/12 2:01 AM
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;
Re: [Neo4j] Cypher returning values from two match queries as one result Glenn Goodrich 3/5/13 5:17 PM
So, what is you want to combine posts, followerPosts and order them by their creation date in descending order?  I can't seem to figure that bit out.  

If I return (posts + followersPosts) as all_posts order by all_posts.created_at desc, I get

CypherTypeException: Expected `posts` to be a Map but it was a Collection - maybe aggregation removed it?

Any ideas?  Been spinning my wheels for a bit on this one....

thanks,
GLenn