cypher: how to get both my tags AND my friends' tags

23 views
Skip to first unread message

arikan

unread,
Jan 29, 2013, 7:55:18 PM1/29/13
to neo...@googlegroups.com
Trying to get both my tags AND my friends' tags in a single query...

find my friends' tags whose name is "bla":

q = Neo4j._query("START me=node(1) MATCH me-[:`following`]->friend-[:`User#tags`]->tag WHERE tag.name = 'bla' RETURN tag ORDER BY tag.name")

find my and friends' shared tags (intersection) whose name is "bla":

q = Neo4j._query("START me=node(1) MATCH me-[:`User#tags`]->tag, me-[:`following`]->friend-[:`User#tags`]->tag WHERE tag.name = 'bla' RETURN tag ORDER BY tag.name")

How can I get both my tags AND my friends' tags (union)? Couldn't see any way to combine MATCH in Cypher, any help appreciated.

Thanks,
Burak

Michael Hunger

unread,
Jan 29, 2013, 8:03:36 PM1/29/13
to neo...@googlegroups.com
What do you actually want to do?

you can use two different tags here:

q = Neo4j._query("START me=node(1) MATCH me-[:`User#tags`]->tag1, me-[:`following`]->friend-[:`User#tags`]->tag1 WHERE tag1.name = 'bla' and tag2.name = 'bla' RETURN tag1,tag2 ORDER BY tag.name")


you can also use collect to take the friends tags first and then add yours secondly

START me=node(1) MATCH me-[:`following`]->friend-[:`User#tags`]->tag WHERE tag.name = 'bla'
with me, tag
order by tag.name
with me, collect(tag) as tags
MATCH me-[:`User#tags`]->tag
return tags + collect(tag)

you can use paths as expressions, then they return just a collection of paths

return extract(p in me-[:`User#tags`]->() : last(p)) as my_tags

or 
extract(p in friend-[:`User#tags`]->() : last(p)) as friend_tags
--
You received this message because you are subscribed to the Google Groups "neo4jrb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to neo4jrb+u...@googlegroups.com.
To post to this group, send email to neo...@googlegroups.com.
Visit this group at http://groups.google.com/group/neo4jrb?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

arikan

unread,
Jan 29, 2013, 8:14:58 PM1/29/13
to neo...@googlegroups.com
Thanks Michael, I tried the following based on your example, but got an error:

START me=node(1) MATCH me-[:`following`]->friend-[:`User#tags`]->tag WHERE tag.name =~ '(?i)bla.*'
WITH me, tag
ORDER BY tag.name
WITH me, collect(tag) as tags
MATCH me-[:`User#tags`]->tag WHERE tag.name =~ '(?i)bla.*'
RETURN tags + collect(tag)

Java::OrgNeo4jCypher::SyntaxException: Unknown identifier `tags`.

Michael Hunger

unread,
Jan 29, 2013, 8:19:37 PM1/29/13
to neo...@googlegroups.com
Which version are you using?

There was an issue with that that was recently fixed (1.9.M04 I think)

arikan

unread,
Jan 29, 2013, 8:24:31 PM1/29/13
to neo...@googlegroups.com
I an currently on neo4j 1.8.1

basically trying to search the combination of tags by me and by my friends in a single query.
Reply all
Reply to author
Forward
0 new messages