Keanu Paik
Keanu Paik

Reputation: 324

How to check triple exists or not?

Firstly, I was intended to express query like COUNT(?sub) AS ?sub_exists and check whether ?sub_exists is 0 or not with the count number. However, this query takes too much time so that sometimes it takes more than timeout. So I secondly tried to change the function like below. But it also takes all the time counting ?sub and even worse, no values are assigned to ?sub_exists.

Is there an alternative way to express the query to just check a triple ?sub wdt:P31/wdt:P279* ?s exists or not and get the flag of it correspondly without two much time consumption ?

SELECT ?s ?sub_exists
WHERE {
  VALUES ?s {wd:Q5}
  ?sub wdt:P31/wdt:P279* ?s
  BIND(IF(COUNT(?sub) > 0,1,0) AS ?sub_exists)
}
GROUP BY ?s ?sub_exists 

Upvotes: 2

Views: 271

Answers (1)

Gilles-Antoine Nys
Gilles-Antoine Nys

Reputation: 1481

SPARQL provides a good alternative ! ASK will return true or false if pattern exists or not, respectively.

Here is a working example :

PREFIX wdt: <http://www.wikidata.org/prop/direct/>

ASK  {
  ?sub wdt:P31/wdt:P279* ?s
}

Upvotes: 4

Related Questions