使用Jena查询时没有结果,但是DBpedia查询表单返回结果

时间:2019-04-24 16:59:58

标签: sparql jena dbpedia

如果我在http://dbpedia.org/sparql上使用耶拿vs查询表单,结果将不一样

我在耶拿(Jena)中的代码(我尝试返回两个列表,其中包含搜索到的文本名称的类型):

s1 = "Ketolide";
s2 = "Aminocoumarin";
String sparqlQueryString1 = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>" +
                        "SELECT  distinct ?type1 " +
                        "WHERE { ?data  rdfs:label ?label1. ?data rdf:type ?type1.   FILTER contains(lcase(str(?label1)),'" + s1.toLowerCase()  + "'). }";

Query query = QueryFactory.create(sparqlQueryString1);

QueryEngineHTTP objectToExec = QueryExecutionFactory.createServiceRequest("http://dbpedia.org/sparql", query);

objectToExec.addParam("timeout","3000");
ResultSet results = objectToExec.execSelect();
List<QuerySolution> s = ResultSetFormatter.toList(results);
ResultSetFormatter.out(System.out, results, query);

sparqlQueryString1 = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> " +
                        "SELECT distinct ?type1 " +
                        "WHERE {?data  rdfs:label ?label1. ?data rdf:type ?type1.  FILTER contains(lcase(str(?label1)),'" + s2.toLowerCase()  + "'). }";

query = QueryFactory.create(sparqlQueryString1);

objectToExec = QueryExecutionFactory.createServiceRequest("http://dbpedia.org/sparql", query);
objectToExec.addParam("timeout","3000");
results = objectToExec.execSelect();
List<QuerySolution> s22 = ResultSetFormatter.toList(results);
ResultSetFormatter.out(System.out, results, query);

当我在http://dbpedia.org/sparql的查询表单中使用相同的查询时,它会得到结果:

SELECT distinct ?type1 WHERE{ ?data rdf:type ?type1. ?data  rdfs:label  ?label1 .    FILTER contains(lcase(str(?label1)), 'ketolide') .}

这将返回:

type1

http://dbpedia.org/ontology/ChemicalCompound
http://dbpedia.org/class/yago/WikicatKetolideAntibiotics
http://dbpedia.org/class/yago/Agent114778436
http://dbpedia.org/class/yago/Antibacterial102716205
http://dbpedia.org/class/yago/Antibiotic102716866
http://dbpedia.org/class/yago/CausalAgent100007347
http://dbpedia.org/class/yago/Drug103247620
http://dbpedia.org/class/yago/Matter100020827
http://dbpedia.org/class/yago/Medicine103740161
http://dbpedia.org/class/yago/PhysicalEntity100001930
http://dbpedia.org/class/yago/Substance100020090
http://dbpedia.org/class/yago/WikicatAntibiotics

这种差异的原因和原因是什么?

1 个答案:

答案 0 :(得分:1)

我可以发现两个差异。

  1. 使用默认图形IRI::首先,位于http://dbpedia.org/sparql的查询表单将默认图形IRI设置为http://dbpedia.org。您的代码无法做到这一点。因此,您的代码将针对数据库中的所有图形运行,而不仅仅是针对DBpedia图形。要将相同的默认图形添加到您的查询中,这应该可行:

    objectToExec.addDefaultGraph("http://dbpedia.org");
    

    (我不知道端点还有其他图形,所以我不知道这实际上有多大区别。)

  2. 不同的超时时间:其次,您的代码将超时时间设置为3000,而查询表单将超时时间设置为30000。此特定端点配置为在命中时返回到目前为止找到的任何内容超时,因此,如果3秒钟后仍未找到任何内容,它将返回无结果。查询表单将使查询运行30秒。

话虽如此,使用bif:contains可以更有效地完成全文搜索:

FILTER bif:contains(?label1, 'ketolide')

这使用全文索引,这比扫描数据库中的所有字符串快得多。

最后,您应该考虑使用fixing the code's vulnerability to SPARQL injection