Selecting pages


The most important part of the Semantic search features in Semantic MediaWiki is a simple format for describing which pages should be displayed as the search result. Queries select wiki pages based on the information that has been specified for them using Categories, Properties, and maybe some other MediaWiki features such as a page's namespace. The following paragraphs introduce the main query features in SMW.

Simplified example to explain the difference between condition and printout result


In the introductory example, we gave the single condition [[Located in::Germany]] to describe which pages we were interested in. The markup text is exactly what you would otherwise write to assert that some page has this property and value. Putting it in a semantic query makes SMW return all such pages. This is a general scheme: The syntax for asking for pages that satisfy some condition is exactly the syntax for explicitly asserting that this condition holds.

The following queries show what this means:

  1. [[Category:Actor]] gives all pages directly or indirectly (through a sub-, subsub-, etc. category) in the category.
  2. [[Born in::Boston]] gives all pages annotated as being about someone born in Boston.
  3. [[Height::180cm]] gives all pages annotated as being about someone having a height of 180cm.

By using other categories or properties than above, we can already ask for pages which have certain annotations. Next let us combine those requirements:

[[Category:Actor]] [[Born in::Boston]] [[Height::180cm]]

asks for everybody who is an actor and was born in Boston and is 180cm tall. In other words: when many conditions are written into one query, the result is narrowed down to those pages that meet all the requirements. Thus we have a logical AND. By the way: queries can also include line breaks in order to make them more readable. So we could as well write:

[[Born in::Boston]] 

to get the same result as above. Note that queries only return the articles that are positively known to satisfy the required properties: if there is no property for the height of some actor, that actor will not be selected.

When specifying property values, SMW will usually ignore any initial and trailing whitespace, so the two conditions [[Height::180cm]] and [[Height:: 180cm ]] have the same meaning. Datatypes such as datatype "Number"Holds integer and decimal numbers, with an optional exponent may have additional features such as ignoring commas that might be used to separate the thousands. SMW will also treat synonymous page names the same, just like MediaWiki would usually consider "Semantic wiki", "Semantic_wiki" and "semantic wiki" to refer to the same page.

If you are using certain condition patterns frequently, you might create a Concept as a shorthand. Concepts form some kind of virtual category and can thus be used similar to category conditions.

Search features[edit]

Sorting results[edit]

It is often helpful to present query results in a suitable order, for example to present a list of cities ordered by population.

 |?Located in=Country
 |?Average rainy days
 |sort=Located in,Average rainy days

Special page "Ask"Provides an interface that assists users with creating and executing semantic queries has a simple interface to add one or more sorting conditions to a query ... read more.

Linking to results[edit]

Links to semantic query results on Special:Ask can be created by means of the inline query feature in SMW as explained in its documentation. It is not recommended to create links directly, since they are very lengthy and use a specific encoding. Developers who create extensions that link to Special:Ask should also use SMW's internal functions for building links. Understanding the details of SMW's encoding of queries in links is therefore not required for using SMW.

Limitations and caveats[edit]

Subqueries for properties[edit]

It is not possible to use a subquery to obtain a list of properties that is then used in a query. One can, however, use a query that returns a list of properties, and copy and paste the result into another query. Alternatively, one can use the template results format to pass properties directly to another query.

Querying for the absence of a property[edit]

It is not possible to query for the absence of a property. (talk). Depending of the structure of the wiki, a workaround may be possible.

Filtering Categories and Concepts[edit]

It is not possible to ask for all pages of a query that are not in a specific category or concept. For example, the query {{#ask: [[Category:!City]] }} will query every page in category "!City" instead of every page that is not in category "City". (talk)

See also[edit]