链接到多个并行子图匹配

时间:2015-05-28 04:41:45

标签: neo4j cypher

我有一个包含多个锻炼节点的图表。每个锻炼节点都与多种媒体,锻炼类型和身体焦点节点有关系。

以下各个查询均按预期工作并提供预期结果:

1)返回锻炼集和每个锻炼媒体:

MATCH (w:Workout)-[:hasMedia]->(m:Media) RETURN w.name as workout, collect(m.url) AS media

2)返回训练集和每种锻炼类型:

MATCH (w:Workout)-[:hasConcept]->(wt:Concept)-[:ofType]->(Category{name:"training"}) RETURN w.name AS workout, collect(wt.name) AS workoutType

3)返回一组训练,身体对每个训练集中:

MATCH (w:Workout)-[:hasConcept]->(bf:Concept)-[:ofType]->(Category{name:"bodyfocus"}) RETURN w.name AS workout, collect(bf.name) AS bodyFocus

此外,我有一组人员节点与锻炼有关系。

以下查询按预期工作并提供预期结果:

A)返回与特定人相关的训练集和原因(分数,证据):

MATCH (Person{personId:"1028"})-[r:hasAffinity]->(c:Concept)<-[s]-(w:Workout) RETURN sum(toFloat(r.score)*toFloat(s.score))/count(c) AS score, w.name AS workout, collect({text:c.name, polarity:r.score, evidenceId:c.name}) AS evidence

我尝试做的事情(并且悲惨地失败)是针对单个复合查询得出的,它回答了以下问题:&#34;返回一组人的相关训练,包括得分,证据,媒体,锻炼类型,身体专注于每次锻炼&#34;。

似乎我想:匹配{A&#39; s path} with W MATCH w- {其余的1&#39; s}},w- {其余的2&#39; s}},w- {其余的3路径}返回{A&#39的东西},{1&#39; s stuff},{2&#39; s stuff},{3&#39; s}}

...但我无法解决这个问题(因为新手可能对此方法有误)。帮助

1 个答案:

答案 0 :(得分:0)

我认为这是您在一个查询中组合所有内容所需要的:

// get the Workouts of a Person via the Concept
MATCH (p:Person {personId:"1028"})-[r:hasAffinity]->(c:Concept)<-[s]-(w:Workout)

// take the Person and Workout to the next step, calculate score and collect evidence
WITH DISTINCT p, w, sum(toFloat(r.score)*toFloat(s.score))/count(c) AS score, 
     collect({text:c.name, polarity:r.score, evidenceId:c.name}) AS evidence

// Match everything else for the workout
MATCH (w)-[:hasConcept]->(wt:Concept)-[:ofType]->(:Category {name:"training"})
MATCH (w)-[:hasConcept]->(bf:Concept)-[:ofType]->(:Category {name:"bodyfocus"})
MATCH (w)-[:hasMedia]->(m:Media)

// Return everything (score and evidence are available from the WITH statement)
RETURN p.personID, w.name AS workout, collect(wt.name) AS workoutType, 
    collect(bf.name) AS bodyFocus, collect(m.url) AS media, score, evidence