如何编写分页 SPARQL 查询以获取具有多个相同节点的产品列表

问题描述

我想使用 SPARQL 查询为一种类型的记录获取分页数据,该记录类型具有类型、图像等重复属性

下面的查询返回重复项,因此分页出错。

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-Syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX schema:<http://schema.org/>
SELECT distinct ?uri ?label ?r ?type ?image ?ownership ?rating ?comments ?allOwners
FROM <http://sample.net/>
WHERE  {
  ?r rdf:type <http://schema.org/Relation> . 
  ?r schema:property ?uri.
  ?r schema:owner ?owner .
  ?r schema:ownership ?ownership .
  ?uri rdfs:label ?label .
  ?uri rdf:type ?type . 
  ?uri schema:image ?image .
  OPTIONAL {?r schema:comments ?comments .}
  OPTIONAL {?r schema:rating ?rating .}
  filter (?owner =<http://sample.net/resource/37654824-334f-4e57-a40c-4078cac9c579>)
} limit 20 offset 0

样本数据

subject,predicate,object
Product-uri,type,Vehicle
Product-uri,Car
Product-uri,Toyota
Product-uri,image,Image-key1.png
Product-uri,Image-key2.png
Product-uri,Image-key3.png
Product-uri2,Vehicle
Product-uri2,Car
Product-uri2,Toyota
Product-uri2,Image-key21.png
Product-uri2,Image-key22.png
Product-uri2,Image-key23.png

如果我查询此数据以获取唯一产品列表(其中每个产品具有多种类型和图像),则总数将为 12 而不是 2。

解决方法

如评论中所述,当使用 ORDER BYLIMIT 单步执行大型解决方案集时,首先要在查询中包含 OFFSET。>

(在找到整个解决方案集之前无法应用ORDER BY,因此它可能出现减慢查询(正如评论中所述)。实际上,查询同时运行速度,但是当没有 ORDER BY 时,可能会在找到解决方案时返回,因此 某些 解决方案可能会很快返回,但完整的解决方案集将非常接近有或没有 ORDER BY 的同时。)

DISTINCT 适用于整个解决方案行 -- 因此,如果任何列发生变化,您将获得看起来重复的行。

您的问题没有说明您所看到的“重复项”。也许您可以添加一些示例结果和/或一些示例数据,以便我们更好地了解哪些地方没有按照您的意愿行事。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...