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:加权关系,如何在查询中反映重要关系?_Neo4j_Cypher - Fatal编程技术网

Neo4j:加权关系,如何在查询中反映重要关系?

Neo4j:加权关系,如何在查询中反映重要关系?,neo4j,cypher,Neo4j,Cypher,我有一些数据,其中一些关系比其他关系重要得多,并通过加权来表示。 例如,我有这样的图表: (city_a:City)-[:has{weight:10}]->(casino:Event) (city_a:City)-[:has{weight:1}]->(restaurant:Event) (city_a:City)-[:has{weight:30}]->(university:Event) (city_a:City)-[:has{weight:25}]-&

我有一些数据,其中一些关系比其他关系重要得多,并通过加权来表示。 例如,我有这样的图表:

  (city_a:City)-[:has{weight:10}]->(casino:Event)
  (city_a:City)-[:has{weight:1}]->(restaurant:Event)
  (city_a:City)-[:has{weight:30}]->(university:Event)    
  (city_a:City)-[:has{weight:25}]->(library:Event) 
   ......
  (city_b:City)-[:has{weight:2}]->(casino:Event)
  (city_b:City)-[:has{weight:2}]->(restaurant:Event)
  (city_b:City)-[:has{weight:5}]->(university:Event)    
  (city_b:City)-[:has{weight:10}]->(library:Event) 
   ......  
我有一个输入,如下所示

  Input: {casino, restaurant, university}
我应该输出

  Output: city_a 
这是一个答案,因为它的大多数关系比其他城市更重要

(也许我的图形模型不正确,但我想不出什么东西。建议非常受欢迎)

那么,如何为这种情况编写密码查询呢


提前谢谢

以下是一个基于您描述的示例图,其中加入了“city_c”,以涵盖输入事件少于所需事件的城市:

merge (city_a:City{name:'city_a'})
merge (city_b:City{name:'city_b'})
merge (city_c:City{name:'city_c'})

merge (casino:Event{name:'casino'})
merge (restaurant:Event{name:'restaurant'})
merge (university:Event{name:'university'})
merge (library:Event{name:'library'})

merge (city_a)-[:has{weight:10}]->(casino)
merge (city_a)-[:has{weight:1}]->(restaurant)
merge (city_a)-[:has{weight:30}]->(university)    
merge (city_a)-[:has{weight:25}]->(library) 

merge (city_b)-[:has{weight:2}]->(casino)
merge (city_b)-[:has{weight:2}]->(restaurant)
merge (city_b)-[:has{weight:5}]->(university)    
merge (city_b)-[:has{weight:10}]->(library) 

merge (city_c)-[:has{weight:100}]->(university) 
我们还需要一个关于:Event(name)的索引,以便通过输入字符串进行快速查找(您可能还需要一个关于:City(name)的索引,尽管这不需要,也不用于此特定查询)

由此,我们可以创建查询,将输入事件与这些事件进行匹配,并按每个城市匹配事件的数量和评分总和排序

而不是用。。。作为输入您希望参数化查询

with ['casino', 'restaurant', 'university'] as input
match (e:Event)<-[r:has]-(city:City)
where e.name in input
with city, count(r) as eventCount, sum(r.weight) as weightSum
return city, eventCount, weightSum
order by eventCount desc, weightSum desc
limit 1
以['casino'、'restaurant'、'university']作为输入

匹配(e:Event)以下是一个基于您描述的示例图,其中加入了“city_c”以覆盖输入事件少于所需事件的城市:

merge (city_a:City{name:'city_a'})
merge (city_b:City{name:'city_b'})
merge (city_c:City{name:'city_c'})

merge (casino:Event{name:'casino'})
merge (restaurant:Event{name:'restaurant'})
merge (university:Event{name:'university'})
merge (library:Event{name:'library'})

merge (city_a)-[:has{weight:10}]->(casino)
merge (city_a)-[:has{weight:1}]->(restaurant)
merge (city_a)-[:has{weight:30}]->(university)    
merge (city_a)-[:has{weight:25}]->(library) 

merge (city_b)-[:has{weight:2}]->(casino)
merge (city_b)-[:has{weight:2}]->(restaurant)
merge (city_b)-[:has{weight:5}]->(university)    
merge (city_b)-[:has{weight:10}]->(library) 

merge (city_c)-[:has{weight:100}]->(university) 
我们还需要一个关于:Event(name)的索引,以便通过输入字符串进行快速查找(您可能还需要一个关于:City(name)的索引,尽管这不需要,也不用于此特定查询)

由此,我们可以创建查询,将输入事件与这些事件进行匹配,并按每个城市匹配事件的数量和评分总和排序

而不是用。。。作为输入
您希望参数化查询

with ['casino', 'restaurant', 'university'] as input
match (e:Event)<-[r:has]-(city:City)
where e.name in input
with city, count(r) as eventCount, sum(r.weight) as weightSum
return city, eventCount, weightSum
order by eventCount desc, weightSum desc
limit 1
以['casino'、'restaurant'、'university']作为输入

比赛(e:事件)我假设休息后的城市应该是“城市b”而不是“城市a”?另外,请确保您正确使用了标签和属性,您添加的样本(如果按原样使用)仅包含变量,这些变量在查询结束后丢失,因此这些都将是相互连接的空白节点。此外,是否要求城市具有输入指定的所有项目,或者只能具有子集,“这意味着如果一个城市的重量非常高,那么它的产出可能是一个只有赌场的城市?”InverseFalcon。谢谢你指出我的错误,我改正了。它需要输出一个城市列表,从指定了尽可能多的项目的顶级城市到指定了少量低权重项目的低端城市。@InverseFalcon。我不确定这是否是为此类需求构建图形的正确方法。您可能需要再次阅读开发人员文档。您的修复添加了标签,这是一个开始,但您的“city_a”、“city_b”、“casino”等仍然是变量,不会保存到数据库中。您需要将这些作为节点的属性。变量只能用于处理查询中的某些元素或值,它们在查询结束后不会保持不变。我假设中断后的城市应该是“city_b”而不是“city_a”?另外,请确保您正确使用了标签和属性,您添加的样本(如果按原样使用)仅包含变量,这些变量在查询结束后丢失,因此这些都将是相互连接的空白节点。此外,是否要求城市具有输入指定的所有项目,或者只能具有子集,“这意味着如果一个城市的重量非常高,那么它的产出可能是一个只有赌场的城市?”InverseFalcon。谢谢你指出我的错误,我改正了。它需要输出一个城市列表,从指定了尽可能多的项目的顶级城市到指定了少量低权重项目的低端城市。@InverseFalcon。我不确定这是否是为此类需求构建图形的正确方法。您可能需要再次阅读开发人员文档。您的修复添加了标签,这是一个开始,但您的“city_a”、“city_b”、“casino”等仍然是变量,不会保存到数据库中。您需要将这些作为节点的属性。变量仅用于处理查询中的某些元素或值,它们在查询结束后不会持久化。