【问题标题】:No result when querying with Jena, but DBpedia query form returns results使用 Jena 查询时没有结果,但 DBpedia 查询表单返回结果
【发布时间】:2019-04-24 16:59:58
【问题描述】:

如果我使用 Jena 与 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

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

【问题讨论】:

  • 您的查询很糟糕,因为它必须对基本上所有具有标签的资源进行全面扫描,然后应用字符串函数。您在表单中得到一个结果是因为它偶然在默认的 30 秒时间内找到了一些匹配的资源。 3000 以毫秒为单位,因此为 3 秒。三合店不太可能在这段时间内找到任何东西

标签: sparql jena dbpedia


【解决方案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

【讨论】:

  • +1 次评论,虽然已经相当快了,但我也只选择英文标签:filter(langmatches(lang(?label1), "en"))
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-22
  • 2015-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多