SPARQL distinct rowcount没有返回正确的数字

时间:2015-03-12 23:47:35

标签: sparql jena apache-jena

我有以下SPARQL查询,请注意选择谓词中的rowcount和查询末尾的group by子句。

我希望查询在每条记录中返回正确的行数,我注意到返回的行数不正确意味着如果有一条记录我得到1或有时2,如果有超过2记录,我仍然得到2,基本上它似乎返回随机值。

我知道因为我的查询有些问题,有人可以让我知道我可能做错了什么吗?我正在使用Apache Jena。

PREFIX  rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX  pf:   <http://jena.hpl.hp.com/ARQ/property#>
PREFIX  d:    <http://data-vocabulary.org/>
PREFIX  foaf: <http://xmlns.com/foaf/0.1/>
PREFIX  s:    <http://schema.org/>
PREFIX  xsd:  <http://www.w3.org/2001/XMLSchema#>
PREFIX  gr:   <http://purl.org/goodrelations/v1#>

SELECT DISTINCT  (count(*) AS ?rowCount) ?productName ?offerImage ?offerName ?productCategory ?salePrice ?suggestedRetailPrice ?productImage ?productThumbNail ?productUrl (GROUP_CONCAT(DISTINCT ?productdescription) AS ?productdescriptions) ?productBrand ?productId ?productSku ?productModel ?productMPN ?productManufacturer ?productGtin13 ?productGtin14 ?productGtin8 ?productAvailable ?productUnAvailable ?productUsedCondition ?productNewCondition ?productColor ?productAggregateRatingValue ?productReviewCount
WHERE
  {         

        ?p2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> d:Product .
        ?p2 <http://data-vocabulary.org/Product/offerDetails> ?schOffer .
        ?schOffer <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> d:Offer .
        ?schOffer <http://data-vocabulary.org/Offer/price> ?salePrice
        OPTIONAL
          { ?schOffer <http://data-vocabulary.org/Offer/name> ?offerName }
        OPTIONAL
          { ?p2 <http://data-vocabulary.org/Product/name> ?productName }
        OPTIONAL
          { ?schOffer <http://data-vocabulary.org/Offer/image> ?offerImage }
        OPTIONAL
          { ?p2 <http://data-vocabulary.org/Product/url> ?productUrl }
        OPTIONAL
          { ?p2 <http://data-vocabulary.org/Product/image> ?productImage }
        OPTIONAL
          { ?schOffer <http://data-vocabulary.org/Offer/category> ?productCategory }
        OPTIONAL
          { ?p2 <http://data-vocabulary.org/Product/description> ?productdescription }
        OPTIONAL
          { ?p2 <http://data-vocabulary.org/Product/brand> ?pBrandNode
            OPTIONAL
              { ?pBrandNode <http://data-vocabulary.org/Brand/name> ?brandNodeName }
            OPTIONAL
              { ?pBrandNode <http://data-vocabulary.org/Organization/name> ?brandNodeName }
            BIND(str(coalesce(?brandNodeName, ?pBrandNode)) AS ?productBrand)
          }
        OPTIONAL
          { ?p2 <http://data-vocabulary.org/Product/manufacturer> ?pManufacturerNode
            OPTIONAL
              { ?pManufacturerNode <http://data-vocabulary.org/manufacturer/name> ?manufacturerNodeName }
            BIND(str(coalesce(?manufacturerNodeName, ?pManufacturerNode)) AS ?productManufacturer)
          }
        OPTIONAL
          { ?p2 <http://schema.org/Product/aggregateRating> ?pAggregateRating
            OPTIONAL
              { ?pAggregateRating <http://schema.org/AggregateRating/ratingValue> ?productAggregateRatingValue }
            OPTIONAL
              { ?pAggregateRating <http://schema.org/AggregateRating/reviewCount> ?productReviewCount }
          }
        OPTIONAL
          { ?p2 <http://data-vocabulary.org/Product/productID> ?productId }
        OPTIONAL
          { ?p2 <http://data-vocabulary.org/Product/sku> ?productSku }
        OPTIONAL
          { ?p2 <http://data-vocabulary.org/Product/itemCondition> ?productNewCondition .
            ?productNewCondition <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> s:NewCondition
          }
        OPTIONAL
          { ?p2 <http://data-vocabulary.org/Product/itemCondition> ?productUsedCondition .
            ?productUsedCondition <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> s:UsedCondition
          }
        OPTIONAL
          { ?p2 <http://data-vocabulary.org/Product/gtin13> ?productGtin13 }
        OPTIONAL
          { ?p2 <http://data-vocabulary.org/Product/gtin14> ?productGtin14 }
        OPTIONAL
          { ?p2 <http://data-vocabulary.org/Product/gtin8> ?productGtin8 }
        OPTIONAL
          { ?schOffer <http://data-vocabulary.org/Offer/availability> ?productAvailable
            FILTER ( ?productAvailable = s:InStock )
          }
        OPTIONAL
          { ?schOffer <http://data-vocabulary.org/Offer/availability> ?productUnAvailable
            FILTER ( ?productUnAvailable = s:OutOfStock )
          }
        OPTIONAL
          { ?p2 <http://data-vocabulary.org/Product/model> ?productModel }
        OPTIONAL
          { ?p2 <http://data-vocabulary.org/Product/mpn> ?productMPN }
        OPTIONAL
          { ?p2 <http://data-vocabulary.org/Product/color> ?productColor }

  }
GROUP BY ?productName ?offerImage ?offerName ?productCategory ?salePrice ?suggestedRetailPrice ?productImage ?productThumbNail ?productUrl ?productBrand ?productId ?productSku ?productModel ?productMPN ?productManufacturer ?productGtin13 ?productGtin14 ?productGtin8 ?productAvailable ?productUnAvailable ?productUsedCondition ?productNewCondition ?productColor ?productAggregateRatingValue ?productReviewCount

1 个答案:

答案 0 :(得分:1)

这就是说你在一个组中有多个项目的模式。尝试在没有计数和分组的情况下运行,并查看获得2的结果。