FILTER

Created on Feb. 23, 2013, 10:49 a.m. by Hevok & updated by Hevok on May 2, 2013, 5:18 p.m.

An RDF Graph is traversed with the help of the SPARQL Query Language which forms Graph Patterns that will be matched by the RDF Graph.

In order to put some conditions on Graph Patterns one has to explicitly formulate Triples that express what can be matched and selected from the Graph.

In addition one can specify Constraints on results that one want to have selected. The Constraints can be given with the help of the FILTER keyword.

For example, given a SPARQL knowledge base with three Prefixes, dc, default and ns (namespace). dc stands for Dublin Core which is for identifying and characterizing bibliography. There are two books with title and prize.

If one wants to select from this database only books that costs less than a certain value one uses a FILTER expression. In the Query there one gives also the dc and ns namespaces, as well as selects the Title and the price of the books which is selected from the namespace that points to the book knowledge base. In the Graph Pattern one has the Variable x to select the book, but one selects only the books that have the property price lower than a certain value. For this books that fulfill the FILTER conditions one wants to select the titles.

  • The keyword FILTER specifies Constraints for the results
  • FILTER expressions contain Operators and Functions
  • FILTER can NOT assign/create new Values

A book Knowledge Base:

    # Default Graph (stored at http://example.org/books)
    @prefix dc: <http://purl.org/dc/elements/1.1/>] .
    @prefix : <http://example.org/book/>] .
    @prefix ns: <http://example.org/ns#>] .

    :book1 dc:title "SPARQL Tutorial" .
    :book2 ns:price 42.
    :book2 dc:title "The Semantic Web" .
    :book2 ns:price 23 .

A Query in SPARQL with the use of FILTER:

    PREFIX dc: <http://purl.org/dc/elements/1.1/>]
    PREFIX ns: <http://example.org/ns#>]
    SELECT ?title ?price
    FROM <http://example.org/book>]
    WHERE {
         ?x ns:price ?price .
             FILTER (?price < 30.5)
         ?x dc:title ?title .
    }
Filter-Active.jpg

Tags: query, statement, coding, programming, database
Categories: Tutorial
Parent: SPARQL

Update entry (Admin) | See changes

Comment on This Data Unit