Help:List the set of unique values for a property

From semantic-mediawiki.org
Semantic extension(s): Semantic MediaWiki
Further extension(s): Arrays
Keyword(s): property values · unique values

Description:

To list the set of unique values that have been assigned to a given property, use the result format "valuerank"Lists values based on their occurrence:

Query
{{#ask:
 [[Has conference::+]]
 |?Has conference
 |format=valuerank
 |limit=5000
 |headers=hide
}}
Result

Alternative methods[edit]

Arrays extension[edit]

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

This approach uses the Arrays extension. It's a bit more complicated but also more flexible.

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[edit]

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 syntax of the #ask parser function! Lets assume the property in question is called 'MyProperty', 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 2018, SMWCon Fall 2019, 1st SMW webinar for developers, SMWCon Fall 2021, SMWCon Fall 2018, SMWCon Fall 2016, SMWCon Spring 2014, SMWCon Fall 2020, SMWCon Fall 2019, SMWCon Spring 2014, SMWCon Fall 2019, SMWCon Fall 2021, SMWCon Spring 2014, SMWCon Fall 2023, SMWCon Fall 2018, SMWCon Fall 2013, SMWCon Fall 2017, SMWCon Fall 2013, SMWCon Spring 2014, SMWCon Fall 2022, SMWCon Fall 2016, SMWCon Fall 2016, SMWCon Fall 2014, SMWCon Spring 2013, SMWCon Fall 2016, SMWCon Fall 2012, SMWCon Fall 2023, SMWCon Fall 2022, SMWCon Spring 2013, SMWCon Fall 2020, SMWCon Fall 2016, SMWCon Fall 2018, SMWCon Fall 2019, SMWCon Spring 2013, SMWCon Fall 2013, SMWCon Fall 2012, SMWCon Fall 2015, SMWCon Fall 2012, SMWCon Fall 2021, SMWCon Fall 2022, SMWCon Fall 2021, SMWCon Fall 2021, SMWCon Fall 2013, SMWCon Fall 2017, SMWCon Fall 2013, SMWCon Fall 2013, SMWCon Fall 2019, SMWCon Fall 2019, SMWCon Fall 2015... further results

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

Step two[edit]

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: my.array
| 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[edit]

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

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

Template[edit]

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 2012... further results, SMWCon Fall 2013, SMWCon Fall 2014, SMWCon Fall 2015, SMWCon Fall 2016, SMWCon Fall 2017, SMWCon Fall 2018, SMWCon Fall 2019, SMWCon Fall 2020, SMWCon Fall 2021, SMWCon Fall 2022, SMWCon Fall 2023, SMWCon Spring 2010, SMWCon Spring 2011, SMWCon Spring 2012, SMWCon Spring 2013, SMWCon Spring 2014, SMWCon Spring 2015, SMWCon

Limitations and issues[edit]

  • 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, not all values will be returned. 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.

Retrieving values from Special:SearchByProperty[edit]

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[edit]

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