{{!
@param IRI contract
@param IRI graph
@param int limit
@param int offset (optional)
}}

{{> templates/prefixes.mustache}}

SELECT ?match (SUM(?contractCount) AS ?score) 
WHERE {
  {
    SELECT ?match (COUNT(?contract) AS ?contractCount)
    WHERE {
      {
        SELECT ?cpvObject
        WHERE {
          GRAPH <{{graph}}> {
            [] pc:lot <{{contract}}> ;
              pc:mainObject/skos:closeMatch ?cpvObject .
          }
        }
      }
      GRAPH <{{graph}}> {
        ?contract pc:mainObject/skos:closeMatch ?cpvObject ;
          pc:lot/pc:awardedTender/pc:bidder ?match . 
      }
    }
    GROUP BY ?match
  }
}
GROUP BY ?match
ORDER BY DESC(?score)
LIMIT {{limit}}
{{#offset}}
OFFSET {{offset}}
{{/offset}}

{{!
@param IRI contract
@param IRI graph
@param int limit
@param int offset (optional)
}}

{{> templates/prefixes.mustache}}

SELECT ?match (SUM(?contractCount) AS ?score) 
WHERE {
  {
    SELECT ?match (COUNT(?contract) AS ?contractCount)
    WHERE {
      {
        SELECT ?cpvObject
        WHERE {
          GRAPH <{{graph}}> {
            [] pc:lot <{{contract}}> ;
              pc:mainObject/skos:closeMatch ?cpvObject .
          }
        }
      }
      GRAPH <{{graph}}> {
        ?contract pc:mainObject/skos:closeMatch ?cpvObject ;
          pc:lot/pc:awardedTender/pc:bidder ?match . 
      }
    }
    GROUP BY ?match ?cpvObject
  }
}
GROUP BY ?match
ORDER BY DESC(?score)
LIMIT {{limit}}
{{#offset}}
OFFSET {{offset}}
{{/offset}}

Fix GROUP BY key to include ?cpvObject to boost contracts that match on more than 1 ?cpvObject
Fix GROUP BY key to include ?cpvObject to boost contracts that match on more than 1 ?cpvObject
