neo4j - cypher find relation direction -
how can find relation direction regards containing path? need weighted graph search takes account relation direction (weighing "wrong" direction 0
, see comments).
lets say:
start a=node({param}) match a-[*]-b a, b match p = allshortestpaths(a-[*]-b) return extract(r in rels(p): flows_with_path(r)) in_flow
where flows_with_path = 1
if sp = (a)-[*0..]-[r]->[*0..]-(b)
, otherwise 0
edit: corrected query
so, here's way existing cypher functions. don't promise it's super performant, give shot. we're building our collection reduce, using accumulator tuple collection , last node looked at, can check it's connected next node. requires 2.0's case/when syntax--there may way in 1.9 it's more complex.
start a=node:node_auto_index(name="trinity") match a-[*]-b <> b distinct a,b match p = allshortestpaths(a-[*]-b) return extract(x in nodes(p): x.name?), // concise representation of path we're checking head( reduce(acc=[[], head(nodes(p))], x in tail(nodes(p)): // pop first node off, traverse tail case when (y in tail(acc) y-->x) // bit of hack because tail(acc)-->x doesn't parse right, had wrap can have bare identifier in pattern predicate [head(acc) + 0, x] // add 0 our accumulator collection else [head(acc) + 1, x] // add 1 our accumulator collection end )) in_line
http://console.neo4j.org/r/v0jx03
output:
+---------------------------------------------------------------------------+ | extract(x in nodes(p): x.name?) | in_line | +---------------------------------------------------------------------------+ | ["trinity","morpheus"] | [1] | | ["trinity","morpheus","cypher"] | [1,0] | | ["trinity","morpheus","cypher","agent smith"] | [1,0,0] | | ["trinity","morpheus","cypher","agent smith","the architect"] | [1,0,0,0] | | ["trinity","neo"] | [1] | | ["trinity","neo",<null>] | [1,1] | +---------------------------------------------------------------------------+
note: @boggle brainstorming session.
Comments
Post a Comment