I am coding in Ruby using Neo4j with the gem Neography. When doing query, I use the method execute_neo4j_query provided by Neography. I am not sure about the best practice.
Suppose I use following code to fetch a user:
user1 = @neo.execute_neo4j_query("
MATCH (user:User {user_id: '#{params[:user_id_1]}'})
RETURN id(user)
LIMIT 1
")
and to fetch another user similarly
user2 = @neo.execute_neo4j_query("
MATCH (user:User {user_id: '#{params[:user_id_2]}'})
RETURN id(user)
LIMIT 1
")
Then I did some stuff with this two users.
Now I need to create an edge between this two users, so I did this
@neo.execute_neo4j_query("
MATCH (user1:User {user_id: '#{params[:user_id_2]}'})
MATCH (user2:User {user_id: '#{params[:user_id_2]}'})
CREATE UNIQUE (user1)-[:FOLLOW]->(user2)
")
However, I believe such approach is not optimal, since I queried for the same two users twice.
My question is:
1) Is there a way to reuse previously queried results using Neography?
2) Is it recommended to use methods provided by Neography such as @neo.create_node other than using execute_neo4j_query directly? I choose the latter because I am not sure if the encapsulated methods can satisfy my task. So, if you can rewrite my above codes in native Neography code, it will be greatly appreciated.
I don't know Neography, but I can take a shot at answering question #1, since that appears to be answerable by changing your third Cypher query.
Since your 2 initial queries seem to be getting the node IDs for user 1 and user 2, you should be able to avoid searching again for those nodes by using the START
clause. You will need to specify a parameter map with the 2 node IDs returned by the first 2 queries.
START user1=node(#{params[:node_id_1]}), user2=node(#{params[:node_id_2]})
CREATE UNIQUE (user1)-[:FOLLOW]->(user2)
MATCH (u:User {name:{name}}) RETURN u
我也不知道Neography,但是您可以使用此衬套稍微简化查询:
MATCH (a:User),(b:User) WHERE a.userId ={id1} AND b.userId = {id2} CREATE UNIQUE (a)-[r:FOLLOWS]->(b) RETURN r
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.