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
.