问题描述
我正在使用带有宝石Neography的Neo4j在Ruby中进行编码.查询时,我使用Neography提供的execute_neo4j_query方法.我不确定最佳做法.
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.
我的问题是:
1)是否可以使用Neography重用先前查询的结果?
1) Is there a way to reuse previously queried results using Neography?
2)除了直接使用execute_neo4j_query之外,是否建议使用Neography提供的方法(例如@ neo.create_node)?我选择后者是因为我不确定封装的方法是否可以满足我的任务.因此,如果您可以用本机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.
推荐答案
- 如果您不知道为什么要使用它,请不要再使用开始
- 使用事务性的密码学中的密码支持
- 在密码查询中使用参数,例如
MATCH (u:User {name:{name}}) RETURN u
- Don't use start anymore if you don't know why you would use it
- use the transactional cypher support in neography
- Use Parameters in your cypher queries, like
MATCH (u:User {name:{name}}) RETURN u
这篇关于在Neo4j中记住和重用先前查询结果的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!