Questions answered by this recipe
- How can I sort search results in PmWiki, so that best matching pages (with biggest number occurences of the search terms) are shown first?
- When searching for pages in PmWiki, can I show fragments of pages with search terms rendered in bold?
Sorts search results by score + shows page excerpts with search terms in bold.
(:pagelist list=normal fmt=score count=3 foo bar:)
may result in results as shown below:
By default, immediately after installation, this format will be set as the default format, in which your search results are displayed.
Initial part of the installation is standard:
- download the sortbyscore.phpΔ file
- put it in your PmWiki's
- in your
local/config.phpfile add a line of:
- Optionally: install the Cookbook:Excerpts recipe if you want fragments of pages which matched the search to be printed (note that 'include_once()' for Excerpts must go above SortByScore).
From now on the recipe is active. It is enabled by default for your search results page (Site.Search). If you want to disable that behaviour, you can add the following line to your
local/config.php file, above the
$SortByScoreDefault = false;
$SortByScoreTitleBonus = 100;
- This parameter defines a multiplier for each hit in page's title. Change to 1000 or more if you have many pages in your wiki. Change to 1 if you want page title hits to be counted as regular ones, and to 0 if you want to count only page text hits.
$EnableSortByScoreExcerpts = 1;
- By default, if you include Cookbook:Excerpts before including SortByScore, each search result gets a fragment of page text with searched terms highlighted. If you want to disable this behaviour and still be able to use Excerpts, set this variable to 0.
- Tested on PmWiki versions: 2.2.0-beta63, 2.1.27 (latest stable), 2.0.0
- Note: might not work on older 2.2.0-betas
- 20171110 - Update for PHP 5.5 and 7.2, requires PmWiki 2.2.56 or newer (Petko)
- 2007-09-32 - v1.2
- 2007-09-26 - v1.1 - by Mateusz
- works with newest beta (pmwiki-2.2.0-beta63) as well as latest stable at the moment (pmwiki-2.1.27); tested also with pmwiki-2.0.0 - works well (!)
- each match in page's title is multiplied by a bonus value (
- works properly when search term includes '/' (bug reported by Chillax - thanks)
- minor tweaks and bug fixes (also: display group+title in place of group+pagename)
- 2006-12-21 - v1.0 - by Mateusz
- first release
- beta version - but it's already working very well and can be used
Using v2.2.0 beta 17 and installation results in 0 search results being displayed. Disabling this and using the default search behavior provides correct results. Dec 26 06 - Ted
2006-12-29 :: Chillax When search term includes a "/", I receive the following errors (the foreach error appears to occur once for each result found):
Warning: Invalid argument supplied for foreach() in /path/to/pmwiki/cookbook/sortbyscore.php on line 74 Warning: implode() [function.implode]: Bad arguments. in /path/to/pmwiki/cookbook/sortbyscore.php on line 85
I am using PmWiki v2.1.27 and have implemented PerGroupSubDirectories
2007-01-03 Anke Wehner I'd like to have pages with the tearch term(s) in the page name displayed at the top of the list - maybe by giving those a default +100 on the score? But I don't understand the function well enough. Help?
2007-02-12 Dan Raymond Can I get more than one line around the search term
- Edit the line $side=30; in function ExcerptMarkup in excerpts.php to change the number of characters displayed either side. Francis March 02, 2007, at 04:15 AM
2007-03-14 Steven Driskell
After downloading sortbyscore.php, excerpts.php and updating the config.php file, I received the below error when executing a search: Fatal error: Call to undefined function: html_entity_decode() in excerpts.php on line 97.
I think that must be because of PHP older than 4.3. A quick and possibly working solution should be changing line 97 in excerpts.php from
html_entity_decode( trim( $par ) ) );
trim( $par ) );
and also in line 101 fragment:
That should work, hopefully not much slower. Thanks for info!
2007-04-02 Steven Driskell - The above code fixed my problem. Nice work!