我试图在dbpedia中获取电影,其中包含与给定电影相同的主题,导演和演员。搜索是通过电影的名称进行的。这是我的最终查询:
SELECT (SUM(?quant) as ?final) ?movie {
{
SELECT distinct ?quant ?movie
WHERE {
SELECT ?movie ?name (4*count(?movie) as ?quant)
WHERE {
?movie rdf:type <http://dbpedia.org/ontology/Film>;
foaf:name ?name;
rdfs:label ?film_title;
dbo:director ?director.
{
SELECT DISTINCT ?director
WHERE {
?movie rdf:type <http://dbpedia.org/ontology/Film>;
rdfs:label ?film_title;
foaf:name ?name;
dbo:director ?director.
FILTER (lang(?film_title) = 'en')
FILTER regex(?name, 'Fast and Furious')
}
}
FILTER (lang(?film_title) = 'en')
}
GROUP BY ?movie ?name
ORDER BY DESC(?quant)
}
}
UNION {
SELECT distinct ?quant ?movie
WHERE{
SELECT ?movie ?name (count(?movie) as ?quant)
WHERE {
?movie rdf:type <http://dbpedia.org/ontology/Film>;
foaf:name ?name;
rdfs:label ?film_title;
dct:subject ?subject.
{
SELECT DISTINCT ?subject
WHERE {
?movie rdf:type <http://dbpedia.org/ontology/Film>;
rdfs:label ?film_title;
foaf:name ?name;
dct:subject ?subject.
FILTER (lang(?film_title) = 'en')
FILTER regex(?name, 'Fast and Furious')
}
}
FILTER (lang(?film_title) = 'en')
}
GROUP BY ?movie ?name
ORDER BY DESC(?quant)
}
}
UNION {
SELECT distinct ?quant ?movie
WHERE{
SELECT ?movie ?name (2*count(?movie) as ?quant)
WHERE {
?movie rdf:type <http://dbpedia.org/ontology/Film>;
foaf:name ?name;
rdfs:label ?film_title;
dbo:starring ?starring.
{
SELECT DISTINCT ?starring
WHERE {
?movie rdf:type <http://dbpedia.org/ontology/Film>;
rdfs:label ?film_title;
foaf:name ?name;
dbo:starring ?starring.
FILTER (lang(?film_title) = 'en')
FILTER regex(?name, 'Fast and Furious')
}
}
FILTER (lang(?film_title) = 'en')
}
GROUP BY ?movie ?name
ORDER BY DESC(?quant)
}
}
}
GROUP BY ?movie
ORDER BY DESC(?final)
LIMIT 11
它在这里工作:https://dbpedia.org/sparql
我在此处验证时不会返回任何错误:http://www.sparql.org/query-validator.html
但是,当我尝试在Jena中运行它时,它什么都不返回。这是我的方法:
public List<String> getRankedMovies(final String movieName) {
List<String> rankedMovies = new ArrayList<>();
final String header = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "+
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"+
"PREFIX foaf: <http://xmlns.com/foaf/0.1/> "+
"PREFIX dct: <http://purl.org/dc/terms/> "+
"PREFIX dbo: <http://dbpedia.org/ontology/> ";
final String selectSum = "SELECT (SUM(?quant) as ?final) ?movie {";
final String selectDirectorOccurency = "{ "+
" SELECT distinct ?quant ?movie "+
" WHERE {"+
" SELECT ?movie ?name (4*count(?movie) as ?quant) "+
" WHERE {"+
" ?movie rdf:type <http://dbpedia.org/ontology/Film>;"+
" foaf:name ?name;"+
" rdfs:label ?film_title;"+
" dbo:director ?director."+
" {"+
" SELECT DISTINCT ?director "+
" WHERE {"+
" ?movie rdf:type <http://dbpedia.org/ontology/Film>;"+
" rdfs:label ?film_title;"+
" foaf:name ?name;"+
" dbo:director ?director."+
" FILTER (lang(?film_title) = 'en')"+
" FILTER regex(?name, \""+movieName+"\")"+
" }"+
" }"+
" FILTER (lang(?film_title) = 'en')"+
" }"+
" GROUP BY ?movie ?name "+
" ORDER BY DESC(?quant)"+
" }"+
"}";
final String union = "UNION {";
final String selectSubjectOccurency = "SELECT distinct ?quant ?movie "+
"WHERE {"+
" SELECT ?movie ?name (count(?movie) as ?quant) "+
" WHERE { "+
" ?movie rdf:type <http://dbpedia.org/ontology/Film>;"+
" foaf:name ?name;"+
" rdfs:label ?film_title;"+
" dct:subject ?subject."+
" {"+
" SELECT DISTINCT ?subject "+
" WHERE { "+
" ?movie rdf:type <http://dbpedia.org/ontology/Film>;"+
" rdfs:label ?film_title;"+
" foaf:name ?name;"+
" dct:subject ?subject."+
" FILTER (lang(?film_title) = 'en')"+
" FILTER regex(?name, \""+movieName+"\")"+
" }"+
" }"+
" FILTER (lang(?film_title) = 'en')"+
" }"+
" GROUP BY ?movie ?name "+
" ORDER BY DESC(?quant)"+
"}"+
"}";
final String selectStarOccurency = "SELECT distinct ?quant ?movie "+
"WHERE {"+
" SELECT ?movie ?name (2*count(?movie) as ?quant) "+
" WHERE { "+
" ?movie rdf:type <http://dbpedia.org/ontology/Film>;"+
" foaf:name ?name;"+
" rdfs:label ?film_title;"+
" dbo:starring ?starring."+
" {"+
" SELECT DISTINCT ?starring "+
" WHERE {"+
" ?movie rdf:type <http://dbpedia.org/ontology/Film>;"+
" rdfs:label ?film_title;"+
" foaf:name ?name;"+
" dbo:starring ?starring."+
" FILTER (lang(?film_title) = 'en')"+
" FILTER regex(?name, \""+movieName+"\")"+
" }"+
" }"+
" FILTER (lang(?film_title) = 'en')"+
" }"+
" GROUP BY ?movie ?name"+
" ORDER BY DESC(?quant)"+
"}";
final String queryFinal = "}"+
"}"+
"GROUP BY ?movie "+
"ORDER BY DESC(?final) "+
"LIMIT 11";
final String finalQuery = header+selectSum+selectDirectorOccurency+union+selectSubjectOccurency+union+selectStarOccurency+queryFinal;
Query query = QueryFactory.create(finalQuery);
QueryExecution qexec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query);
ResultSet results = qexec.execSelect();
System.out.println(finalQuery);
while(results.hasNext()) {
final QuerySolution soln = results.nextSolution();
System.out.println(soln.toString());
}
qexec.close();
return rankedMovies;
}
ResultSet中没有任何内容。我几乎可以肯定这个查询是有效的,因为它会在Virtuoso中返回我想要的内容。我在同一个项目中做了其他查询,但工作正常。那么......可能导致这个问题的原因是什么?我在SPARQL中有点新功能,我将不胜感激!