Skip to content

SPARQL query examples

Jindřich Mynarz edited this page Mar 21, 2015 · 8 revisions

This pages contains illustrative examples of the SPARQL queries used in matchmaking.

Exact CPV

The following query demonstrates the matchmaker that employs exact matching over CPV concepts using no inhibition for pc:additionalObject (1 as a weight).

PREFIX gr:      <http://purl.org/goodrelations/v1#>
PREFIX pc:      <http://purl.org/procurement/public-contracts#>

SELECT ?match (SAMPLE(?_label) AS ?label) ?score
WHERE {
  {
    SELECT ?match (SUM(?qWeight * ?mWeight * ?contractCount) AS ?score) 
    WHERE {
      {
        SELECT ?match ?qWeight ?mWeight (COUNT(?contract) AS ?contractCount)
        WHERE {
          {
            SELECT ?cpv ?qWeight 
            WHERE {
              GRAPH <http://linked.opendata.cz/resource/dataset/vestnikverejnychzakazek.cz/2014-08-25> {
                VALUES (?objectProperty     ?qWeight) {
                       (pc:mainObject       1)
                       (pc:additionalObject 1) 
                }
                <http://linked.opendata.cz/resource/vestnikverejnychzakazek.cz/public-contract/479352-7403012079352> ?objectProperty ?cpv .
              }
            }
          }
          GRAPH <http://linked.opendata.cz/resource/dataset/vestnikverejnychzakazek.cz/2014-08-25> {
            VALUES (?objectProperty     ?mWeight) {
                   (pc:mainObject       1)
                   (pc:additionalObject 1) 
            }
            ?contract pc:awardedTender/pc:bidder ?match ;
              ?objectProperty ?cpv .
          }
        }
        GROUP BY ?match ?qWeight ?mWeight
      }
    }
    GROUP BY ?match
    ORDER BY DESC(?score)
    LIMIT 100
    OFFSET 0
  }
  OPTIONAL {
    GRAPH <http://linked.opendata.cz/resource/dataset/vestnikverejnychzakazek.cz/2014-08-25> {
      ?match gr:legalName ?_label .
    }
  }
}
GROUP BY ?match ?score

Exact CPV with 0.1 additional object inhibition

The following is an example query for the matchmaker that employs exact matching over CPV concepts using 0.1 as a weight (inhibition) for pc:additionalObject.

PREFIX gr:      <http://purl.org/goodrelations/v1#>
PREFIX pc:      <http://purl.org/procurement/public-contracts#>

SELECT ?match (SAMPLE(?_label) AS ?label) ?score
WHERE {
  {
    SELECT ?match (SUM(?qWeight * ?mWeight * ?contractCount) AS ?score) 
    WHERE {
      {
        SELECT ?match ?qWeight ?mWeight (COUNT(?contract) AS ?contractCount)
        WHERE {
          {
            SELECT ?cpv ?qWeight 
            WHERE {
              GRAPH <http://linked.opendata.cz/resource/dataset/vestnikverejnychzakazek.cz/2014-08-25> {
                VALUES (?objectProperty     ?qWeight) {
                       (pc:mainObject       1)
                       (pc:additionalObject 0.1) 
                }
                <http://linked.opendata.cz/resource/vestnikverejnychzakazek.cz/public-contract/479352-7403012079352> ?objectProperty ?cpv .
              }
            }
          }
          GRAPH <http://linked.opendata.cz/resource/dataset/vestnikverejnychzakazek.cz/2014-08-25> {
            VALUES (?objectProperty     ?mWeight) {
                   (pc:mainObject       1)
                   (pc:additionalObject 0.1) 
            }
            ?contract pc:awardedTender/pc:bidder ?match ;
              ?objectProperty ?cpv .
          }
        }
        GROUP BY ?match ?qWeight ?mWeight
      }
    }
    GROUP BY ?match
    ORDER BY DESC(?score)
    LIMIT 100
    OFFSET 0
  }
  OPTIONAL {
    GRAPH <http://linked.opendata.cz/resource/dataset/vestnikverejnychzakazek.cz/2014-08-25> {
      ?match gr:legalName ?_label .
    }
  }
}
GROUP BY ?match ?score

Exact CPV aggregated to maximum

The following query demonstrates the matchmaker that employs exact matching over CPV concepts using no inhibition for pc:additionalObject and aggregation of similarity scores to maximum.

PREFIX gr:      <http://purl.org/goodrelations/v1#>
PREFIX pc:      <http://purl.org/procurement/public-contracts#>

SELECT ?match (SAMPLE(?_label) AS ?label) ?score
WHERE {
  {
    SELECT ?match (MAX(?contractScore) AS ?score) 
    WHERE {
      {
        SELECT ?match (SUM(?qWeight * ?mWeight) AS ?contractScore)
        WHERE {
          {
            SELECT ?cpv ?qWeight 
            WHERE {
              GRAPH <http://linked.opendata.cz/resource/dataset/vestnikverejnychzakazek.cz/2014-08-25> {
                VALUES (?objectProperty     ?qWeight) {
                       (pc:mainObject       1)
                       (pc:additionalObject 1) 
                }
                <http://linked.opendata.cz/resource/vestnikverejnychzakazek.cz/public-contract/479352-7403012079352> ?objectProperty ?cpv .
              }
            }
          }
          GRAPH <http://linked.opendata.cz/resource/dataset/vestnikverejnychzakazek.cz/2014-08-25> {
            VALUES (?objectProperty     ?mWeight) {
                   (pc:mainObject       1)
                   (pc:additionalObject 1) 
            }
            ?contract pc:awardedTender/pc:bidder ?match ;
              ?objectProperty ?cpv .
          }
        }
        GROUP BY ?match ?qWeight ?mWeight
      }
    }
    GROUP BY ?match
    ORDER BY DESC(?score)
    LIMIT 100
    OFFSET 0
  }
  OPTIONAL {
    GRAPH <http://linked.opendata.cz/resource/dataset/vestnikverejnychzakazek.cz/2014-08-25> {
      ?match gr:legalName ?_label .
    }
  }
}
GROUP BY ?match ?score

Exact CPV with zIndex

In the following query, we extend the exact matching over CPV concepts using no inhibition for pc:additionalObject with weighing contracting authority fairness score (zIndex).

PREFIX adms:    <http://www.w3.org/ns/adms#>
PREFIX gr:      <http://purl.org/goodrelations/v1#>
PREFIX pc:      <http://purl.org/procurement/public-contracts#>
PREFIX pccz:    <http://purl.org/procurement/public-contracts-czech#>
PREFIX rov:     <http://www.w3.org/ns/regorg#>
PREFIX skos:    <http://www.w3.org/2004/02/skos/core#>

SELECT ?match (SAMPLE(?_label) AS ?label) ?score
WHERE {
  {
    SELECT ?match (SUM(?qWeight * ?mWeight * ?zIndex * ?contractCount) AS ?score) 
    WHERE {
      {
        SELECT ?match
               ?qWeight
               ?mWeight
               (SAMPLE(?__zIndex) AS ?zIndex)
               (COUNT(?contract) AS ?contractCount)
        WHERE {
          {
            SELECT ?cpv ?qWeight 
            WHERE {
              GRAPH <http://linked.opendata.cz/resource/dataset/vestnikverejnychzakazek.cz/2014-08-25> {
                VALUES (?objectProperty     ?qWeight) {
                       (pc:mainObject       1)
                       (pc:additionalObject 1) 
                }
                <http://linked.opendata.cz/resource/vestnikverejnychzakazek.cz/public-contract/479352-7403012079352> ?objectProperty ?cpv .
              }
            }
          }
          GRAPH <http://linked.opendata.cz/resource/dataset/vestnikverejnychzakazek.cz/2014-08-25> {
            VALUES (?objectProperty     ?mWeight) {
                   (pc:mainObject       1)
                   (pc:additionalObject 1) 
            }
            ?contract pc:awardedTender/pc:bidder ?match ;
              pc:contractingAuthority ?contractingAuthority ;
              ?objectProperty ?cpv .
          }
          OPTIONAL {
            GRAPH <http://linked.opendata.cz/resource/dataset/vestnikverejnychzakazek.cz/2014-08-25> {
              ?contractingAuthority adms:identifier/skos:notation ?identifier .
            }
            GRAPH <http://linked.opendata.cz/resource/zindex/2014-10-16> {
              [] pccz:zindex ?_zIndex ;
                rov:registration/skos:notation ?identifier .
            }
          }
          BIND (COALESCE(?_zindex, 0.5) AS ?__zindex)
        }
        GROUP BY ?match ?qWeight ?mWeight
      }
    }
    GROUP BY ?match
    ORDER BY DESC(?score)
    LIMIT 100
    OFFSET 0
  }
  OPTIONAL {
    GRAPH <http://linked.opendata.cz/resource/dataset/vestnikverejnychzakazek.cz/2014-08-25> {
      ?match gr:legalName ?_label .
    }
  }
}
GROUP BY ?match ?score

Exact CPV with better deduplication

The following query demonstrates the matchmaker that employs exact matching over CPV concepts using no inhibition for pc:additionalObject (1 as a weight). The only difference from the first query is that we use a better deduplicated dataset in this one.

PREFIX gr:      <http://purl.org/goodrelations/v1#>
PREFIX pc:      <http://purl.org/procurement/public-contracts#>

SELECT ?match (SAMPLE(?_label) AS ?label) ?score
WHERE {
  {
    SELECT ?match (SUM(?qWeight * ?mWeight * ?contractCount) AS ?score) 
    WHERE {
      {
        SELECT ?match ?qWeight ?mWeight (COUNT(?contract) AS ?contractCount)
        WHERE {
          {
            SELECT ?cpv ?qWeight 
            WHERE {
              GRAPH <http://linked.opendata.cz/resource/dataset/vestnikverejnychzakazek.cz/2014-08-25/supplier-deduplication> {
                VALUES (?objectProperty     ?qWeight) {
                       (pc:mainObject       1)
                       (pc:additionalObject 1) 
                }
                <http://linked.opendata.cz/resource/vestnikverejnychzakazek.cz/public-contract/479352-7403012079352> ?objectProperty ?cpv .
              }
            }
          }
          GRAPH <http://linked.opendata.cz/resource/dataset/vestnikverejnychzakazek.cz/2014-08-25/supplier-deduplication> {
            VALUES (?objectProperty     ?mWeight) {
                   (pc:mainObject       1)
                   (pc:additionalObject 1) 
            }
            ?contract pc:awardedTender/pc:bidder ?match ;
              ?objectProperty ?cpv .
          }
        }
        GROUP BY ?match ?qWeight ?mWeight
      }
    }
    GROUP BY ?match
    ORDER BY DESC(?score)
    LIMIT 100
    OFFSET 0
  }
  OPTIONAL {
    GRAPH <http://linked.opendata.cz/resource/dataset/vestnikverejnychzakazek.cz/2014-08-25/supplier-deduplication> {
      ?match gr:legalName ?_label .
    }
  }
}
GROUP BY ?match ?score

Exact CPV with better deduplication and 0.1 additional object inhibition

The following query shows exact matching over CPV concepts using 0.1 as the inhibition for pc:additionalObject. The query is executed on a better deduplicated dataset.

PREFIX gr:      <http://purl.org/goodrelations/v1#>
PREFIX pc:      <http://purl.org/procurement/public-contracts#>

SELECT ?match (SAMPLE(?_label) AS ?label) ?score
WHERE {
  {
    SELECT ?match (SUM(?qWeight * ?mWeight * ?contractCount) AS ?score) 
    WHERE {
      {
        SELECT ?match ?qWeight ?mWeight (COUNT(?contract) AS ?contractCount)
        WHERE {
          {
            SELECT ?cpv ?qWeight 
            WHERE {
              GRAPH <http://linked.opendata.cz/resource/dataset/vestnikverejnychzakazek.cz/2014-08-25/supplier-deduplication> {
                VALUES (?objectProperty     ?qWeight) {
                       (pc:mainObject       1)
                       (pc:additionalObject 0.1) 
                }
                <http://linked.opendata.cz/resource/vestnikverejnychzakazek.cz/public-contract/479352-7403012079352> ?objectProperty ?cpv .
              }
            }
          }
          GRAPH <http://linked.opendata.cz/resource/dataset/vestnikverejnychzakazek.cz/2014-08-25/supplier-deduplication> {
            VALUES (?objectProperty     ?mWeight) {
                   (pc:mainObject       1)
                   (pc:additionalObject 0.1) 
            }
            ?contract pc:awardedTender/pc:bidder ?match ;
              ?objectProperty ?cpv .
          }
        }
        GROUP BY ?match ?qWeight ?mWeight
      }
    }
    GROUP BY ?match
    ORDER BY DESC(?score)
    LIMIT 100
    OFFSET 0
  }
  OPTIONAL {
    GRAPH <http://linked.opendata.cz/resource/dataset/vestnikverejnychzakazek.cz/2014-08-25/supplier-deduplication> {
      ?match gr:legalName ?_label .
    }
  }
}
GROUP BY ?match ?score

Expand 1 hop to broader CPV

This query uses 1-hop expansion to broader CPV concepts. The concepts inferred in this way are weighted by their pre-computed concept specificity.

PREFIX ex:      <http://example.com/>
PREFIX gr:      <http://purl.org/goodrelations/v1#>
PREFIX pc:      <http://purl.org/procurement/public-contracts#>
PREFIX skos:    <http://www.w3.org/2004/02/skos/core#>

SELECT ?match (SAMPLE(?_label) AS ?label) ?score
WHERE {
  {
    SELECT ?match (SUM(?qWeight * ?mWeight * ?contractCount) AS ?score)
    WHERE {
      {
        SELECT ?match ?qWeight ?mWeight (COUNT(?contract) AS ?contractCount)
        WHERE {
          {
            SELECT ?cpv ?qWeight
            WHERE {
              {
                SELECT ?cpv (1 AS ?qWeight)
                WHERE {
                  GRAPH <http://linked.opendata.cz/resource/dataset/vestnikverejnychzakazek.cz/2014-08-25> {
                    <http://linked.opendata.cz/resource/vestnikverejnychzakazek.cz/public-contract/50017676-5001767603001> pc:mainObject ?cpv .
                  }
                }
              } UNION {
                SELECT ?cpv (1 * ?cs AS ?qWeight)
                WHERE {
                  GRAPH <http://linked.opendata.cz/resource/dataset/vestnikverejnychzakazek.cz/2014-08-25> {
                    <http://linked.opendata.cz/resource/vestnikverejnychzakazek.cz/public-contract/50017676-5001767603001> pc:additionalObject ?cpv .
                  }
                  GRAPH <http://linked.opendata.cz/resource/dataset/vestnikverejnychzakazek.cz/2014-08-25/asserted-cpv-specificities> {
                    ?cpv ex:conceptSpecificity ?cs .
                  }
                }
              } UNION {
                SELECT ?cpv (1 * ?cs AS ?qWeight)
                WHERE {
                  GRAPH <http://linked.opendata.cz/resource/dataset/vestnikverejnychzakazek.cz/2014-08-25> {
                    <http://linked.opendata.cz/resource/vestnikverejnychzakazek.cz/public-contract/50017676-5001767603001> pc:mainObject ?_cpv .
                  }
                  GRAPH <http://linked.opendata.cz/resource/dataset/cpv-2008> {
                    ?_cpv skos:broaderTransitive{1,1} ?cpv .
                  }
                  GRAPH <http://linked.opendata.cz/resource/dataset/vestnikverejnychzakazek.cz/2014-08-25/inferred-cpv-specificities> {
                    ?cpv ex:conceptSpecificity ?cs .
                  }
                }
              }
            }
          }
          GRAPH <http://linked.opendata.cz/resource/dataset/vestnikverejnychzakazek.cz/2014-08-25> {
            VALUES (?objectProperty     ?mWeight) {
                   (pc:mainObject       1)
                   (pc:additionalObject 1)
            }
            ?contract pc:awardedTender/pc:bidder ?match ;
              ?objectProperty ?cpv .
          }
        }
        GROUP BY ?match ?qWeight ?mWeight
      }
    }
    GROUP BY ?match
    ORDER BY DESC(?score)
    LIMIT 100
    OFFSET 0
  }
  GRAPH <http://linked.opendata.cz/resource/dataset/vestnikverejnychzakazek.cz/2014-08-25> {
    OPTIONAL {
      ?match gr:legalName ?_label .
    }
  }
}
GROUP BY ?match ?score

Expand 1 hop to broader CPV with better deduplication and 0.1 inhibition for additional objects

In this query we show 1-hop expansion to broader CPV concepts, which are weighted by their pre-computed concept specificity. Concepts assigned to public contracts via pc:additionalObject property are weighted by 0.1. The query is executed on a better deduplicated dataset.

PREFIX ex:      <http://example.com/>
PREFIX gr:      <http://purl.org/goodrelations/v1#>
PREFIX pc:      <http://purl.org/procurement/public-contracts#>
PREFIX skos:    <http://www.w3.org/2004/02/skos/core#>

SELECT ?match (SAMPLE(?_label) AS ?label) ?score
WHERE {
  {
    SELECT ?match (SUM(?qWeight * ?mWeight * ?contractCount) AS ?score)
    WHERE {
      {
        SELECT ?match ?qWeight ?mWeight (COUNT(?contract) AS ?contractCount)
        WHERE {
          {
            SELECT ?cpv ?qWeight
            WHERE {
              {
                SELECT ?cpv (1 AS ?qWeight)
                WHERE {
                  GRAPH <http://linked.opendata.cz/resource/dataset/vestnikverejnychzakazek.cz/2014-08-25/supplier-deduplication> {
                    <http://linked.opendata.cz/resource/vestnikverejnychzakazek.cz/public-contract/50017676-5001767603001> pc:mainObject ?cpv .
                  }
                }
              } UNION {
                SELECT ?cpv (0.1 * ?cs AS ?qWeight)
                WHERE {
                  GRAPH <http://linked.opendata.cz/resource/dataset/vestnikverejnychzakazek.cz/2014-08-25/supplier-deduplication> {
                    <http://linked.opendata.cz/resource/vestnikverejnychzakazek.cz/public-contract/50017676-5001767603001> pc:additionalObject ?cpv .
                  }
                  GRAPH <http://linked.opendata.cz/resource/dataset/vestnikverejnychzakazek.cz/2014-08-25/asserted-cpv-specificities> {
                    ?cpv ex:conceptSpecificity ?cs .
                  }
                }
              } UNION {
                SELECT ?cpv (1 * ?cs AS ?qWeight)
                WHERE {
                  GRAPH <http://linked.opendata.cz/resource/dataset/vestnikverejnychzakazek.cz/2014-08-25/supplier-deduplication> {
                    <http://linked.opendata.cz/resource/vestnikverejnychzakazek.cz/public-contract/50017676-5001767603001> pc:mainObject ?_cpv .
                  }
                  GRAPH <http://linked.opendata.cz/resource/dataset/cpv-2008> {
                    ?_cpv skos:broaderTransitive{1,1} ?cpv .
                  }
                  GRAPH <http://linked.opendata.cz/resource/dataset/vestnikverejnychzakazek.cz/2014-08-25/inferred-cpv-specificities> {
                    ?cpv ex:conceptSpecificity ?cs .
                  }
                }
              }
            }
          }
          GRAPH <http://linked.opendata.cz/resource/dataset/vestnikverejnychzakazek.cz/2014-08-25/supplier-deduplication> {
            VALUES (?objectProperty     ?mWeight) {
                   (pc:mainObject       1)
                   (pc:additionalObject 0.1)
            }
            ?contract pc:awardedTender/pc:bidder ?match ;
              ?objectProperty ?cpv .
          }
        }
        GROUP BY ?match ?qWeight ?mWeight
      }
    }
    GROUP BY ?match
    ORDER BY DESC(?score)
    LIMIT 100
    OFFSET 0
  }
  GRAPH <http://linked.opendata.cz/resource/dataset/vestnikverejnychzakazek.cz/2014-08-25/supplier-deduplication> {
    OPTIONAL {
      ?match gr:legalName ?_label .
    }
  }
}
GROUP BY ?match ?score

Expand 1 hop to narrower CPV

This query uses 1-hop expansion to narrower CPV concepts. The concepts inferred in this way are weighted by their pre-computed concept specificity.

PREFIX ex:      <http://example.com/>
PREFIX gr:      <http://purl.org/goodrelations/v1#>
PREFIX pc:      <http://purl.org/procurement/public-contracts#>
PREFIX skos:    <http://www.w3.org/2004/02/skos/core#>

SELECT ?match (SAMPLE(?_label) AS ?label) ?score
WHERE {
  {
    SELECT ?match (SUM(?qWeight * ?mWeight * ?contractCount) AS ?score)
    WHERE {
      {
        SELECT ?match ?qWeight ?mWeight (COUNT(?contract) AS ?contractCount)
        WHERE {
          {
            SELECT ?cpv ?qWeight
            WHERE {
              {
                SELECT ?cpv (1 AS ?qWeight)
                WHERE {
                  GRAPH <http://linked.opendata.cz/resource/dataset/vestnikverejnychzakazek.cz/2014-08-25> {
                    <http://linked.opendata.cz/resource/vestnikverejnychzakazek.cz/public-contract/50017676-5001767603001> pc:mainObject ?cpv .
                  }
                }
              } UNION {
                SELECT ?cpv (1 * ?cs AS ?qWeight)
                WHERE {
                  GRAPH <http://linked.opendata.cz/resource/dataset/vestnikverejnychzakazek.cz/2014-08-25> {
                    <http://linked.opendata.cz/resource/vestnikverejnychzakazek.cz/public-contract/50017676-5001767603001> pc:additionalObject ?cpv .
                  }
                  GRAPH <http://linked.opendata.cz/resource/dataset/vestnikverejnychzakazek.cz/2014-08-25/asserted-cpv-specificities> {
                    ?cpv ex:conceptSpecificity ?cs .
                  }
                }
              } UNION {
                SELECT ?cpv (1 * ?cs AS ?qWeight)
                WHERE {
                  GRAPH <http://linked.opendata.cz/resource/dataset/vestnikverejnychzakazek.cz/2014-08-25> {
                    <http://linked.opendata.cz/resource/vestnikverejnychzakazek.cz/public-contract/50017676-5001767603001> pc:mainObject ?_cpv .
                  }
                  GRAPH <http://linked.opendata.cz/resource/dataset/cpv-2008> {
                    ?_cpv skos:narrowerTransitive{1,1} ?cpv .
                  }
                  GRAPH <http://linked.opendata.cz/resource/dataset/vestnikverejnychzakazek.cz/2014-08-25/inferred-cpv-specificities> {
                    ?cpv ex:conceptSpecificity ?cs .
                  }
                }
              }
            }
          }
          GRAPH <http://linked.opendata.cz/resource/dataset/vestnikverejnychzakazek.cz/2014-08-25> {
            VALUES (?objectProperty     ?mWeight) {
                   (pc:mainObject       1)
                   (pc:additionalObject 1)
            }
            ?contract pc:awardedTender/pc:bidder ?match ;
              ?objectProperty ?cpv .
          }
        }
        GROUP BY ?match ?qWeight ?mWeight
      }
    }
    GROUP BY ?match
    ORDER BY DESC(?score)
    LIMIT 100
    OFFSET 0
  }
  GRAPH <http://linked.opendata.cz/resource/dataset/vestnikverejnychzakazek.cz/2014-08-25> {
    OPTIONAL {
      ?match gr:legalName ?_label .
    }
  }
}
GROUP BY ?match ?score