Expose sort orders from search engine in ApiQuerySearch
authorErik Bernhardson <ebernhardson@wikimedia.org>
Sat, 19 May 2018 13:42:54 +0000 (06:42 -0700)
committerErik Bernhardson <ebernhardson@wikimedia.org>
Wed, 13 Jun 2018 15:57:16 +0000 (08:57 -0700)
Selectable sort orders were added to search some time ago, but never
appear to have been used.  Expose them in ApiQuerySearch.

There is one large problem here, but it's not easily solvable.
Specifically supported sort orders vary per search engine, but the
available api parameters are calculated with the default search
engine. This is already a problem with search profiles so it's
nothing new, just continuing the existing problem. Much like
search profiles we hide the functionality when multiple engines
are available.

Bug: T195071
Change-Id: I187a530bc973c08386d2c2126e02a85b952d9022

includes/api/ApiQuerySearch.php
includes/api/i18n/en.json
includes/api/i18n/qqq.json

index 7d46a5f..e5e3f36 100644 (file)
@@ -60,6 +60,9 @@ class ApiQuerySearch extends ApiQueryGeneratorBase {
 
                // Create search engine instance and set options
                $search = $this->buildSearchEngine( $params );
+               if ( isset( $params['sort'] ) ) {
+                       $search->setSort( $params['sort'] );
+               }
                $search->setFeatureData( 'rewrite', (bool)$params['enablerewrites'] );
                $search->setFeatureData( 'interwiki', (bool)$interwiki );
 
@@ -391,6 +394,20 @@ class ApiQuerySearch extends ApiQueryGeneratorBase {
                        'enablerewrites' => false,
                ];
 
+               // If we have more than one engine the list of available sorts is
+               // difficult to represent. For now don't expose it.
+               $alternatives = MediaWiki\MediaWikiServices::getInstance()
+                       ->getSearchEngineConfig()
+                       ->getSearchTypes();
+               if ( count( $alternatives ) == 1 ) {
+                       $this->allowedParams['sort'] = [
+                               ApiBase::PARAM_DFLT => 'relevance',
+                               ApiBase::PARAM_TYPE => MediaWiki\MediaWikiServices::getInstance()
+                                       ->newSearchEngine()
+                                       ->getValidSorts(),
+                       ];
+               }
+
                return $this->allowedParams;
        }
 
index 6838e54..fa388d1 100644 (file)
        "apihelp-query+search-param-interwiki": "Include interwiki results in the search, if available.",
        "apihelp-query+search-param-backend": "Which search backend to use, if not the default.",
        "apihelp-query+search-param-enablerewrites": "Enable internal query rewriting. Some search backends can rewrite the query into another which is thought to provide better results, for instance by correcting spelling errors.",
+       "apihelp-query+search-param-sort": "Set the sort order of returned results.",
        "apihelp-query+search-example-simple": "Search for <kbd>meaning</kbd>.",
        "apihelp-query+search-example-text": "Search texts for <kbd>meaning</kbd>.",
        "apihelp-query+search-example-generator": "Get page info about the pages returned for a search for <kbd>meaning</kbd>.",
index 594bf8e..5f9cc70 100644 (file)
        "apihelp-query+search-param-interwiki": "{{doc-apihelp-param|query+search|interwiki}}",
        "apihelp-query+search-param-backend": "{{doc-apihelp-param|query+search|backend}}",
        "apihelp-query+search-param-enablerewrites": "{{doc-apihelp-param|query+search|enablerewrites}}",
+       "apihelp-query+search-param-sort": "{{doc-apihelp-param|query+search|sort}}",
        "apihelp-query+search-example-simple": "{{doc-apihelp-example|query+search}}",
        "apihelp-query+search-example-text": "{{doc-apihelp-example|query+search}}",
        "apihelp-query+search-example-generator": "{{doc-apihelp-example|query+search}}",