Match Cypher中的多个关系 [英] Multiple relationships in Match Cypher

查看:270
本文介绍了Match Cypher中的多个关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试根据标签查找相似的电影.但是我还需要给定电影及其每个相似电影的所有标签(进行一些计算).但是令人惊讶的是,collect(h.w)给出了h.w的重复值(其中wh的属性)

Trying to find similar movies on the basis of tags. But I also need all the tags for the given movie and its each similar movie (to do some calculations). But surprisingly collect(h.w) gives repeated values of h.w (where w is a property of h)

这是密码查询.请帮忙.

Here is the cypher query. Please help.

MATCH (m:Movie{id:1})-[h1:Has]->(t:Tag)<-[h2:Has]-(sm:Movie),
(m)-[h:Has]->(t0:Tag), 
(sm)-[H:Has]->(t1:Tag) 
WHERE m <> sm 
RETURN distinct(sm), collect(h.w)

基本上是

MATCH (x)-[h]->(y), (a)-[H]->(b) 
RETURN h

返回h n times的每个结果,其中nresults for H的编号.可以解决这个问题吗?

is returning each result for h n times where n is the number of results for H. Any way around this?

推荐答案

我为此问题复制了数据模型以帮助回答.

I replicated the data model for this question to help answer it.

然后,我使用Neo4j的在线控制台设置示例数据集: http://console.neo4j.org /?id = dakmi3

I then setup a sample dataset using Neo4j's online console: http://console.neo4j.org/?id=dakmi3

从您的问题中运行以下查询:

Running the following query from your question:

MATCH (m:Movie { title: "The Matrix" })-[h1:HAS_TAG]->(t:Tag),
      (t)<-[h2:HAS_TAG]-(sm:Movie),
      (m)-[h:HAS_TAG]->(t0:Tag),
      (sm)-[H:HAS_TAG]->(t1:Tag)
WHERE m <> sm
RETURN DISTINCT sm, collect(h.weight)

这将导致:

(1:电影{title:黑客帝国:重载"})[0.31,0.12,0.31,0.12,0.31,0.01,0.31,0.01]

(1:Movie {title:"The Matrix: Reloaded"}) [0.31, 0.12, 0.31, 0.12, 0.31, 0.01, 0.31, 0.01]

问题在于有重复的关系被返回,这导致集合中的重量重复.解决方案是使用WITH将关系限制为不同的记录,然后返回这些关系的权重集合.

The issue is that there are duplicate relationships being returned, which results in duplicated weight in the collection. The solution is to use WITH to limit relationships to distinct records and then return the collection of weights of those relationships.

MATCH (m:Movie { title: "The Matrix" })-[h1:HAS_TAG]->(t:Tag),
      (t)<-[h2:HAS_TAG]-(sm:Movie),
      (m)-[h:HAS_TAG]->(t0:Tag),
      (sm)-[H:HAS_TAG]->(t1:Tag)
WHERE m <> sm
WITH DISTINCT sm, h
RETURN sm, collect(h.weight)

(1:电影{title:黑客帝国:重载"})[0.31,0.12,0.01]

(1:Movie {title:"The Matrix: Reloaded"}) [0.31, 0.12, 0.01]

这篇关于Match Cypher中的多个关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆