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



如果我使用 Jena 与查询表单,结果就不一样了 http://dbpedia.org/sparql

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

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. 使用默认图形 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')

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

最后,您应该考虑修复代码对SPARQL注入的漏洞。

最新更新