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

问题描述 投票:1回答:1

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

我在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

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

sparql jena dbpedia
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.