Help:Semantic search

From Shifti
Jump to: navigation, search

Semantic properties are useless if you can't use them in some way, so the creators of the Semantic MediaWiki extension have provided three facilities for querying the database about them: The 'Ask' Special Page,the {{#ask}} parser function and the {{#show}} parser function.



This page has two panes — the left is where you enter your query and the right is where you can ask for it to output additional pieces of semantic information. The left pane expects the query in a certain format — it is almost a complete database query language, but the advanced features need not be used. On the other hand the right pane is much simpler, and while it also expects input to be in a certain format, it is not as complex as the query setting.


The queries are actually a bit simpler than I have made them sound, though constructing a proper query can be tricky. By default you just enter a series of terms–similar to what you are used to with Google but the terms are required to be in a specific format–and the system will use the "AND" principle to find pages that match every one of the specified terms.

Extra Information

The right-hand page is meant to be used to detail extra properties you want displayed in the results. These are formatted as: ?<property name>



This is how you can create an "inline query" that is embedded on a page. The basic format of this really special tag is:

{{#ask: <query>
| ?<property name>


The 'query' parameter of the tag can be any valid query that you'd pass to Special:Ask and the 'property name' parameters are what you would enter into the right-hand pane of the Special:Ask page.

There are other parameters available and these let you specify the format of the output and a lot of other factors… An example of a quick&dirty query without any of the extra parameters is: {{#ask: [[Category:Story]][[setting::Xanadu]][[author::ShadowWolf]] | ?title | ?setting | ?author }} Which produces the following results:

But by adding the 'format' parameter we get: {{#ask: [[Category:Story]][[setting::Xanadu]][[author::ShadowWolf]] | ?title | ?setting | ?author | format=broadtable }} Which generates:

Big difference, no?

The 'format' parameter and the demonstrated possible value for it of 'broadtable' is just an example. See the Semantic MediaWiki Help Page for more information on the possible values and more.


The {{#show}} parser function allows for directly inserting the results of a semantic query inline into a page. The basic format is the same as that of the {{#ask}} function, with the exception that you give it a page name (what you'd put inside the double-brackets to create a link) instead of a query. For instance, if you want to know the setting of 'Ripples in Time' you could just do {{#show: User:ShadowWolf/Ripples in Time|?setting}} —

A different form, and one that is slightly more powerful, is by using a special form of the {{#ask}} – {{#ask:<page link>|?<property>=}}. Taking the previous example, that would be {{#ask:[[User:ShadowWolf/Ripples in Time]]|?setting=}} - which generates the result:
ShadowWolf/Ripples in Time

General Information

Category Matches

To match against a category, you enter it in the same format you would use to add the category to a page—ie:[[Category:Story]]. This will return every page that is either directly or indirectly[1] in that category.

Property Matches

Matching against properties is very similar–the format is [[<property name>::<property value>]].[2]


Say, for instance, you want to match every page that is fiction and not in the Paradise setting–with just property matches and category matches this isn't possible. However there is a way to do it—by using a 'comparison operator'. The solution to the problem I set would be to have [[setting::!Paradise]] as one of the terms. The available comparison operators are:

The value is less than the specified value. ([[height::<6 ft]], for instance, would match any 'height' property that was set to a value under six feet.
The exact opposite of the 'less than' operator.
The value is not the same as what the property is set to. (see the opening example)
This is the "similarity" operator, used for matching a small piece of text in a larger value. ([[name::~*Xanadu*]] would match all properties that had the word 'Xanadu' in them)


There is only one wildcard defined for matching semantic properties and that is the plus sign (+). It is used to mean 'any value' – ie: [[setting::+]] would match any page that had the 'setting' property set.


If you want to match a series of properties—perhaps you're looking for a story that is either set in Xanadu or Paradise then the 'AND' method that the most basic of searches occurs in won't work—you need an 'OR' type of search. To do that, simply put the word 'OR'[3] in between the terms.

To simplify things you could format the above example as: [[setting::Paradise||Xanadu]] – this form is what I'd recommend be used.

Namespace Restriction

On Shifti this isn't that useful in general. However, if you are trying to spot a Special Page or a Help Page then this will be very handy. To restrict a search to a namespace you'd add [[<namespace name>:+]] to the list of search terms.(ie: to restrict the search to the 'Help' namespace, you'd add [[Help:+]] to the list of search terms)

Chained Queries

You can also 'chain' queries together. Suppose, for a moment, that Shifti offered the same breadth of articles as Wikipedia and you wanted to find a list of all actors born in Italy. You know there is a list of cities located in Italy and also know that there is a 'born in' property, but the classic query syntax doesn't let you feed the output of one query into another.

By chaining the queries together you can solve this problem. The solution to the above problem would be a query with the terms: [[Category:Actor]] [[born in::<q>[[cities in::Italy]]</q>]]. Yes—the <q> tag gets wrapped around the query! And this can be nested to any depth. (ie: to extend the above to all of europe you'd just change the [[born in]] term to: [[born in::<q>[[cities in::<q>[[countries in::Europe]]</q>]]</q>]])

Templates and Variables

Templates and Template Variables can be used in queries. You can just insert them into the "value" side of things when you want to use them.[4]


  1. Indirectly, in this case, means 'is not in the category, but is in a sub-category (add as many sub-'s as you want) of that category
  2. This is, technically, also the format for adding a property directly. However, because this creates links to pages with the name of the property value, we ask that you use the Semantic Property Template ({{SMW Base}}
  3. Yes, without the quotes :P
  4. {{#vardefine}} style variables could possibly be used, but there is no information available as to whether this is true or not, so we'd recommend you not use them in queries.