Jump to content

Enable search box suggestions-as-you-type that match words or phrases anywhere in the page title

Enable search box suggestions-as-you-type that match words or phrases anywhere in the page title

I've slightly modified the extension to enable suggestions-as-you-type for the search box that match a word or phrase anywhere in the page title, not just at the beginning. For instance, typing "baby strol" should bring up pages with titles "Plik-o-matic baby stroller 2003", "Swift Baby Stroller", "Zippy one hand fold baby stroller by inglesina - new 2005", etc. as seen in this example. Use the patch below and add the following lines to LocalSettings.php:

$wgEnableMWSuggest = true;
$wgEnableSphinxInfixSearch = true;

Note that this overrides $wgEnableSphinxPrefixSearch = true; (search suggestions matching the beginning of the page title only).

--- a/SphinxSearch/SphinxSearch.php
+++ b/SphinxSearch/SphinxSearch.php
@@ -103,10 +103,17 @@
 # $wgEnableMWSuggest needs to be set to true as well
 $wgEnableSphinxPrefixSearch = false;
 
+# Same as $wgEnableSphinxPrefixSearch except that search suggestions include
+# matches against words in the middle of the page title. Takes precedence 
+# over wgEnableSphinxPrefixSearch if both are true.
+$wgEnableSphinxInfixSearch = false;
+
 function efSphinxSearchPrefixSetup() {
-       global $wgHooks, $wgEnableSphinxPrefixSearch;
+       global $wgHooks, $wgEnableSphinxPrefixSearch, $wgEnableSphinxInfixSearch;
 
-       if ( $wgEnableSphinxPrefixSearch ) {
+       if ( $wgEnableSphinxInfixSearch ) {
+               $wgHooks[ 'PrefixSearchBackend' ][ ] = 'SphinxMWSearch::infixSearch';
+       } elseif ( $wgEnableSphinxPrefixSearch ) {
                $wgHooks[ 'PrefixSearchBackend' ][ ] = 'SphinxMWSearch::prefixSearch';
        }
 }
--- a/SphinxSearch/SphinxMWSearch.php
+++ b/SphinxSearch/SphinxMWSearch.php
@@ -56,6 +56,23 @@
        }
 
        /**
+        *  PrefixSearchBackend override for OpenSearch results
+        */
+       static function infixSearch( $namespaces, $term, $limit, &$results ) {
+               $search_engine = new SphinxMWSearch( wfGetDB( DB_SLAVE ) );
+               $search_engine->namespaces = $namespaces;
+               $search_engine->setLimitOffset( $limit, 0 );
+               $result_set = $search_engine->searchText( '@page_title: ' . $term . '*' );
+               $results = array();
+               if ( $result_set ) {
+                       while ( $res = $result_set->next() ) {
+                               $results[ ] = $res->getTitle()->getPrefixedText();
+                       }
+               }
+               return false;
+       }
+
+       /**
         * Perform a full text search query and return a result set.
         *
         * @param string $term - Raw search term
198.176.189.20114:29, 19 June 2012