0

This query works on http://dbpedia.org/snorql/.

But it isn't working on http://wikidata.dbpedia.org/sparql.

How do I fix it so that it works in http://wikidata.dbpedia.org/sparql?

PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbpedia: <http://dbpedia.org/resource>
PREFIX dbpprop: <http://dbpedia.org/property>
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>

SELECT DISTINCT *
WHERE{
    ?city rdf:type dbo:PopulatedPlace.
    OPTIONAL {?city rdfs:label ?labelEN. FILTER ( lang(?labelEN) = 'en').}
    OPTIONAL {?city rdfs:label ?labelES. FILTER ( lang(?labelES) = 'es').}
    OPTIONAL {?city dbo:populationTotal  ?pop.}
    OPTIONAL {?city dbo:country ?country. 
              OPTIONAL {?country rdfs:label ?countryEN . FILTER ( lang(?countryEN) = 'en').}
              OPTIONAL {?country rdfs:label ?countryES . FILTER ( lang(?countryES) = 'es').}
             }
    OPTIONAL {?city geo:long ?long.}
    OPTIONAL {?city geo:lat ?lat.}

    FILTER (?pop>1000000).
}
LIMIT 100
Stanislav Kralin
  • 11,070
  • 4
  • 35
  • 58
  • Replace `OPTIONAL {?city dbo:populationTotal ?pop.}` with `?city dbo:populationTotal ?pop.` – Stanislav Kralin Jul 18 '17 at 14:14
  • @StanislavKralin That's not necessary but indeed more useful. – UninformedUser Jul 18 '17 at 19:29
  • 1
    @HumanFromEarth Your query works but because of the bunch of OPTIONALs which( are a bunch of left-joins) the query is quite expensive and leads to a timeout - in that case, Virtuoso has some anytime feature, which returns all results found in the given time. Default in the WebUI is 30 seconds, you can increase it. – UninformedUser Jul 18 '17 at 19:32
  • You can see that the query without most of the OPTIONALs returns pretty fast: `PREFIX dbo: PREFIX dbpedia: PREFIX dbpprop: PREFIX geo: SELECT DISTINCT * WHERE{ ?city rdf:type dbo:PopulatedPlace. OPTIONAL {?city dbo:populationTotal ?pop. } FILTER (?pop>1000000) } LIMIT 100` – UninformedUser Jul 18 '17 at 19:32

1 Answers1

1

The population filter seems to be failing on your query for some reason on http://wikidata.dbpedia.org/sparql
In your specific query I changed it to:

FILTER (?pop>1).

and it worked.
I also made this variant of your query that works (some cities have multiple values for dbo:populationTotal so I picked the MAX value):

PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbpedia: <http://dbpedia.org/resource>
PREFIX dbpprop: <http://dbpedia.org/property>
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>

    SELECT ?city  ?labelEN ?labelES MAX(?pop) ?countryEN ?countryES
    WHERE{
        ?city rdf:type dbo:PopulatedPlace.
        ?city dbo:city ?country.
        {?city rdfs:label ?labelEN. FILTER ( lang(?labelEN) = 'en').}
        {?city rdfs:label ?labelES. FILTER ( lang(?labelES) = 'es').}
        {?city dbo:populationTotal  ?pop.}
        OPTIONAL {?country rdfs:label ?countryEN . FILTER ( lang(?countryEN) = 'en').}
        OPTIONAL {?country rdfs:label ?countryES . FILTER ( lang(?countryES) = 'es').}
        FILTER(?pop > 1000000)
    }
    GROUP by ?city ?labelEN ?labelES ?countryEN ?countryES
Jang-Vijay Singh
  • 732
  • 3
  • 11
  • The filter does **not** fail! It's just the timeout (30s default) of Virtuoso and it returns everything back found in the given time. Increase the timeout and you'll get the results. – UninformedUser Jul 18 '17 at 19:33
  • Actually it didn't timeout on (http://wikidata.dbpedia.org/sparql) - just didn't return any results due to the structure of the original query. Give it a go... – Jang-Vijay Singh Jul 18 '17 at 19:36
  • I cannot follow you. The original SPARQL query does not return a result in the Web interface - and that's because Virtuoso returns anything that has been found in the given time. You can see that the FILTER works if you either a) increase the timeout value or b) remove all OPTIONAL clause except the one with the population property. – UninformedUser Jul 19 '17 at 07:50
  • Thank you! But how to change the resource prefixes so that the query returns data from the wikidata.dbpedia.org? – HumanFromEarth Jul 19 '17 at 07:52
  • @AKSW you might be right - but that indicates buggy behaviour - a "timeout" should result in an error message that says "timeout" (as does happen for other cases where the resultset is excessively large for the filter to be applied correctly) and not return blank results. Anyway, the variant of the query I created does return results and of course, OP could choose to use a higher timeout as required – Jang-Vijay Singh Jul 19 '17 at 10:48
  • @Jang-VijaySingh I know that this is not the first time people are confused about an empty resultset. Anyways, documentation can be found here: http://docs.openlinksw.com/virtuoso/anytimequeries/ – UninformedUser Jul 19 '17 at 11:22
  • @AKSW thanks. that makes sense as this is a public/global instance - it makes a best effort to match as many results as possible within the given time. As noted on this discussion, a private virtuoso instance can be configured to be more deterministic: https://github.com/openlink/virtuoso-opensource/issues/112 – Jang-Vijay Singh Jul 19 '17 at 16:45