| Table of Contents | |
|---|---|
Full-text search (FTS), once it is enabled for SQL, offers features that help to overcome the native limitations of searching in property values. It changes the standard behaviour of existing syntax and comes with additional syntax options.
Full-text search is typically word-driven, language-dependent and optimised for large chunks of text. Features include word tokenization, phrase matching, wide proximity and search highlighting.
It may not provide the right solution for you if you require more exact forms of pattern-matching. For those use cases, queries using the like:/nlike: notation should continue to provide a fallback.
Data sample
For the examples in this guide we will store a paragraph as a semantic annotation using the property "Has text" of datatype "Text"Holds text of arbitrary length.1 Note that while it's possible to enable FTS indexing for properties of datatype "Page"Holds names of wiki pages, and displays them as a link, this situation is not (yet) covered below.
... The principles of definition, the law of contradiction, the fallacy of arguing in a circle, the distinction between the essence and accidents of a thing or notion, between means and ends, between causes and conditions; also the division of the mind into the rational, concupiscent, and irascible elements, or of pleasures and desires into necessary and unnecessary --these and other great forms of thought are all of them to be found in the Republic, and were probably first invented by Plato. The greatest of all logical truths, and the one of which writers on philosophy are most apt to lose sight, the difference between words and things, has been most strenuously insisted on by him, although he has not always avoided the confusion of them in his own writings. ...
Features
Tokenization
Tokenization is an essential part of FTS:
- Spaces, certain punctuation marks and other typographic marks are treated as delimiters that subdivide a string into individual words or 'tokens', making it easier to match on them. If a space is required as part of the match, use phrase matching instead, as illustrated below.
- Not everything is a token. To reduce noise and aim at greater efficiency, some words/tokens are ignored by the indexer. This is true even if it may come at the cost of leaving out some meaningful words.
- The SQL parser rejects tokens that do not meet the minimum token length requirement. The minimum length, which is configurable, is 3 by default; for CJK, however, this minimum is internally set to 1.
- Depending on the kind of language support provided, certain 'stopwords', or words that may be considered too common or functional to be meaningful, are not indexed. For example, no result is returned for a query condition like
[[Has text::~probably]]if "probably" is indexed as a stopword. For documentation on stopwords, see that published by MariaDB and MySQL. Additional stopword detection is done with the TextSanitizer that comes with SMW. - If the entire string is rejected for FTS because either of these reasons, SMW will fall back to regular SQL search with LIKE/NLIKE.
Case and accent folding
- FTS allows for case-insensitive searches to be made. For instance, a query with
[[Has text::~conTradiCtIon]]should let you find results containing the word 'contradiction'. - To an extent, it also allows for diacritic-insensitive searches.
Matching with and without wildcards
- You do not need a wildcard if you want to match full words within a longer string, e.g.
[[Has text::~contradiction]]. - To match on any individual token, you can still use the asterisk as a wildcard, e.g.
[[Has text::~contradict*]]. Leading and word-internal asterisks are not supported.- Tokenization introduces a limitation that not exist for standard SQL search. If the full string is broken into a series of multiple tokens, it is not possible to match the full string exclusively at its beginning: all tokens are evaluated in their own right. This means, for instance, that if you want to list every novel whose title begins with the letter S and titles are recorded by a dedicated semantic property, an FTS query is probably the wrong tool.
Boolean operators (+ / -)
+of* or -of*. Otherwise your query may exit with a Runtime Exception or hard database error. A fix is forthcoming.
The following condition lets you find results containing at least one of the tokens 'contradiction' and 'irascible':
[[Has text::~contradiction irascible]]
Full-text match search also supports the use of boolean operators + / -, which can be prefixed to any token in the query to broaden or narrow down the scope. Prepend a plus sign to combine tokens or a minus sign to exclude one.
To indicate that both tokens from our example must be included, you would use the boolean operator.
[[Has text::~+contradiction +irascible]]Boolean and wildcard operators can be used together on the same token, as long as their positioning is correct. Trailing boolean operators and leading wildcards are not supported.
- Examples
{{#ask: [[Has text::~fallacy hippopotomonstrosesquippedaliophobia]] }}{{#ask: [[Has text::~+fallacy +logical]] }}{{#ask: [[Has text::~+fallacy -logical]] }}{{#ask: [[Has text::~+fallacy +log*]] }}Phrase matching
- Phrase matching is done in either one of two ways:
- by putting double quotes on either side of the phrase ("..."),
- or by using
phrase:expression in front of the phrase (replacing the tilde). The one limitation of this expression is that it only supports single phrases.
- Many of the comments above apply equally to phrase matching. For instance:
- The parser ignores stopwords and tokens that are too short.
- Phrase matching works for both uppercase and lowercase characters.
- Example using double quotes
Query:
{{#ask:
[[Has text::~"probably first invented by plato"]]
|?Has text#-hl
|format=plainlist
}}Result:
Because 'by' is too short to be treated as a token and possibly a stopword, you can omit it from the phrase:
{{#ask:
[[Has text::~"probably first invented plato"]]
|?Has text#-hl
|format=plainlist
}}
- Example using
phrase:
{{#ask:
[[Has text::phrase:probably first invented by plato]]
|?Has text#-hl
|format=plainlist
}}
Wide proximity using two tildes
Broad text search tries to match a string text without a specific property to broaden possible result matches. To initiate a full-text match search specific an additional ~ will indicate to the QueryEngine to use the full-text index. In other words, if the property is unknown, just use two tildes (~~) instead of one:
Query:
{{#ask:
[[~~probably first invented by Plato]]
|format=broadtable
|link=all
|headers=show
}}
How it works:
[[~~ first invented by Plato]]is translated into the SQL-query(MATCH(t0.o_text) AGAINST ('first invented by plato' IN BOOLEAN MODE) )[[!~~ first invented by Plato]](the negative) is translated into the SQL-query(MATCH(t0.o_text) AGAINST ('-first invented by plato' IN BOOLEAN MODE) )
Wide proximity using phrase:
The phrase: expression is also available for wide proximity searches. It should offer an exact equivalent to the approach above using two tildes. Example:
{{#ask:
[[phrase:probably first invented by Plato]]
|format=broadtable
|link=all
|headers=show
}}
Reference table
| Expression | Description | Example of a condition |
|---|---|---|
~
|
Default operator to use for full-text search | |
!~
|
Negative counterpart to ~. Alternatively, use ~ with a minus as boolean operator.
|
[[Has text::!~invented]] // same as: [[Has text::~-invented]] |
*
|
Wildcard operator that can be appended to one or multiple tokens | [[Has text::~contradicti*]] |
+
|
Boolean operator that can be prepended to a token to indicate that every query result must include the token. | [[Has text::+contradiction]] |
-
|
Boolean operator that can be appended to a token to indicate that no query result must include the token. | [[Has text::-contradiction]] |
~~ / !~~
|
Wide proximity search | [[~~probably first invented by Plato]] |
"..."
|
Phrase matching. Multiple phrases allowed. Does not support wide proximity searches. | [[Has text::~"probably first invented by Plato"]] [[Has text::~"concupiscent and irascible" "invented by Plato"]] |
phrase:
|
Alternative syntax for phrase matching and wide proximity searches. Single phrases only. | [[Has text::phrase:probably first invented by Plato]] [[phrase:probably first invented by Plato]] // wide proximity |
For a comparative table covering SQLStore (default), SQLStore with FTS and Elasticstore, see here.
Alternative search modes
Most of the features described so far uniquely belong to the standard full-text search mode called 'boolean', which is specified by default. Two other modes are available for use. You can tune in to any mode by adding the relevant shorthand code at the end of the query condition.
&INL(IN NATURAL LANGUAGE MODE). Example:[[Has text::~arguing in a circle&INL]]&QEX(WITH QUERY EXPANSION). Example:[[Has text::~athletics boxing badminton&QEX]]&BOOL(IN BOOLEAN MODE). Because 'boolean' is already the default, there is no need to add it.
Search highlighting
Search highlighting2 is done by adding the #-hl formatter to the printout statement of the respective property.
- Query
{{#ask: [[Has text::~"probably first invented by plato"]]
|?Has text#-hl=
|format=plainlist
}}
- Result
How to do searching with the full-text search (... The principles of definition, the law of contradiction, the fallacy of arguing in a circle, the distinction between the essence and accidents of a thing or notion, between means and ends, between causes and conditions; also the division of the mind into the rational, concupiscent, and irascible elements, or of pleasures and desires into necessary and unnecessary --these and other great forms of thought are all of them to be found in the Republic, and were probably first invented by Plato. The greatest of all logical truths, and the one of which writers on philosophy are most apt to lose sight, the difference between words and things, has been most strenuously insisted on by him, although he has not always avoided the confusion of them in his own writings. ...)
- This example also demonstrates that highlighting is case-insensitive: note the difference between Plato (with an uppercase initial) in the original text and plato (as part of the query string).
- All occurrences of strings included in the query statement will be highlighted as well, e.g. "by" in the above example.
Regular SQLStore search (like: / nlike:)
As stated in the lede, you won't lose access to regular SQL search (LIKE/NOT LIKE:) capabilities if FTS-based search is enabled. To make use of it, you will have to make a small syntax change: instead of starting with ~ / !~, use like: / nlike:. Everything else should work as documented on this manual page about search operators.
It is possible to mix both types of search and get the best of both worlds, as in this example:
// Find results containing both 'ntradiction' and the word 'circle' in the property value
[[Has text::like:*ntradiction*]] [[Has text::~circle]]More examples
References
- ^ Semantic MediaWiki: GitHub issue #1481 example
- ^ Semantic MediaWiki: GitHub pull request gh:smw:2253