Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.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
Neo4j/Cypher:如果满足特定条件,则创建关系_Neo4j_Cypher - Fatal编程技术网

Neo4j/Cypher:如果满足特定条件,则创建关系

Neo4j/Cypher:如果满足特定条件,则创建关系,neo4j,cypher,Neo4j,Cypher,我在graph DB中执行求和操作:我比较两个节点,计算一个图来表示图中某些区域的相似程度,如果该图足够大,我想在节点之间创建一个关系 我有一个查询,除了检查数字是否足够大之外,它完成了所有这些;它目前还创建相似性分数为0的相似性关系,我不希望这样 我的完整密码查询有点长,所以我在这篇文章中简化了它。因此,我恐怕无法在neo4j控制台中提供示例graph db。我的图形包含中心节点,这些节点周围有关联节点和搜索的节点。如果两个中心节点具有相似的关联或搜索节点,则中心节点应获得一个关系 以下是带注

我在graph DB中执行求和操作:我比较两个节点,计算一个图来表示图中某些区域的相似程度,如果该图足够大,我想在节点之间创建一个关系

我有一个查询,除了检查数字是否足够大之外,它完成了所有这些;它目前还创建相似性分数为0的相似性关系,我不希望这样

我的完整密码查询有点长,所以我在这篇文章中简化了它。因此,我恐怕无法在neo4j控制台中提供示例graph db。我的图形包含
中心
节点,这些节点周围有
关联
节点和
搜索的
节点。如果两个中心节点具有相似的关联或搜索节点,则中心节点应获得一个关系

以下是带注释的简化语句:

MATCH (a:Center), (x:Center)
WHERE id(a) <> id(x)
OPTIONAL MATCH a-->(aff1:Affinity), x-->(aff2:Affinity)
WHERE aff1.affinityReference=aff2.affinityReference     // if the Affinity nodes have the same reference, then their Center nodes are similar
OPTIONAL MATCH a-->(search1:Search), x-->(search2:Search)
WHERE search1.searchTerm = search2.searchTerm   // if the Search nodes have the same searchTerm, then their Center nodes are similar
WITH a, x, 
SUM (CASE WHEN aff2.relative_weight IS NULL THEN 0 ELSE (aff2.relative_weight * 5) END) AS AffinityScore, // Affinity nodes have a relative weight, which shall be used in the similarity calculation.
(count(search2) * 5) AS SearchScore   // matching Search nodes shall just be counted and multiplied with 5.

OPTIONAL MATCH x-[r1:IS_SIMILAR_TO]->()  // Delete all similarity relationships for x
WITH a,x,r1,AffinityScore, SearchScore, (AffinityScore+SearchScore) AS TotalScore

DELETE r1   // delete relationship if it exists...
MERGE      // ... and create it anew.
  x-[:IS_SIMILAR_TO {
  SimilarityScore:Total,
  AffinityScore:AffinityScore,
 SearchScore:SearchScore
 }]->a

RETURN a, x, AffintyScore, SearchScore, TotalScore 
ORDER BY TotalScore DESC
匹配(a:Center),(x:Center)
其中id(a)id(x)
可选匹配a-->(aff1:关联),x-->(aff2:关联)
其中aff1.affinityReference=aff2.affinityReference//如果亲和节点具有相同的引用,则它们的中心节点相似
可选匹配a-->(搜索1:搜索),x-->(搜索2:搜索)
其中search1.searchTerm=search2.searchTerm//如果搜索节点具有相同的searchTerm,则它们的中心节点相似
用a,x,
SUM(当aff2.relative_weight为NULL时,则为0 ELSE(aff2.relative_weight*5)END)作为AffinityScore,//Affinity节点具有相对权重,应在相似度计算中使用。
(计数(search2)*5)作为SearchScore//匹配的搜索节点应仅计数并乘以5。
可选匹配x-[r1:IS_-similor_-TO]->()//删除x的所有相似关系
使用a、x、r1、AffinityScore、SearchScore(AffinityScore+SearchScore)作为TotalScore
删除r1//删除关系(如果存在)。。。
合并/。。。并重新创造它。
x-[:与{
相似性得分:总,
AffinityScore:AffinityScore,
SearchScore:SearchScore
}]->a
返回a、x、AffinityScore、SearchScore、TotalScore
按TotalScore描述排序
我试着在不同的地方介绍一个案例陈述,但显然从来没有在正确的地方。它应该去哪里


谢谢你的帮助

执行条件变异操作有一个技巧:当条件为真时,使用
CASE
语句返回长度为1的列表,否则返回空列表。之后,使用
FORACH
对该数组进行迭代,以执行
CREATE
MERGE

...
WITH a, x, AffintyScore, SearchScore, TotalScore, Total, 
  CASE AffinityScore WHEN 0 THEN [] ELSE [1] END as array
FOREACH (x in array | 
   MERGE
   x-[:IS_SIMILAR_TO {
   SimilarityScore:Total,
   AffinityScore:AffinityScore,
   SearchScore:SearchScore
   }]->a 
)
RETURN a, x, AffintyScore, SearchScore, TotalScore 
ORDER BY TotalScore DESC

这正是我所需要的为什么Cypher不能有更好的方法来做这件事呢?因为最简单的东西是给懦夫的;-)更严重的是:在