Discussion Forum

Finding two-hop relations in Knowledge Graph

Hey everyone,

I would like to get to know whether there is a way to write a query or use the Concept/Client API in order to find all two-hop relations between two entity types. So for example I want to get a way from entity type A to C, where both of them are not directly connected. Type A is attached to type B and type B is attached to type C. For that case I would like to get all relations and entity types within the way from A to C, that fulfill my requirements.

Thanks in advance!

Hey Robert!

Given you have a schema similar to this (three entities - a, b and c; a and b being connected via ab relation and b and c being connected with bc relation:

define a sub entity; b sub entity; c sub entity;
define ab sub relation, relates a, relates b;
define bc sub relation, relates b, relates c;
define a plays ab:a;
define b plays ab:b;
define b plays bc:b;
define c plays bc:c;

you can use a query like this one to figure how $a and $c are related:

match $a isa entity; $b isa entity; $c isa entity; $relab($a, $b) isa relation; $relbc($b, $c) isa relation;

Notice that the query uses base types (entity and relation) and doesn’t name specific types (a, b, c, ab and bc).