0

I have following query:

# endpoint used: https://query.wikidata.org/
#
# PREFIX wd: <http://www.wikidata.org/entity/>
# PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/resource/>
SELECT DISTINCT ?musicianLabel
WHERE {
  {
    # *** dbpedia ***
    SERVICE <http://dbpedia.org/sparql> {
    #SERVICE <http://lod.openlinksw.com/sparql> {
      ?musician a dbo:Person .
      {
        # person, who has musical genre xy
        ?musician dbo:genre ?genre .
      } UNION {
        # the member (person) of a band, which has musical genre xy
        ?musician dbo:member|wdt:associatedBand ?band .
        ?band dbo:genre ?genre .
      }
      OPTIONAL {
        ?musician <http://www.w3.org/2000/01/rdf-schema#label> ?musicianLabel .
        FILTER ( LANGMATCHES ( LANG ( ?musicianLabel ), 'en' ) )
      }
      FILTER ( ?genre IN (
        dbp:Psychedelic_pop,
        dbp:Psychedelic_rock,
        #dbp:Psychedelic_Rock,
        dbp:Acid_rock,
        dbp:Psychedelic_folk,
        dbp:Neo-psychedelia,
        dbp:Space_rock,
        dbp:Dream_pop
      ) )
    }
  } UNION {
    # *** wikidata ***
    SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
    ?musician wdt:P31 wd:Q5 ;
    {
      ?musician wdt:P136 ?genre .
    } UNION {
      ?musician wdt:P361|wdt:P463 ?band .
      ?band wdt:P136 ?genre .
    }
    FILTER ( ?genre IN (
      wd:Q383982,
      wd:Q206159,
      wd:Q236932,
      wd:Q1500364,
      wd:Q703778,
      wd:Q236913,
      wd:Q592330
    ) )
  }
}
GROUP BY ?musicianLabel
ORDER BY ASC(?musicianLabel)

My questions are:

  • How would I change the query to use http://lod.openlinksw.com/sparql instead of http://dbpedia.org/sparql in order to get more results?

  • even though I used DISTINCT and GROUP BY, there are still persons appearing more than once in the result list (i.e. "Anton Newcombe"). Why?

  • In dbpedia, a resource can have multiple URI's, some redirecting to the other. i.e. dbp:Psychedelic_Rock has resources attached, but redirects to dbp:Psychedelic_rock, which actually seems to be the URI one should use. How can I resolve this in the query, without having to manually look up with DESCRIBE dbp:Psychedelic_Rock first? Probably something with dbo:wikiPageRedirects or so?

RoRu
  • 15
  • 7
  • 2
    (1) This site likes one question per question. I suggest you break this up. (2) Running your query, I get Anton once. I don't know why you're seeing him listed twice. (3) You won't always get more *results* (meaning, entities), but you will often get more *data* (meaning, attributes of those entities, some coming from sources other than DBpedia), from lod.openlinksw.com than from dbpedia.org. – TallTed Jan 16 '19 at 16:01
  • Sorry. please use `dbp:Psychedelic_rock` instead of `dbp:Psychedelic_Rock` then you get Anton twice. – RoRu Jan 16 '19 at 16:30
  • Yes, will make 3 questions next time. This means posting the same SPARQL query 3 times - in 3 questions - as all questions have this query as the base. Not a problem? – RoRu Jan 16 '19 at 16:33
  • OK thanks for clarification, more *data* with lod.openlinksw.com than with dbpedia.org. Still, the questions remains :-) – RoRu Jan 16 '19 at 16:34
  • 1
    Please see [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) for some advice on writing questions. These three questions should not all have the same SPARQL query, when following the second article's advice. – TallTed Jan 16 '19 at 17:44
  • 2
    1. Wikidata allows federated queries to call out to a selected number of external databases. http://lod.openlinksw.com/sparql is not listed [here](https://www.mediawiki.org/wiki/Wikidata_Query_Service/User_Manual/SPARQL_Federation_endpoints) – Stanislav Kralin Jan 16 '19 at 17:56
  • 1
    The query can be simplified. For DBpedia: `?musician a dbo:Person . ?musician (dbo:member|dbo:associatedBand)?/dbo:genre ?genre .` replaces the `UNION` construct – UninformedUser Jan 16 '19 at 19:36
  • And I can see Anton Newcombe just once. Note, `DISTINCT` is the way to go, not `group by` – UninformedUser Jan 16 '19 at 19:50
  • 1
    Regarding redirects, just extend my suggested property path above, i.e. `?musician a dbo:Person . ?musician (dbo:member|dbo:associatedBand)?/dbo:genre/dbo:wikiPageRedirects? ?genre .` - ideally, you'd even follow transitive paths (`dbo:wikiPageRedirects*`), but it can be very expensive – UninformedUser Jan 16 '19 at 19:55
  • 1
    What makes this a "*faceted* SPARQL query"? Looks like ***Federated*** SPARQL to me... That said, you might find the Virtuoso Facet Browser, available for use on both the [DBpedia](http://dbpedia.org/fct) and [LOD Cloud Cache](http://lod.openlinksw.com/fct) endpoints, interesting. – TallTed Jan 17 '19 at 01:16
  • 1
    You may also find [URIBurner](http://uriburner.com/) interesting, as its [SPARQL endpoint](http://uriburner.com/sparql) allows [authenticated users](http://uriburner.com/sparql/login.vsp?action=login&res=%2Fsparql%3Fdefault-graph-uri%3D%26qtxt%3DSELECT%2520DISTINCT%2520*%2520WHERE%2520%257B%255B%255D%2520a%2520%253FEntityType%257D%2520LIMIT%252050%26should-sponge%3D%26format%3Dtext%252Fhtml%26CXML_redir_for_subjs%3D121%26CXML_redir_for_hrefs%3D%26timeout%3D30000000) to run Federated SPARQL queries over any accessible remote endpoint, and also offers [the FCT service](http://uriburner.com/fct). – TallTed Jan 17 '19 at 01:17
  • 1
    And... Listen to @AKSW about `DISTINCT` vs `GROUP BY`. Speed of the latter is generally significantly slower than the former (which is hard to demonstrate on a live, hot server). – TallTed Jan 17 '19 at 01:25
  • @TallTed: I tried URIBurner's Endpoint. Dbpedia worked. Wikidata did not work, Any idea why? i.e. simple query `PREFIX wd: PREFIX wdt: PREFIX wikibase: PREFIX bd: SELECT DISTINCT ?musician ?musicianLabel WHERE { SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } ?musician wdt:P31 wd:Q5 . ?musician wdt:P136 ?genre . FILTER(?genre IN (wd:Q383982, wd:Q206159)) } ORDER BY ASC(?musicianLabel)` – RoRu Jan 17 '19 at 08:02
  • @AKSW: nice to have it simplified like that! Would love to read about it in the specs so that I understand what it does. Does this simplification has a name so I can google it? (The `?/` part is new to me, and i would like to know what they are used for) – RoRu Jan 17 '19 at 08:07
  • 1
    ah, yeah sorry. It's called [*property paths*](https://www.w3.org/TR/sparql11-query/#propertypaths) as introduced in SPARQL 1.1. I really like it but for query optimizers it's sometimes a nightmare given the possibility of transitive paths – UninformedUser Jan 17 '19 at 08:32
  • 1
    New questions should be *new questions.* "Did not work" is very broad; please be specific. That said… Keep existing `PREFIX` declarations; change remainder of query _(note corrected Wikidata `SERVICE` URI!):_ `SELECT DISTINCT ?musician ?musicianLabel WHERE { SERVICE { SELECT ?musician ?musicianLabel WHERE { ?musician wdt:P31 wd:Q5 . ?musician wdt:P136 ?genre . FILTER ( ?genre IN ( wd:Q383982, wd:Q206159 )) SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }}}} ORDER BY ASC ( ?musicianLabel )`. – TallTed Jan 17 '19 at 14:57
  • "did not work" meant that it showed the result HTML page, including the column titles (musician, musicianLabel), but no data. – RoRu Jan 17 '19 at 16:31
  • Thanks for your solution @TallTed. Worked. Will have to find out why it is nescessary to have a SELECT within a SELECT statement, und what `SERVICE ` is for. But I won't ask another question here – RoRu Jan 17 '19 at 16:33
  • 1
    Federated `SERVICE` syntax allow you to leave out the `SELECT` keyword on simple queries, as that's the default verb, but including it allows more complex queries to be issued to the remote endpoint. `SERVICE ` is Wikidata's SPARQL endpoint. The inner `SELECT` is what goes to that `SERVICE`. – TallTed Jan 17 '19 at 16:49

0 Answers0