Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Graph 在双向关系图中搜索neo4j.rb中的n级好友_Graph_Neo4j_Bidirectional_Neo4j.rb_Bidirectional Relation - Fatal编程技术网

Graph 在双向关系图中搜索neo4j.rb中的n级好友

Graph 在双向关系图中搜索neo4j.rb中的n级好友,graph,neo4j,bidirectional,neo4j.rb,bidirectional-relation,Graph,Neo4j,Bidirectional,Neo4j.rb,Bidirectional Relation,我有一个定义如下的用户类 class User include Neo4j::ActiveNode include Neo4j::Timestamps property :user_id, type: Integer, constraint: :unique property :max_friends_count, type: Integer, default: 5 validates :user_id, :presence => true has_many :ou

我有一个定义如下的用户类

class User
  include Neo4j::ActiveNode
  include Neo4j::Timestamps
  property :user_id, type: Integer, constraint: :unique
  property :max_friends_count, type: Integer, default: 5
  validates :user_id, :presence => true
  has_many :out, :followings, model_class: :GraphUser, rel_class: :GraphRel, unique: true
  has_many :in, :followers, model_class: :GraphUser, rel_class: :GraphRel, unique: true
end
我已经分别创建了用户id为1和2的
user1
user2

然后我使用
user1.以下内容(相对长度:2)
。但是结果显示为
user1
本身,因为
user1
user2
都在相互跟踪


我尝试了
order(:width\u first)
和其他方法来排除已经访问过的节点。我可能没有做足够的研究,但有人知道怎么做吗?

首先,您可能想使用
id\u属性。如果您对
用户id
使用
属性
,并设置
约束::唯一
,您仍将拥有自动生成的
uuid
属性。这可能是你想要的,但只是一个提醒。以下是
id\u属性的文档

对于您的问题,您的代码生成的密码与此类似(我需要更改
model_class
rel_class
选项):

在Cypher中的单个
MATCH
子句中,Neo4j将确保单个路径遍历不会多次遍历同一关系。但正如你所说的,如果他们相互跟踪,这意味着它可以按照其他关系返回到原始用户。在这种情况下,您需要将原始用户从潜在结果中排除:

MATCH user15573
WHERE (ID(user15573) = {ID_user15573})
MATCH user15573-[rel1:`FOLLOWS`*2]->(result_followings:`User`)
WHERE result_followings <> user15573
匹配用户15573
其中(ID(user15573)={ID_user15573})
匹配user15573-[rel1:`FOLLOWS`*2]>(结果\u FOLLOWS:`User`)
其中,结果_在用户15573之后
在Ruby中,这将是:

user1.as(:source).followings(:target, nil, rel_length: 2).where('source <> target')
user1.as(:source)。以下(:target,nil,rel_length:2)。其中('source-target'))
user1.as(:source).followings(:target, nil, rel_length: 2).where('source <> target')