Cypher query for implementing Status updates

99 views
Skip to first unread message

Abdul Azeez Shaik

unread,
Oct 9, 2012, 11:48:30 PM10/9/12
to ne...@googlegroups.com
Hi,

Is there any working cypher query to get status updates of friends (for below model)? (similar to newsfeed in facebook)?
I am using latest SDN and 1.8GA.
In Neo4j documentation, it was given for traversal, am unable to convert it into cypher query. 
If there is any working example code that would be really great.


Inline image 1 
image.png

Wes Freeman

unread,
Oct 10, 2012, 12:06:29 AM10/10/12
to ne...@googlegroups.com
Something like? (replace 1 with node id of person)

start n=node(1)
match n-[:friend]->f-[:status]->s
return s
order by s.date;

Wes

--
 
 

image.png

Abdul Azeez Shaik

unread,
Oct 10, 2012, 12:18:22 AM10/10/12
to ne...@googlegroups.com
Thanks for the prompt response Wes.
NEXT relationship must also be included, as next.status might be the latest one compared to s.status. struggling here to write a one...

And Lets say, i limit to 20 updates now. Next time when user wants next list, i need to get next 20updates, by this time,  there might be 10 new updates, due to this i will get 10old updates(which was already retrieved in first query).
Is this something that i need to handle after retrieving data?

Thanks,
Abdul

--
 
 

image.png

Abdul Azeez Shaik

unread,
Oct 10, 2012, 1:36:37 AM10/10/12
to ne...@googlegroups.com
I have written below cypher query is to create Statusupdate, 

@Query( "START n=node:search(username={0}) MATCH n-[r:STATUS]->oldsu DELETE r " +

"WITH n,oldsu CREATE n-[:STATUS]->(newsu {statusid:{1},statusUpdate:{2},postedDate:{3}})-[:NEXT]-oldsu")

void newStatusUpdate(String username, String statusid, String statusupdate, Date createddate);

However, how do i need to take care of is "su" doesn't exist, in that case second sub-query would not get executed.

Kindly help me.

Thanks,

Abdul
image.png

Abdul Azeez Shaik

unread,
Oct 10, 2012, 1:55:52 AM10/10/12
to ne...@googlegroups.com
I tried same query with optional relationship in first subquery, first subquery works fine.
However, second sub-query fails due to null in second query.
Can we avoid creating second relationship in oldsu is null?
Here is the updated query,

@Query( "START n=node:search(username={0}) MATCH n-[r?:STATUS]->oldsu DELETE r " +

"WITH n,oldsu CREATE n-[:STATUS]->(newsu {statusid:{1},statusUpdate:{2},postedDate:{3}})-[:NEXT]->oldsu")

void newStatusUpdate(String username, String statusid, String statusupdate, Date createddate);



image.png

Abdul Azeez Shaik

unread,
Oct 10, 2012, 2:14:45 AM10/10/12
to ne...@googlegroups.com
Ok, got the first part, got confused unnecessarily.
start n=node(1)
match n-[:friend]->f-[:status]->s-[:next*0..]->su 
return su
order by su.date desc;

Thanks for providing me initial help Wes.

Any help on the second part,
Lets say, i limit to 20 updates now. Next time when user wants next list, i need to get next 20updates, by this time,  there might be 10 new updates, due to this i will get 10old updates(which was already retrieved in first query).
Is this something that i need to handle after retrieving data?

image.png

Wes Freeman

unread,
Oct 10, 2012, 2:34:19 AM10/10/12
to ne...@googlegroups.com
You can do:
start n=node(1)
match n-[:friend]->f-[:status]->s-[:next*0..]->su 
return su
order by su.date desc
skip 20
limit 20;

To skip the first 20, and get 20 more status updates. Then you can keep incrementing skip as you get more.

Wes

--
 
 

image.png

Abdul Azeez Shaik

unread,
Oct 10, 2012, 2:50:34 AM10/10/12
to ne...@googlegroups.com
Hi Wes,

In between first call and second call if there are 10 more status updates, then it returns 10new and 10old(which are already return in first call) in second call.
I am thinking to save posted data from first call and add this condition to second call so that query doesn't consider new status updates.
Please let me know if this can be implemented in some other way.

Thanks,
 Abdul

--
 
 

image.png

Wes Freeman

unread,
Oct 10, 2012, 3:06:45 AM10/10/12
to ne...@googlegroups.com
Are status updates linked together via next? Maybe you should just be querying based on the oldest status update already returned, and then following that path. For the new status updates, that should be a different query (I think).

--
 
 

image.png

Abdul Azeez Shaik

unread,
Oct 10, 2012, 4:10:24 AM10/10/12
to ne...@googlegroups.com
Thats a nice idea. It would make query too very simple.
Thanks for the help Wes.

Any idea on the other query which i posted on same thread?

I tried same query with optional relationship in first subquery, first subquery works fine.
However, second sub-query fails due to null in second query.
Can we avoid creating second relationship in oldsu is null?
Here is the updated query,

@Query( "START n=node:search(username={0}) MATCH n-[r?:STATUS]->oldsu DELETE r " +

"WITH n,oldsu CREATE n-[:STATUS]->(newsu {statusid:{1},statusUpdate:{2},postedDate:{3}})-[:NEXT]->oldsu"


--
 
 

image.png

Wes Freeman

unread,
Oct 10, 2012, 4:19:51 AM10/10/12
to ne...@googlegroups.com
You can use predicates with CREATE also, so just add a WHERE oldsu <> null at the end.

At least, I think that's what you're trying to do.

Wes

--
 
 

image.png

Abdul Azeez Shaik

unread,
Oct 10, 2012, 6:57:10 AM10/10/12
to ne...@googlegroups.com
That predicate will exclude the complete operation, CREATE n-[:STATUS]->(newsu {statusid:{1},statusUpdate:{2},postedDate:{3}})-[:NEXT]->oldsu

First relation and node must be created, only second one must get excluded.i.e.NEXT relation.

Thanks,
Abdul

--
 
 

image.png

Wes Freeman

unread,
Oct 10, 2012, 10:02:06 AM10/10/12
to ne...@googlegroups.com
So, this is the closest I can get it:

start n=node:node_auto_index(name="person")  
match n-[r?:STATUS]->oldsu  
create n-[:STATUS]->(newsu {id:"an id"})  
with oldsu, newsu  
create newsu-[:NEXT]->oldsu  
where oldsu <> null  
return newsu;

The trouble with this one is that you can get multiple results back from the first match, so it will create more than one record with the second create.

In my latest pull request, there is a way to limit and order after a with statement, which I think might actually help here:

This way you can do:
start n=node:node_auto_index(name="person")
match n-[r?:STATUS]->oldsu  
with oldsu,n 
order by oldsu.date desc
limit 1
create n-[:STATUS]->(newsu {id:"an id",date:1000000})  
with oldsu, newsu
create newsu-[:NEXT]->oldsu  
where oldsu <> null  
return newsu;

I tested it on my local build and it creates 1 node and 1 relationship the first time, and then 1 node and 2 relationships the additional times I run it.

Maybe Michael or Peter know a better way to do it without the new pull request.

Wes

--
 
 

image.png

Abdul Azeez Shaik

unread,
Oct 10, 2012, 11:57:57 AM10/10/12
to ne...@googlegroups.com
Thats awesome Wes. I tried it just now and it worked like champ.
This query also makes sure that there is only one node with 'STATUS relationship, so, first match always returns single result.

Thanks once again for all the help Wes.

Regards,
 Abdul

--
 
 

image.png
Reply all
Reply to author
Forward
0 new messages