Jena sparql (Dbpedia) query OPTIONAL filter 没有给出结果但是 (http://dbpedia.org/snorql/) 相同的查询有效

Jena sparql (Dbpedia) query OPTIONAL filter gives no results but (http://dbpedia.org/snorql/) same query works

你好,这个查询在这个网站上工作 http://dbpedia.org/snorql/

   PREFIX ontology: <http://dbpedia.org/ontology/>

SELECT DISTINCT ?author ?name ?wiki  ?desc ?thumb WHERE {
?author a ontology:Book;
rdfs:label ?name;
ontology:wikiPageID ?wiki;
ontology:abstract ?desc.
OPTIONAL {?author <http://dbpedia.org/ontology/thumbnail> ?thumb }.
FILTER (lang(?name) = 'en')
FILTER (regex(?name,  "lo") || regex(?desc, "lo"))
FILTER (lang(?desc) = 'en').
}ORDER BY ?name LIMIT 100

但在我的 jena queryFactory java class 中,只有当我在这一行删除缩略图的可选过滤器时它才有效:

OPTIONAL {?author <http://dbpedia.org/ontology/thumbnail> ?thumb }.

这是我的 jena java 有效的方法:

private String authorQuery(String entity, String keyWord, String language) {

        return addPrefix("rdfs: <http://www.w3.org/2000/01/rdf-schema#>") +
                addPrefix("rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>") +
                addPrefix("dbpprop: <http://dbpedia.org/property/>") +
                addPrefix("ontology: <http://dbpedia.org/ontology/>") +

                addQuery("SELECT DISTINCT ?author ?name ?wiki  ?desc ?thumb WHERE {\n" +
                        "?author a ontology:" + entity + ";\n" +
                        "rdfs:label ?name;\n" +
                        "ontology:wikiPageID ?wiki;\n" +
                        "ontology:abstract ?desc;\n" +
                        "<http://dbpedia.org/ontology/thumbnail> ?thumb.\n" +
                        "FILTER (lang(?name) = '" + language + "') " +
                        "FILTER (regex(?name,  \"" + keyWord + "\") || regex(?desc,  \"" + keyWord + "\"))\n" +
                        " FILTER (lang(?desc) = '" + language + "')." +
                        "}\n" +
                        "ORDER BY ?name\n" +
                        "LIMIT 40000");
    }

但是只要我在这一行中添加可选关键字:

"<http://dbpedia.org/ontology/thumbnail> ?thumb.\n" +

没有返回结果,谁能告诉我为什么? :(

PS : 它在没有可选过滤器的情况下运行良好

编辑:整个 DbpediaQuery class

public class DbPediaQuery {
    //array of authors
    private DbPediaQuery() {
    }

    public static DbPediaQuery createDbPediaQuery() {
        return new DbPediaQuery();
    }

    public LinkedList<Entity> queryAuthor(String entity, String keyWord, String language) {
        LinkedList<Entity> temp = new LinkedList<>();
        Query query = QueryFactory.create(authorQuery(entity, keyWord, language));

        QueryExecution qexec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query);
        try {
            temp.addAll(collectAuthors(qexec.execSelect()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return temp;
    }

    private String authorQuery(String entity, String keyWord, String language) {

        return addPrefix("rdfs: <http://www.w3.org/2000/01/rdf-schema#>") +
                addPrefix("rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>") +
                addPrefix("dbpprop: <http://dbpedia.org/property/>") +
                addPrefix("ontology: <http://dbpedia.org/ontology/>") +

                addQuery("SELECT DISTINCT ?author ?name ?wiki  ?desc ?thumb WHERE {\n" +
                        "?author a ontology:" + entity + ";\n" +
                        "rdfs:label ?name;\n" +
                        "ontology:wikiPageID ?wiki;\n" +
                        "ontology:abstract ?desc;\n" +
                        "<http://dbpedia.org/ontology/thumbnail> ?thumb.\n" +
                        "FILTER (lang(?name) = '" + language + "') " +
                        "FILTER (regex(?name,  \"" + keyWord + "\") || regex(?desc,  \"" + keyWord + "\"))\n" +
                        " FILTER (lang(?desc) = '" + language + "')." +
                        "}\n" +
                        "ORDER BY ?name\n" +
                        "LIMIT 40000");
    }

    private LinkedList<Entity> collectAuthors(ResultSet results) {
        LinkedList<Entity> temp = new LinkedList<>();
        while (results.hasNext()) {
            Entity a = new Entity();
            QuerySolution row = results.next();
            String fullName = row.get("name").toString().substring(0, row.get("name").toString().indexOf("@"));
            String biography = row.get("desc").toString().substring(0, row.get("desc").toString().indexOf("@"));
            a.setTitle(fullName);
            a.setWikiID(Integer.parseInt(row.get("wiki").toString().substring(0, row.get("wiki").toString().indexOf("^"))));
            if (!row.get("thumb").toString().isEmpty())
                a.setPictureURL(row.get("thumb").toString());
            else
                a.setPictureURL("http://www.google.fr/imgres?imgurl=http%3A%2F%2Fwww.elated.com%2Fres%2FImage%2Farticles%2Fmanagement%2Fapache%2Fmaking-a-custom-error-page%2Fapache_404_default.gif&imgrefurl=https%3A%2F%2Fgithub.com%2Fjdorn%2Fphp-reports%2Fissues%2F43&h=241&w=400&tbnid=KQI5AbkkVp3-uM%3A&zoom=1&docid=6Bd7CTaQ291_UM&ei=5AU0VceoI87WPYOvgCg&tbm=isch&iact=rc&uact=3&dur=3255&page=1&start=0&ndsp=20&ved=0CDYQrQMwBw");
            a.setBiography(biography);
            temp.add(a);
            System.out.println("FAAT" + a.getTitle());
        }
        return temp;
    }

    private String addPrefix(String prefix) { return "PREFIX " + prefix + "\n"; }

    private String addQuery(String query) { return query; }
}

好的,我在记录了几件事后想通了...首先我记录了结果集的大小,使用可选过滤器它给了我更多的结果,而不是如果我没有放置可选过滤器...2383 vs 2103(没有可选) .. 然后我注意到如果

a.setPictureURL(row.get("thumb2").toString()); 为空或 null 循环不继续...它在 14 个结果后停止并且没有给我此方法的 return :

private LinkedList<Entity> collectAuthors(ResultSet results) {
        LinkedList<Entity> temp = new LinkedList<>();
        while (results.hasNext()) {
            Entity a = new Entity();
            QuerySolution row = results.next();
            String fullName = row.get("name").toString().substring(0, row.get("name").toString().indexOf("@"));
            String biography = row.get("desc").toString().substring(0, row.get("desc").toString().indexOf("@"));
            a.setTitle(fullName);
            a.setWikiID(Integer.parseInt(row.get("wiki").toString().substring(0, row.get("wiki").toString().indexOf("^"))));
            if (!row.get("thumb").toString().isEmpty())
                a.setPictureURL(row.get("thumb").toString());

            a.setBiography(biography);
            temp.add(a);
            System.out.println("FAAT" + a.getTitle());
        }
        return temp;
    }

所以我就这样用 if(row.getResource("thumb2")!=null) 检查了

    if(row.getResource("thumb")!=null)
   a.setPictureURL(row.get("thumb").toString());

然后循环继续--->问题解决

感谢大家的提示,注意 Jena 的空 reslutset 列