简体   繁体   中英

How do I provide multiple queries in Neo4j Cypher?

I want to use the results from the first query in the second query. I am not sure how to do this in Cypher?

Current code,

START user1=node:USER_INDEX(USER_INDEX = "userA") 
MATCH user1-[r1:ACCESSED]->docid1<-[r2:ACCESSED]-user2, user2-[r3:ACCESSED]->docid2 
WHERE r2.Topic=r3.Topic 
RETURN distinct docid2.Label;

I want to have different conditions checked in the WHERE clause for the same docid2 set of nodes and accumulate the results and perform order by based on a date field. I am not able to provide multiple match and return within the same transaction. That is when I am trying to have two different cypher scripts and combine them in a third query. Is this possible in cypher? Or is there any option to write custom functions and invoke them? Do we have stored Cypher scripts like Stored Gremlin scripts?

As Michael mentioned in the comment, you can use the "with" statement to stream result into further statements. Unfortunately, you can't start another statement after the "where" clause. Multiple return statements would be kind of illogical, but you can do multiple things in a single query eg:

START x=node:node_auto_index(key="x")
with count(x) as exists
start y=node:node_auto_index(key="y")
where exists = 0
create (n {key:"y"})<-[:rel]-y
return n, y

This would check if the "x" node exists and if it doesn't, proceed to create it and add a couple of parameters.

If you wish to do more sophisticated things on result sets, your best options are either batch requests or the Java API...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM