【问题标题】:How to write paginated SPARQL query to fetch product list having multiple same nodes如何编写分页 SPARQL 查询以获取具有多个相同节点的产品列表
【发布时间】:2021-05-30 04:38:03
【问题描述】:

我想使用 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,type,Car
Product-uri,type,Toyota
Product-uri,image,Image-key1.png
Product-uri,image,Image-key2.png
Product-uri,image,Image-key3.png
Product-uri2,type,Vehicle
Product-uri2,type,Car
Product-uri2,type,Toyota
Product-uri2,image,Image-key21.png
Product-uri2,image,Image-key22.png
Product-uri2,image,Image-key23.png

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

【问题讨论】:

  • 重复是什么意思?在不同的页面上?如果是这样,这是因为您必须对整个结果进行排序,因为除了实现特定的东西之外没有隐式排序,但从来没有保证。长话短说,将ORDER BY 与一个或多个变量一起使用。
  • 在您询问或疑惑之前,排序会使整个查询变慢。

标签: sparql rdf virtuoso linked-data


【解决方案1】:

如 cmets 中所述,当使用 LIMITOFFSET 逐步执行大型解决方案集时,第一件重要的事情是在查询中包含 ORDER BY

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

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

您的问题并不清楚您所看到的“重复”。也许您可以添加一些示例结果和/或一些示例数据,这样我们就可以更好地了解哪些方面没有按照您的意愿去做。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 2023-02-01
    • 1970-01-01
    • 2016-12-12
    • 2018-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多