Combining Cypher Queries

49 views
Skip to first unread message

Chris Bolton

unread,
Aug 9, 2012, 4:38:25 PM8/9/12
to ne...@googlegroups.com
I currently have multiple Cypher queries that are being stored in variables and then being added or subtracted from one another. My understanding is that it would probably process faster if I was able to string together one really big query. Is it possible? All the queries start from the same node, the queries are just returning various collections of nodes based on their relationship and their reciprocal values (i.e. 

oof = Neo4j._query("START n=node(#{self.id}) MATCH (n)-[:outer_only_friends]->(x) RETURN x")
bf = Neo4j._query("START n=node(#{self.id}) MATCH (n)-[:blocked_friends]->(x) RETURN x")
otif = Neo4j._query("START n=node(#{self.id}) MATCH(n)-[:outer_only_friends]->(x) WHERE (x-[:friends]->n) RETURN x")
itof = Neo4j._query("START n=node(#{self.id}) MATCH(n)-[:friends]->(x) WHERE (x-[:outer_only_friends]->n) RETURN x")
icic = START n=node(4) MATCH (n)-[:friends]->(x)-[:friends]->(z) WHERE z<>n AND (x-[:friends]->n AND z-[:friends]->x) AND NOT(n-[:blocked_friends]->x AND x-[:blocked_friends]->z) RETURN z
ic = Neo4j._query("START n=node(#{self.id}) MATCH (n)-[:friends]->(x) WHERE (x-[:friends]->n) AND NOT(n-[:blocked_friends]->x) RETURN distinct x")

Any ideas? I appreciate any help.

Chris

Michael Hunger

unread,
Aug 9, 2012, 5:12:51 PM8/9/12
to ne...@googlegroups.com
How are they subtracted?
Don't use
> node(#{self.id})
but a parameter like node({id}) and then pass a hash :id => self.id


you can do as you did in the third query
match the outer friends and then
START n=node(#{self.id})
MATCH (n)-[:outer_only_friends]->(x)
where not((n)-[:blocked_friends]->(x))
return x

you can also use in and filter
START n=node(#{self.id})
MATCH (n)-[:blocked_friends]->(x)
WITH n,collect(x) as blocked
MATCH (n)-[:outer_only_friends]->(x)
RETURN filter( m in collect(x) : m not in blocked)

or

START n=node(#{self.id})
MATCH (n)-[:blocked_friends]->(x)
WITH n,collect(x) as blocked
MATCH (n)-[:outer_only_friends]->(x)
WHERE x not in blocked
RETURN x

HTH

Michael

Chris Bolton

unread,
Aug 10, 2012, 11:49:10 AM8/10/12
to ne...@googlegroups.com
Good morning Michael (although I don't think it's morning where you are),

The queries are each individually stored in variables and then subtracted from one another.

results = oof - icic + ic ...

Why would you not use #{self.id}? Security concerns?

I am going to work on the solutions this morning. Thanks for the help. I'll circle back with any questions.

Chris 

--- On Thu, 8/9/12, Michael Hunger <michael...@neotechnology.com> wrote:
Reply all
Reply to author
Forward
0 new messages