Help:List the set of unique values for a property

From semantic-mediawiki.org
Jump to: navigation, search

Semantic extension(s): Semantic MediaWiki

Further extension(s): Arrays

Keyword(s): property values, unique values

Description:

Warning: Using the following may lead to a problem with duplicate SMW properties.

One feature that is still missing from SMW is the ability to easily list the set of unique values that have been assigned to a given property. To achieve this goal, we need to use the Arrays extension.

Note in advance: While values of type "Page" may be retrieved using inverse querying, this approach does not work for other property types such as "Text".

Step one

First, we need to query all the values for a given property in a simple, unformatted list. This step can be quite challenging if you are not familiar with the Ask syntax! Lets assume the property in question is called 'MyProp', the query would look something like this:

{{#ask: [[MyProperty::+]] |mainlabel=- |headers=hide |?MyProperty }}

Lets try an example on this wiki, using the property 'Has conference', which gives the following result:

SMWCon Spring 2013, SMWCon Fall 2012, 1st SMW webinar for developers, SMWCon Fall 2016, SMWCon Spring 2014, SMWCon Spring 2014, SMWCon Spring 2014, SMWCon Fall 2013, SMWCon Fall 2013, SMWCon Spring 2014, SMWCon Fall 2016, SMWCon Fall 2016, SMWCon Fall 2014, SMWCon Spring 2013, SMWCon Fall 2016, SMWCon Fall 2012, SMWCon Spring 2013, SMWCon Fall 2016, SMWCon Spring 2013, SMWCon Fall 2013, SMWCon Fall 2012, SMWCon Fall 2015, SMWCon Fall 2012, SMWCon Fall 2013, SMWCon Fall 2013, SMWCon Fall 2013, SMWCon Fall 2015, SMWCon Spring 2015, SMWCon Fall 2015, SMWCon Fall 2013, SMWCon Fall 2012, SMWCon Spring 2014, SMWCon Spring 2013, SMWCon Spring 2014, SMWCon Fall 2012, SMWCon Fall 2013, SMWCon Fall 2013, SMWCon Spring 2014, SMWCon Fall 2015, SMWCon Fall 2014, SMWCon Fall 2015, SMWCon Fall 2016, SMWCon Fall 2012, SMWCon Fall 2014, SMWCon Fall 2015, SMWCon Fall 2016, SMWCon Spring 2014, SMWCon Spring 2013, SMWCon Fall 2013, SMWCon Fall 2012... further results

See the note below if there too many results to be presented on a single page.

Step two

Next, we need to use the arraydefine function of the Arrays extension. This step has been greatly simplified in newer versions of the extension. For example:

{{#arraydefine: MyArray
 | red, red, green, blue, yellow, red, red, blue, green
 | , | print=list, sort=asc, unique
}}

Which gives the following output:

blue, green, red, yellow

Step three

Putting it together, we'd do something like this:

{{#arraydefine: my.array
 | {{#ask: [[MyProperty::+]] |mainlabel=- |headers=hide |?MyProperty |limit=10000 }}
 | , | print=list, sort=asc, unique
}}

Template

So much code! Lets wrap it up in a nice convenient template! :-)

Calling the template like this:

{{Property values|Has conference}}

Gives the following result:

1st SMW webinar for developers, SMWCon Fall 2010, SMWCon Fall 2011, SMWCon Fall 2012, SMWCon Fall 2013, SMWCon Fall 2014, SMWCon Fall 2015, SMWCon Fall 2016, SMWCon Spring 2013, SMWCon Spring 2014, SMWCon Spring 2015, SMWCon

Limitations and issues

Duplicate properties

As noted above, this approach may lead to a problem with duplicate SMW properties.

Limitation with numerous values

This approach may present a problem when a query returns numerous values: the query needs to produce all values of the property in order for the individual instances of the array to be made unique. That's why the above query (and the query in the template) has a very large value for the 'limit' option (limit = 10000). If the limit is set too low, you will see a string "further results" appearing in the list of values. Solutions:

  • If you can, increase the limit of your values query.
  • If this is not an option for you (because the total number of values is too large for your website), there may be no workaround.

Possible alternatives

Retrieving values from Special:SearchByProperty

  • Chris Koerner has described an alternative based on Special:SearchByProperty on his blog here. Note that because it relies on this special page, more work may be necessary in order that values are correctly transliterated to their URL equivalent in encoding.

One value per subobject

If it is possible and desirable in your case, a more radical alternative to consider is to use dedicated subobjects for every single value.