Search Multiple Queries from List in SPARQL

2019-05-31 21:02发布

问题:

I want to find sales data for a set of 300 addresses using the UK Land Registry database. The database permits SPARQL queries, however I am completely new to SPARQL, and do not know how to make multiple queries at once (e.g. search for 300 addresses in one SPARQL query).

This is an example land registry query for a single address.

So I have two questions:

1) How can I search for multiple addresses in one query?

2) Is there a way to connect a database list of addresses to automate the query?

Help and direction will be hugely appreciated.

回答1:

In the example query, the first five triple patterns in the WHERE clause ties the result to a specific result. Delete these or comment out with a # and you will get a list of all addresses known to the chosen SPARQL endpoint:

prefix ...

SELECT  ?item ?ppd_propertyAddress ?ppd_hasTransaction ?ppd_pricePaid ? ppd_transactionCategory ?ppd_transactionDate ?ppd_transactionId ?ppd_estateType    ?ppd_newBuild ?ppd_propertyAddressCounty ?ppd_propertyAddressDistrict ?ppd_propertyAddressLocality ?ppd_propertyAddressPaon ?ppd_propertyAddressPostcode ?ppd_propertyAddressSaon ?ppd_propertyAddressStreet ?ppd_propertyAddressTown ?ppd_propertyType ?ppd_recordStatus
WHERE
{ #?ppd_propertyAddress text:query _:b0 .
  #_:b0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "paon: ( 12 )  AND street: ( PATTINSON AND DRIVE )" .
  #_:b0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> _:b1 .
  #_:b1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> 3000000 .
  #_:b1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> .
  ?item ppd:propertyAddress ?ppd_propertyAddress .
  ?item ppd:hasTransaction ?ppd_hasTransaction .
  ?item ppd:pricePaid ?ppd_pricePaid .
  ?item ppd:transactionCategory ?ppd_transactionCategory .
  ?item ppd:transactionDate ?ppd_transactionDate .
  ?item ppd:transactionId ?ppd_transactionId
  OPTIONAL
  { ?item ppd:estateType ?ppd_estateType }
  OPTIONAL
  { ?item ppd:newBuild ?ppd_newBuild }
  OPTIONAL
  { ?ppd_propertyAddress lrcommon:county ?ppd_propertyAddressCounty }
  OPTIONAL
  { ?ppd_propertyAddress lrcommon:district ?ppd_propertyAddressDistrict }
  OPTIONAL
  { ?ppd_propertyAddress lrcommon:locality ?ppd_propertyAddressLocality }
  OPTIONAL
  { ?ppd_propertyAddress lrcommon:paon ?ppd_propertyAddressPaon }
  OPTIONAL
  { ?ppd_propertyAddress lrcommon:postcode ?ppd_propertyAddressPostcode }
  OPTIONAL
  { ?ppd_propertyAddress lrcommon:saon ?ppd_propertyAddressSaon }
  OPTIONAL
  { ?ppd_propertyAddress lrcommon:street ?ppd_propertyAddressStreet }
  OPTIONAL
  { ?ppd_propertyAddress lrcommon:town ?ppd_propertyAddressTown }
  OPTIONAL
  { ?item ppd:propertyType ?ppd_propertyType }
  OPTIONAL
  { ?item ppd:recordStatus ?ppd_recordStatus }
}
LIMIT   100

The second question is unclear and perhaps resolved by the above? I.e. you don't need to submit multiple queries.

If you want to query for a specific list of addresses, then you could use a SPARQL VALUES expression, see VALUES: Providing inline data. A starting point may be the following (by inspection only - you'd have to check this against the data):

SELECT *
WHERE {
   VALUES ?addr {"address1" "paon: ( 12 )  AND street: ( PATTINSON AND DRIVE )" ...}
   ?ppd_propertyAddress text:query _:b0 .
   _:b0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> ?addr .
   ?item ppd:propertyAddress ?ppd_propertyAddress .

   ...
}

Note that VALUES binds ?addr to each string in the list between the braces in the VALUES clause. ?addr is then used in the triple pattern in place of the address in the original query.