Search operators

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

Semantic MediaWiki provides different search operators to enable a user to refine search conditions and criteria.

Wildcards

Wildcards are written as "+" and allow any value for a given condition. For example, [[born in::+]] returns all pages that have any value for the property «born in». Please note that "+" can only be used by itself. See the section Like, not like for the wildcards "*" and "?".

Comparators

Comparators are special symbols like < or >. They are placed after :: in property conditions.

  • >> and <<: "greater than" and "less than"
  • > and <: "greater than or equal" and "less than or equal" by default, but "greater than" and "less than" if configuration parameter $smwStrictComparatorsSets whether the ">" and "<" comparators should be strict is set to true.
  • and : "greater than or equal" and "less than or equal"
  • !: "not" ("unequal")
  • ~: «like» comparison for strings
  • !~: «not like» comparison for strings

Comparators work only for property values and not for conditions on categories. A wiki installation can limit which comparators are available, which is done by the administrator by modifying the value of Help:Con­figu­ration para­meter "$smwgQComparators" as explained.

Depending on the value of $smwStrictComparators, interpretation of > and < can differ; the different behaviours are documented on the page about Strict comparators.

When applying comparators to pages, then the title of the page (without namespace prefix) is used. However, this can be changed by setting another MediaWiki sortkey for that page, e.g. {{DEFAULTSORTKEY:custom key}}. Please mark that this applies to all comparators, including ! and ~. It is not possible to have multiple sortkeys for one page. In particular, redirect pages are not taken into account when applying comparators.

Not equal

You can select pages that have a property value which is unequal to a given value. For example,

  [[Area code::!415]]

will select pages that have an area code which is not «415». Note that this query description does not look for pages which do not have an area code 415. Rather, it looks for all pages that (also) have a code unequal to 415. In particular, pages that have no area code at all cannot be the result of the above query.

As with the (default) equality comparator, the use of Custom units may require rounding in numeric conversions that can lead to unexpected results. For example, [[height::!6.00 ft]] may still select someone whose height displays as «6.00 feet» simply because the exact numeric value is not really 6. In such situations, it might be more useful to query for pages that have a property value outside a certain range, expressed by taking a disjunction (see below) of conditions with < and >.

Like, not like

The comparators ~ and !~ work only for properties of datatype "String" and "Page" (SMW ≤ 1.7.x) or "Text" and "Page" (SMW ≥ 1.8.x) and since Semantic MediaWiki 1.3.0 also for "Email"1, "Telephone number"1 and "Date"2. In a like condition, one uses '*' wildcards to match any sequence of characters and '?' to match any single character. For example, one could ask [[Address::~*Park Place*]] to select addresses containing the string "Park Place", or [[Honorific::~M?.]] to select both "Mr." and "Ms.".

Note that in case of datatype "String" (SMW ≤ 1.7.x) all 255 storable characters are searchable, in case of datatype "Text" (SMW ≥ 1.8.x) only the first 40 characters (if more than 72 characters were stored as property value) or all 72 characters (if a maximum of 72 characters were stored as property value) are searchable as explained.[1]. For datatype "Page" all 255 characters are searchable (all SMW versions).

Also note that some special characters need masking when used in combination with '~'-comparator.

Example

Property "Path" of datatype "Text" holds something like "n:\path\morepath" as data value. To query for all pages that contain "n:\path\..." in property "Path" you need to mask the backslashes "\" to your query like this:

{{#ask:
 [[Path::~n:\\path\\+]]
  ...
}}
Please note the help page on how do do searching in connection with the full-text search feature enabled.

Ranges

Greater than or equal, less than or equal

With numeric values, you often want to select pages with property values within a certain range. For example

[[Category:Actor]] [[height::>6 ft]] [[height::<7 ft]]

asks for all actors that are between 6 feet and and 7 feet tall. Note that this takes advantage of the automatic unit conversion: even if the height of the actor was set with [[height::195cm]] it would be recognized as a correct answer (provided that the datatype for height understands both units, see Custom units). Note that the comparator means greater/less than or equal – the equality symbol = is not needed.

Such range conditions on property values are mostly relevant if values can be ordered in a natural way. For example, it makes sense to ask [[start date::>May 6 2006]] but it is not really helpful to say [[homepage URL::>http://www.somewhere.org]].

If a datatype has no natural linear ordering, Semantic MediaWiki will just apply the alphabetical order to the normalised datavalues as they are used in the RDF export. You can thus use greater than and less than to select alphabetic ranges of a string property. For example, you could ask [[surname::>Do]] [[surname::<G]] to select surnames between «Do» and up to «G». For wiki pages, the comparator refers to the name of the given page (without the namespace prefix).

Here and in all other uses of comparators, it might happen that a searched for value really starts with a symbol like <. In this case, SMW can be prevented from interpreting the symbol as a comparator if a space is inserted after ::. For example, [[property:: <br>]] really searches for pages with the value «<br>» for the given property.

Greater than, less than

At times you might want to exclude the precise value from the result itself, e.g. to find an actor taller than Hugh Laurie (1.89m), you can query using a combination of the > comparator and the ! comparator:

[[Category:Actor]] [[height::>1.89m]] [[height::!1.89m]]

Strict comparators

The default behaviour of SMW, where comparators < and > mean "less than or equal" and "greater than or equal", can be somewhat confusing for people that are familiar with the mathematical meaning of < and >. Thus you can choose to have SMW interpret < and > "strict", as explained here.

Values containing operator symbols

Cases may occur where the value of a given property starts with one of the following symbols: <, ≤, >, ≥, =, ! and ~. To assure that Semantic MediaWiki can handle those cases, value notations are white-space sensitive. To avoid confusion a space is inserted after ::. For example, [[property:: <br>]] really searches for pages with the value "<br>" for the given property.

Case insensitivity

Starting with Semantic MediaWiki 3.0.034 it is possible to enable case insensitive matching for properties of e.g. datatype "Page", datatype "Text", datatype "Code" and datatype "URL" (blob types, i.e. strings or text) using the SMW_FIELDT_CHAR_NOCASE option to configuration parameter $smwgFieldTypeFeaturesSets relational database specific field type features.

NoteNote: The full-text search feature is in most cases preferable to use over the feature described here.

The following four examples will provide an overview of the general differences between having this feature enabled or not:

Example 1

Property "Has text" of datatype "Text" holds "CAseInSensitiveSearch" as data value on page "Example". If enabled queries No. 1 to 3 and if not (default) queries No. 1 to 3 will also select page "Example":

  1. {{#ask: [[Has text::~CASEIN*]] |?Has text }}
  2. {{#ask: [[Has text::~casein*]] |?Has text }}
  3. {{#ask: [[Has text::~CAseIn*]] |?Has text }}
Example 2

Property "Has page" of datatype "Page" holds "CAseInSensitiveSearch" as data value on page "Example". If enabled queries No. 1 and 2 and if not (default) query No. 3 will select page "Example":

  1. {{#ask: [[Has page::~CASEIN*]] |?Has page }}
  2. {{#ask: [[Has page::~casein*]] |?Has page }}
  3. {{#ask: [[Has page::~CAseIn*]] |?Has page }}
Example 3

Property "Has text" of datatype "Text" holds "CAseIn" as data value on page "Example". If enabled queries No. 1 to 3 and if not (default) query No. 3 will select page "Example":

  1. {{#ask: [[Has text::CASEIN]] |?Has text }}
  2. {{#ask: [[Has text::casein]] |?Has text }}
  3. {{#ask: [[Has text::CAseIn]] |?Has text }}
Example 4

Property "Has page" of datatype "Page" holds "CAseIn" as data value on page "Example". If enabled query No. 3 and if not (default) query No. 3 will select page "Example":

  1. {{#ask: [[Has page::CASEIN]] |?Has page }}
  2. {{#ask: [[Has page::casein]] |?Has page }}
  3. {{#ask: [[Has page::CAseIn]] |?Has page }}
    1. See this mailing list post for detailed information

    References

    1. a b  Semantic MediaWiki: GitHub pull request gh:smw:1129
    2. ^  Semantic MediaWiki: GitHub pull request gh:smw:1178
    3. ^  Semantic MediaWiki: GitHub issue gh:smw:1912
    4. ^  Semantic MediaWiki: GitHub pull request gh:smw:2499