How the pass the output of one sparql query as a i

2019-08-09 00:09发布

I am trying get the dbpedia movie link using the movie name in the first query and pass that link in the second query to get the movies similar to this movie.For e.g Lagaan.Now instead of passing the link manually in the second query is there a way to combine the two queries and pass the output of first query as an input to the second query.i.e:the link of the movie lagaan.Also,if the first query gives multiple links eg:if i am searching for Harry potter it will return multiple harry potter series links so,it should handle that case as well.

Query1

prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
        prefix dbpedia-owl: <http://dbpedia.org/ontology/>

          select distinct ?film where  {
          ?film a dbpedia-owl:Film .
          ?film rdfs:label ?label .
          filter regex( str(?label), "Lagaan", "i")
          }
          limit 10

Query 2

PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
        select ?similar (count(?p) as ?similarity) where { 
          values ?movie { <http://dbpedia.org/resource/Lagaan> }
          ?similar ?p ?o ; a dbpedia-owl:Film .
          ?movie   ?p ?o .
        }
        group by ?similar ?movie
        having count(?p) > 35
        order by desc(?similarity)

Edited query:

  select ?film ?similar (count(?p) as ?similarity) where {

  { 
          select distinct ?film where  {
          ?film a dbpedia-owl:Film .
          ?film rdfs:label ?label .
          filter regex( str(?label), "Lagaan", "i")
          } 
  }

         ?similar ?p ?o ; a dbpedia-owl:Film .
         ?film  ?p ?o .
        }
        group by ?similar ?film
        having count(?p) > 35
        order by desc(?similarity)

corrected query as told by Joshua Taylor

select ?film ?other (count(*) as ?similarity) {
  {
    select ?film where {
      ?film a dbpedia-owl:Film ; rdfs:label ?label .
      filter contains(lcase(?label),"lagaan")
    }
    limit 1
  }
  ?film ?p ?o .
  ?other a dbpedia-owl:Film ; ?p ?o .
}
group by ?film ?other
having count(?p) > 25
order by desc(?similarity)

1条回答
不美不萌又怎样
2楼-- · 2019-08-09 00:52

is there a way to combine the two queries and pass the output of first query as an input to the second query.

SPARQL 1.1 defines subqueries. The results of inner queries are available to outer queries, so they are "passed" to them. In your case, you would have something along the lines of:

select ?similarMovie (... as ?similarity) where {

  { #-- QUERY 1, find one or more films
    select distinct ?film where {
      #-- ...
    }
  }

  #-- QUERY 2, find films similar to ?film
  #-- ...
}
查看更多
登录 后发表回答