query-56d87a736e780184353a4a1ffcfd52dd

rq turtle/ttl

Timeout when finding distance between two entitiesThis code is supposed to return the distance between two nodes in Wikidata. Essentially it gets the paths from A and from B to their nearest common ancestor.

Use at

PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://www.wikidata.org/entity/>
select ?a ?b ?super (?aLength + ?bLength as ?length)
{
  values (?a ?b) { (wd:Q5 wd:Q349) }
  { 
    select ?a ?super (count(?mid) as ?aLength) { 
      ?a wdt:P279* ?mid .
      ?mid wdt:P279+ ?super .
    }
    group by ?a ?super
  }
  { 
    select ?b ?super (count(?mid) as ?bLength) { 
      ?b wdt:P279* ?mid .
      ?mid wdt:P279+ ?super .
    }
    group by ?b ?super
  }
}
order by ?length
limit 1

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v2("?a"):::projected v6("?aLength"):::projected v3("?b"):::projected v7("?bLength"):::projected v8("?length") v4("?mid") v5("?super"):::projected bind0[/VALUES ?a ?b/] bind0-->v2 bind0-->v3 bind00(["wd:Q5"]) bind00 --> bind0 bind01(["wd:Q349"]) bind01 --> bind0 v2 --"wdt:P279"--> v4 v4 --"wdt:P279"--> v5 bind2[/"count(?mid)"/] v4 --o bind2 bind2 --as--o v6 v3 --"wdt:P279"--> v4 v4 --"wdt:P279"--> v5 bind4[/"count(?mid)"/] v4 --o bind4 bind4 --as--o v7 bind5[/"?aLength + ?bLength"/] v6 --o bind5 v7 --o bind5 bind5 --as--o v8