Topic on Extension talk:SphinxSearch

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

9
198.176.189.201 (talkcontribs)

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

This works for me using MediaWiki 1.19 and SphinxSearch 0.8.5 (SVN rev 115479). The above also assumes that sphinx is already configured for infix searches using star syntax, which is the case if you followed the instructions on the install page. In other words, this requires sphinx.conf to include min_infix_len = n (where n is a positive integer) and enable_star = 1.

Krenair (talkcontribs)
MWJames (talkcontribs)

The [1] commit implements a prefix wildcard search.

- Enable wildcard prefix search suggestions ($wgSphinxPrefixSearchAdvanced = false;)

- Extend prefix search namespace beyond NS_MAIN ($wgSphinxPrefixSearchNamespaces = false;)

- Extend prefix result display limit ($wgSphinxPrefixSearchLimit = 15;)

[1] https://gerrit.wikimedia.org/r/#/c/15859/

SmartK (talkcontribs)

Hallo MWJames, I think this is a great idea. But on my 1.19 MW it just won't work. Sphinx itself is running smoothly but I cannot find out why the "InfixSearch" will NOT work.
I checked here: sphinx.conf:

min_infix_len = 1
enable_star = 1


And also in LocalSettings.php

$wgSearchType = 'SphinxMWSearch';
require_once( "$IP/extensions/SphinxSearch/SphinxSearch.php" );
$wgSphinxSearch_port = 9312;
$wgEnableSphinxInfixSearch = true;
$wgSphinxPrefixSearchAdvanced = true;
$wgSphinxPrefixSearchNamespaces = true;
$wgSphinxPrefixSearchLimit = 15;

I got the "updated" version from here: https://gerrit.wikimedia.org/r/#/c/15859/
Your help would be highly appreciated ;-) Thank you!!!

SmartK (talkcontribs)

Ohhh, my fault. It wasn't MWJames how thought of this. I am sorry. So I just implemented the code from the anonymous user "198.176.189.201" ;-) Now the "InfixSearch" is working like a charm. Couldn't we implement this in standard git aswell? I think this would be really good!

SmartK (talkcontribs)

Just found a "bug" with the now working "InfixSearch". As soon as I turn on "$wgEnableSphinxInfixSearch = true;" the auto-complete will NOT work IF there is a character like "ä, ö, ü" in the search-term. If the InfixSearch is OFF the auto-complete works flawless.

Quick73 (talkcontribs)
Svemir Brkic (talkcontribs)
Svemir Brkic (talkcontribs)
Reply to "Enable search box suggestions-as-you-type that match words or phrases anywhere in the page title"