How do I provide multiple queries in Neo4j Cypher?

十年热恋 提交于 2019-12-23 01:52:50

问题


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?


回答1:


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 e.g.:

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...



来源:https://stackoverflow.com/questions/10880546/how-do-i-provide-multiple-queries-in-neo4j-cypher

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!