Nitin
Nitin

Reputation: 2642

SPARQL union not merging the results

I have a sparql query which is the union of three individual queries. On the union i apply a filter. Although my individual queries return rows, but the union doesn't return any rows. I have checked that the output of individual queries have rows which satisfy the outer filter criteria.

PREFIX dcterms: <http://purl.org/dc/terms/> 
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/> 
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>  
PREFIX yago: <http://dbpedia.org/class/yago/> 

SELECT * 
where{

       {
    SELECT (?LandingURI) AS ?URI ?category
        WHERE {
                        {
                         ?LandingURI rdfs:label ?term .
                         ?LandingURI (dcterms:subject|rdf:type) ?category .
                        }
                FILTER((?term ="Roger"@en))
                     }
       }
       UNION
       {
       SELECT (?redirects) AS ?URI ?category
                WHERE {

                         {
                         ?LandingURI rdfs:label ?term .
                         ?LandingURI <http://dbpedia.org/ontology/wikiPageRedirects> ?redirects .
                         ?redirects (dcterms:subject|rdf:type) ?category .
                         }          
                        FILTER((?term ="Roger"@en))         
              }
       }
       UNION
       {
       SELECT (?disambiguates) AS ?URI ?category
                WHERE {

                         {
                         ?LandingURI rdfs:label ?term .
                         ?LandingURI <http://dbpedia.org/ontology/wikiPageDisambiguates> ?disambiguates .
                         ?disambiguates (dcterms:subject|rdf:type) ?category .
                         }          
                        FILTER((?term ="Roger"@en))         
              }
       }
FILTER (?category = dbpedia-owl:TennisPlayer || 
        ?category = yago:TennisOrganisations ||
        ?category = <http://dbpedia.org/resource/Category:The_Championships,_Wimbledon>
       ) 
}

Upvotes: 0

Views: 449

Answers (1)

Joshua Taylor
Joshua Taylor

Reputation: 85853

I don't know what software you're using to process your query, but it shouldn't accept that query at all; it's not well-formed. You can check your query at sparql.org's query validator.

The bracketing in your query makes it hard to tell exactly where the union is placed, but just in case it's not , union should appear between two group graph patterns, e.g.: { ... } UNION { ... }.

All that said, there's actually no need to use union (or filter) here at all. You can do this all with property paths and values. If I understand it correctly, your query can be rewritten as:

select ?uri ?term ?category where {
  #-- acceptable values of ?category
  values ?category {
    dbpedia-owl:TennisPlayer
    yago:TennisOrganisations
    <http://dbpedia.org/resource/Category:The_Championships,_Wimbledon>
  }

  #-- acceptable values of ?term
  values ?term {
    "Roger"@en
  }

  #-- get the rdfs:label of the ?landingUri, then
  #-- follow an optional redirect or disambiguation
  #-- link to the actual uri, and then get any
  #-- dcterms:subject or rdf:type from the actual uri.
  ?landingUri rdfs:label ?term .
  ?landingUri (dbpedia-owl:wikiPageRedirects|dbpedia-owl:wikiPageDisambiguates)? ?uri
  ?uri (dcterms:subject|rdf:type) ?category .
}

Upvotes: 3

Related Questions