Thread:Extension talk:SphinxSearch/Enable search box suggestions-as-you-type that match words or phrases anywhere in the page title: Difference between revisions
Appearance
Content deleted Content added
New thread: Enable search box suggestions-as-you-type that match words or phrases anywhere in the page title |
Added version numbers |
||
| Line 58: | Line 58: | ||
* @param string $term - Raw search term |
* @param string $term - Raw search term |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
This works for me using MediaWiki 1.19 and SphinxSearch 0.8.5 (SVN rev 115479). |
|||
Revision as of 14:35, 19 June 2012
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).