2

Currently I use the following Cypher / APOC query to search the relationship type TO by a certain property (user id):

CALL apoc.index.relationships('TO','user:16c01100-aa92-11e3-a3f6-35e25c9775ff') YIELD rel, start, end
WITH DISTINCT rel, start, end
MATCH (ctx:Context)
WHERE rel.context = ctx.uid 
ETURN DISTINCT start.uid AS source_id,
start.name AS source_name,
end.uid AS target_id,
end.name AS target_name,
rel.uid AS edge_id,
ctx.name AS context_name,
rel.statement AS statement_id,
rel.weight AS weight;

I want to make it posible to not only search the index TO but also the index AT (AT type of relationships), so that the resulting rel parameter contains both TO and AT relationships.

I would imagine it would be something as simple as adding an OR operator, like this:

CALL apoc.index.relationships('TO','user:16c01100-aa92-11e3-a3f6-35e25c9775ff') OR
apoc.index.relationships('AT','user:16c01100-aa92-11e3-a3f6-35e25c9775ff') YIELD rel, start, end
WITH DISTINCT rel, start, end
MATCH (ctx:Context)
WHERE rel.context = ctx.uid
RETURN DISTINCT start.uid AS source_id,
start.name AS source_name,
end.uid AS target_id,
end.name AS target_name,
rel.uid AS edge_id,
ctx.name AS context_name,
rel.statement AS statement_id,
rel.weight AS weight;

But it doesn't work...

Maybe there's somethign I could do that I get the rels from these two apocs and then simply conflate them together into one rel but I don't really get how to do that... Or maybe there's an easier way I don't see?

Bruno Peres
  • 15,845
  • 5
  • 53
  • 89
Aerodynamika
  • 7,883
  • 16
  • 78
  • 137

2 Answers2

1

What about the use of UNION clause:

CALL apoc.index.relationships('TO','user:16c01100-aa92-11e3-a3f6-35e25c9775ff') YIELD rel, start, end
WITH DISTINCT rel, start, end
MATCH (ctx:Context)
WHERE rel.context = ctx.uid
RETURN DISTINCT start.uid AS source_id,
start.name AS source_name,
end.uid AS target_id,
end.name AS target_name,
rel.uid AS edge_id,
ctx.name AS context_name,
rel.statement AS statement_id,
rel.weight AS weight
UNION
CALL apoc.index.relationships('AT','user:16c01100-aa92-11e3-a3f6-35e25c9775ff') YIELD rel, start, end
WITH DISTINCT rel, start, end
MATCH (ctx:Context)
WHERE rel.context = ctx.uid
RETURN DISTINCT start.uid AS source_id,
start.name AS source_name,
end.uid AS target_id,
end.name AS target_name,
rel.uid AS edge_id,
ctx.name AS context_name,
rel.statement AS statement_id,
rel.weight AS weight
Bruno Peres
  • 15,845
  • 5
  • 53
  • 89
0

You can do it with run cypher fragment:

WITH '16c01100-aa92-11e3-a3f6-35e25c9775ff' as userId

CALL apoc.cypher.run("
     CALL apoc.index.relationships('TO','user:' + $id) YIELD rel
     RETURN rel
", {id: userId}) YIELD value
WITH userId, collect(value.rel) as r1

CALL apoc.cypher.run("
     CALL apoc.index.relationships('AT','user:' + $id) YIELD rel
     RETURN rel
", {id: userId}) YIELD value
WITH r1, collect(value.rel) as r2

UNWIND (r1 + r2) as rel
WITH rel, nodeStart(rel) as start, nodeEnd(rel) as end
MATCH (ctx:Context)
WHERE rel.context = ctx.uid
RETURN DISTINCT start.uid AS source_id,
                start.name AS source_name,
                end.uid AS target_id,
                end.name AS target_name,
                rel.uid AS edge_id,
                ctx.name AS context_name,
                rel.statement AS statement_id,
                rel.weight AS weight;
stdob--
  • 28,222
  • 5
  • 58
  • 73