In ProcessWire 3.0.160 we’ve got some major upgrades and additions to our text-matching selectors and operators. This brings a whole new level of power to $pages->find() and similar API calls, especially when it comes to search engine type queries.
We will briefly walk through all the new operators in this post to introduce them. Following that is a demo search engine that lets you test all of them out with pages on this site. All of these new operators are available and ready to use in ProcessWire 3.0.160 API calls. Note however that they are not yet available interactively in InputfieldSelector (Lister/ListerPro) or other places where you might select operators interactively in the admin. They should be by 3.0.161 though. Just in case you missed the forum post last week, 3.0.160 also includes some nice two-factor authentication upgrades as well.
In the examples below, you'll see unique search queries included with each. If you don't know what terms to try in the search engine further down on this page, I recommend using the terms in the examples for results that help to demonstrate how the operator works.
Newly added search operators
Contains words partial ~*=
This is like the existing "match words" ~= operator except that rather than just matching whole words, it can match partial words as well. So that means that a search for "web image" on this site will match terms like "WebP" and "images" (plural), rather than only matching "web" and "image" (singular).
Contains words live ~~=
This new operator is designed to work exactly like the existing "match words" ~= operator except that the last word is considered a "partial match" word rather than a full match word. That makes this particular operator useful in live-search situations where you are returning results as someone types.
Contains words like ~%=
This operator matches all words in the query in full or in part. It can perform partial matches not just from the beginning of the word, but anywhere within the word. That means that a word like "build" can match words like "building" and "rebuild", and words like "site" can match words like "website" and "sites".
Contains words and expand
This operator works exactly like the regular "match words" ~= operator except that it also adds in "query expansion". This is a feature of MySQL fulltext indexes where in the best case it seems to magically come up with related matches, even if they don't contain the original search terms. As far as I can tell, it analyzes the matching results and looks for words in the match that might be fairly unique, checks if those words appear on any other page titles, and bundles them into the results when they do. As an example, use this operator in the search engine below to search for the term "books", and notice how it matches "Canongate Books" and a related blog post that doesn't even mention the term "books" in the title — pretty cool huh? Well, at least with short queries it can be. The longer the query, the more likely that query expansion is to introduce noise into the results, but that's to be expected.
Contains any words ~|=
$pages->find('title~|=architecture engineering construction');
ProcessWire's word matching operators have required that all of the mentioned words match, but not anymore. Now we have a few new implied OR operators like ~|= (and others, mentioned below) that specify "any" words can match. As you might know, you can also do this by separating each word with a pipe, i.e. "this|that", but that's a bit more work than we'd like when processing searches from input. In addition, because the feature is native to this operator, it is able to perform the OR match more quickly and efficiently than if we were separating our search values with pipes.
Contains any partial words ~|*=
This is just like the previous operator except that rather than just matching any whole word, it'll also match any partial word. That means that words like "auth" will match "author" and "authentication", and words like "edit" will match "editor" and "editing".
Contains any words like ~|%=
This is just like the above mentioned ~|*= operator except that it uses LIKE rather than the fulltext index to perform matches. That means that it can be slower (at scale) but can match anywhere in words, rather than just from the beginning of them. That means that a term like "grade" can also match "upgrade" and a term like "port" can match "import", "export", "portal", etc.
Contains phrase and expand *+=
This is a variation of our common "phrase match" *= operator that adds in MySQL query expansion. That enables it to add in other potentially relevant pages into the matches, even if they don't have the target phrase. For instance, notice that a search for "CDN" also matches pages about ProCache, even if they don't mention CDNs in the title.
Contains match **=
This uses something more like the standard fulltext MATCH/AGAINST logic included with MySQL than most of the other operators. For those that want this more traditional search logic, this operator provides it. It behaves in an OR fashion with the words. Since fulltext indexes consider "book" and "books" to be unrelated words (unless partial matching), ProcessWire also includes plural versions of singular words, when possible.
Contains match and expand **+=
This is just like the "contains match" operator mentioned above, except that it also adds in MySQL query expansion, discussed in earlier operators.
Advanced text search #=
Searches using this operator recognize special command characters that designate what is, and is not included. When "+" is prefixed to a word or quoted phrase, it indicates that term must be included. When "-" is prefixed to a quoted word or phrase, it indicates that term must NOT be included. When there is no prefix, then it means the term or phrase is optional but its presence will increase ranking. Any word can be appended with an asterisk "*" to indicate that you also want to match any words that begin with the term. For example "bar*" will match not just bar, but also barn, barbell, barge, etc.
Test out any of these new operators and/or compare the results with existing operators below. This engine searches mostly blog posts and sites directory pages on this site, and a few others. Note that it is limited to 10 results per operator, per search. If you aren't sure what to search for, try using the terms from any of the examples above, all of which were identified as a good demonstration of the operator they appear with.
1. Check boxes for the operators you want to test
Post a comment