axiothOntology Hub

了解什么是本体 / 第三部分:查询 · 第 12 课,共 24 课

暂无所选语言的译文,当前显示English内容。

SPARQL, one step further: optional parts, filters and unions

Pull out exactly the rows you want with OPTIONAL, FILTER, UNION and ordering, and see the trap in asking for what is missing.

请先阅读: 6. Asking questions, and building on others

Part 1 asked questions with triple-shaped patterns only. Real data has gaps, and real questions have conditions. The data in this lesson has three pets, and one of them, ex:kong, has no name label (rdfs:label). That single gap changes the result all the way through.

PREFIX ex: <https://example.org/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?pet ?name ?age WHERE {
  ?pet ex:age ?age .
  ?pet rdfs:label ?name .
}

There are three pets and two rows. ex:kong has an age and no label, so it fails the second line and drops out entirely.

OPTIONAL for what may be absent

Every line in a pattern means "this must be there". If one line fails to match, that thing drops out of the result entirely. A line inside OPTIONAL { } is filled in when present and left blank when not. If you have used SQL, it plays the role of a LEFT JOIN.

  OPTIONAL { ?pet rdfs:label ?name . }

Now there are three rows. In the row for ex:kong the ?name cell is empty. The age is still required, so anything without one stays out.

FILTER narrows by value

A pattern matches the shape of the graph. FILTER compares the values found that way and keeps only the rows where the condition is true.

Written asWhat stays
FILTER (?age >= 3)Rows where the age is 3 or more
FILTER (LANG(?name) = "en")Rows where the name is in English
FILTER (CONTAINS(STR(?name), "Na"))Rows where the name contains "Na"
FILTER (?a != ?b)Rows where the two variables are different things
FILTER (BOUND(?name))Rows where ?name has a value
  FILTER (?age >= 3)

Of the three rows the patterns found, the two where the condition is true remain. FILTER finds no new rows. It only removes rows already found.

UNION for either one

Patterns written side by side mean "and". For "or", join two brace groups with UNION. Rows found by either side end up in one result.

SELECT ?pet WHERE {
  { ?pet a ex:Cat . }
  UNION
  { ex:minji ex:keeps ?pet . }

ex:nabi appears twice. It is a cat and it is kept by Minji, so each group found it once. SPARQL does not remove duplicates by itself. Write SELECT DISTINCT to get it once.

Asking for what is missing

To find pets with no label, use FILTER NOT EXISTS { }. Only rows for which the shape inside the braces cannot be found in the graph remain.

  ?pet ex:age ?age .
  FILTER NOT EXISTS { ?pet rdfs:label ?name . }

Only ex:kong, the one dropped in the first step, remains. It is the row for which the shape in the braces cannot be found in this graph.

Read this result with care. The answer is not "Kong has no name". It is "this graph does not state a name for Kong". An ontology follows the open-world assumption, but SPARQL answers from the graph it is given and nothing else. Merge in data from elsewhere and the answer may change.

Order and count

A graph has no order, so the rows of a result have none either. Add ORDER BY when order matters and LIMIT to take only the first few rows. When the same row comes out several times, SELECT DISTINCT keeps one.

SELECT ?pet ?age WHERE {
ORDER BY DESC(?age)
LIMIT 2

ORDER BY and LIMIT go outside the braces, because ordering and cutting happen after all rows are found. Without DESC the smallest value comes first.

常见误解

A condition on a value from an OPTIONAL gives the same result wherever it is written.
With FILTER (LANG(?name) = "en") outside the OPTIONAL, the row for ex:kong, whose name is blank, has nothing to compare and is dropped too. To keep the blanks, put the condition inside the braces of the OPTIONAL.
What NOT EXISTS finds really does not exist.
It means the queried graph does not state it. Concluding that something is absent needs a separate guarantee that the data is complete.
Results come out in the order the file was written.
Without ORDER BY the order is up to the store, and the same query may return rows differently each time.

A pattern is "a shape that must be there", OPTIONAL is "a shape to fill in if there", FILTER is "a condition on values", and NOT EXISTS is "a shape this graph does not contain".