简体   繁体   中英

How the pass the output of one sparql query as a input to another sparql query

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 eg 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.ie: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)

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
  #-- ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM