通过关系 属性 值限制 Neo4j apoc.path.expand
Limit Neo4j apoc.path.expand by relationship property value
是否可以在加权 Neo4j 图中找到给定节点的 n 跳内的所有路径,并限制每个节点的前 m 个关系(按权重)returned/further 展开?
例如,给定下图:
这个查询...
MATCH (n0:Foo {n: "a"})
CALL apoc.path.expand(n0, "TO>", "", 1, 3)
YIELD path as p
WHERE ALL (x in relationships(p) where x.score > 0.02) // Additional constraint not directly related to the question
RETURN p, length(p) AS hops
ORDER BY hops
...returns:
╒═══════════════════════════════════════════════════════════════════════╤══════╕
│"p" │"hops"│
╞═══════════════════════════════════════════════════════════════════════╪══════╡
│[{"n":"a"},{"score":0.03},{"n":"d"}] │1 │
├───────────────────────────────────────────────────────────────────────┼──────┤
│[{"n":"a"},{"score":0.03},{"n":"d"},{"n":"d"},{"score":0.11},{"n":"k"}]│2 │
├───────────────────────────────────────────────────────────────────────┼──────┤
│[{"n":"a"},{"score":0.03},{"n":"d"},{"n":"d"},{"score":0.1},{"n":"j"}] │2 │
├───────────────────────────────────────────────────────────────────────┼──────┤
│[{"n":"a"},{"score":0.03},{"n":"d"},{"n":"d"},{"score":0.12},{"n":"l"}]│2 │
└───────────────────────────────────────────────────────────────────────┴──────┘
是否也可以限制来自每个节点的传出关系,例如得分前 2.
预期输出为:
╒═══════════════════════════════════════════════════════════════════════╤══════╕
│"p" │"hops"│
╞═══════════════════════════════════════════════════════════════════════╪══════╡
│[{"n":"a"},{"score":0.03},{"n":"d"}] │1 │
├───────────────────────────────────────────────────────────────────────┼──────┤
│[{"n":"a"},{"score":0.03},{"n":"d"},{"n":"d"},{"score":0.11},{"n":"k"}]│2 │
├───────────────────────────────────────────────────────────────────────┼──────┤
│[{"n":"a"},{"score":0.03},{"n":"d"},{"n":"d"},{"score":0.12},{"n":"l"}]│2 │
└───────────────────────────────────────────────────────────────────────┴──────┘
我不认为你可以一次完成,因为你必须比较每条路径中的每一步。
此外,您还必须考虑得分为 0.09,0.11,0.11,0,12 的情况
其中获得前 2 名可能 return 任意结果。
MATCH p=(n0)-[:TO*1..3]->()
// From starting node
WHERE n0.n='a'
// get for each node the scores of the outgoing reps, sort them and get the second one
// and put them in an array
WITH p,
REDUCE (array=[], n IN [x in nodes(p) WHERE (x)-[:TO]->()] |
(array
+ apoc.coll.sort([(n)-[r:TO]->() | r.score])[1])
) AS cutOffScoresByStep
WITH p,cutOffScoresByStep
// only get the paths where the score on each rel is higher than the corresponding cutOffScore
WHERE ALL (rel IN relationships(p)
WHERE rel.score >= cutOffScoresByStep[apoc.coll.indexOf(relationships(p),rel)]
)
RETURN p
是否可以在加权 Neo4j 图中找到给定节点的 n 跳内的所有路径,并限制每个节点的前 m 个关系(按权重)returned/further 展开?
例如,给定下图:
这个查询...
MATCH (n0:Foo {n: "a"})
CALL apoc.path.expand(n0, "TO>", "", 1, 3)
YIELD path as p
WHERE ALL (x in relationships(p) where x.score > 0.02) // Additional constraint not directly related to the question
RETURN p, length(p) AS hops
ORDER BY hops
...returns:
╒═══════════════════════════════════════════════════════════════════════╤══════╕
│"p" │"hops"│
╞═══════════════════════════════════════════════════════════════════════╪══════╡
│[{"n":"a"},{"score":0.03},{"n":"d"}] │1 │
├───────────────────────────────────────────────────────────────────────┼──────┤
│[{"n":"a"},{"score":0.03},{"n":"d"},{"n":"d"},{"score":0.11},{"n":"k"}]│2 │
├───────────────────────────────────────────────────────────────────────┼──────┤
│[{"n":"a"},{"score":0.03},{"n":"d"},{"n":"d"},{"score":0.1},{"n":"j"}] │2 │
├───────────────────────────────────────────────────────────────────────┼──────┤
│[{"n":"a"},{"score":0.03},{"n":"d"},{"n":"d"},{"score":0.12},{"n":"l"}]│2 │
└───────────────────────────────────────────────────────────────────────┴──────┘
是否也可以限制来自每个节点的传出关系,例如得分前 2.
预期输出为:
╒═══════════════════════════════════════════════════════════════════════╤══════╕
│"p" │"hops"│
╞═══════════════════════════════════════════════════════════════════════╪══════╡
│[{"n":"a"},{"score":0.03},{"n":"d"}] │1 │
├───────────────────────────────────────────────────────────────────────┼──────┤
│[{"n":"a"},{"score":0.03},{"n":"d"},{"n":"d"},{"score":0.11},{"n":"k"}]│2 │
├───────────────────────────────────────────────────────────────────────┼──────┤
│[{"n":"a"},{"score":0.03},{"n":"d"},{"n":"d"},{"score":0.12},{"n":"l"}]│2 │
└───────────────────────────────────────────────────────────────────────┴──────┘
我不认为你可以一次完成,因为你必须比较每条路径中的每一步。
此外,您还必须考虑得分为 0.09,0.11,0.11,0,12 的情况 其中获得前 2 名可能 return 任意结果。
MATCH p=(n0)-[:TO*1..3]->()
// From starting node
WHERE n0.n='a'
// get for each node the scores of the outgoing reps, sort them and get the second one
// and put them in an array
WITH p,
REDUCE (array=[], n IN [x in nodes(p) WHERE (x)-[:TO]->()] |
(array
+ apoc.coll.sort([(n)-[r:TO]->() | r.score])[1])
) AS cutOffScoresByStep
WITH p,cutOffScoresByStep
// only get the paths where the score on each rel is higher than the corresponding cutOffScore
WHERE ALL (rel IN relationships(p)
WHERE rel.score >= cutOffScoresByStep[apoc.coll.indexOf(relationships(p),rel)]
)
RETURN p