SortByScore

Summary: Sorts (:pagelist:) search results by number of matching words found in page + displays fragments of page near the word.
Version: 20171110
Prerequisites: PmWiki2.2.56+
Status: beta
Maintainer: None (original author: MateuszCzaplinski)
Users: +1 (View / Edit)
Categories: Searching PHP72

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?

Description

Sorts search results by score + shows page excerpts with search terms in bold.

This recipe is intended to be used as a value for the fmt= option of (:pagelist:) and (:searchbox:) directives (see: PmWiki.PageLists). For example, a pagelist like that:

 (:pagelist list=normal fmt=score count=3 foo bar:)

may result in results as shown below:

PmWiki.ReleaseNotes - score 18

...--XMLHeader-->@] and [@<!--XMLFooter-->@] as aliases for [@<!--...
...t or cookie. * The [[Site.SideBar]] page now defaults its edit ...

PmWiki.WikiGroup - score 15

...e a default page in the group Foo, create a link to [=[[Foo/]]=...
....]]=]). To make a page called Bar in the group Foo, create a li...

PmWiki.ChangeLog - score 13

...d <!--XMLHeader--> and <!--XMLFooter--> aliases to <!--HTMLHead...
...et [@{$Action}@] (reported by Bart). !!!Version 2.1.17 (2006-08...

By default, immediately after installation, this format will be set as the default format, in which your search results are displayed.

Installation

Initial part of the installation is standard:

  1. download the sortbyscore.phpΔ file
  2. put it in your PmWiki's cookbook/ directory
  3. in your local/config.php file add a line of:
    include_once("$FarmD/cookbook/sortbyscore.php");
  4. 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 include_once(...) line:

  $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.

Notes

  • 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

Release Notes

  • 20171110 - Update for PHP 5.5 and 7.2, requires PmWiki 2.2.56 or newer (Petko)
  • 2007-09-32 - v1.2
    • removed the requirement for Cookbook:Excerpts recipe, not showing excerpts now unless Cookbook:Excerpts included before (still can be disabled by setting $EnableSortByScoreExcerpts=0)
  • 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 ($SortByScoreTitleBonus)
    • 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

Comments

By the way; I'm very interested in the reception of my recipes. Therefore, please feel encouraged to write a word or two where/how you use this recipe, if you fancy :). But only if you have time - no force intended here. Greetz, M.

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

Fixed in new version, now works with pmwiki-2.2.0-beta63 (although not necessarily with older betas!). Thanks! MateuszCzaplinski September 26, 2007, at 01:40 PM

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

Fixed in new version. Thanks! MateuszCzaplinski September 26, 2007, at 01:40 PM

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?

Implemented in new version. Thanks for the suggestion! MateuszCzaplinski September 26, 2007, at 01:40 PM

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
I added this as an official option in Cookbook:Excerpts v1.2. Thanks for reporting! MateuszCzaplinski

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[1] ) ) );

to

 trim( $par[1] ) );

and also in line 101 fragment:

 $page

to

 htmlspecialchars($page)

That should work, hopefully not much slower. Thanks for info!

MateuszCzaplinski April 01, 2007, at 04:56 PM

2007-04-02 Steven Driskell - The above code fixed my problem. Nice work!

See Also

Contributors

User notes +1: If you use, used or reviewed this recipe, you can add your name. These statistics appear in the Cookbook listings and will help newcomers browsing through the wiki.