jenkins-bot has submitted this change and it was merged.

Change subject: Move query rewriting into search backend
......................................................................


Move query rewriting into search backend

This is the CirrusSearch half of I0a8f75759. In that patch rewriting
was adjusted so it can be implemented directly by the search engine.

This will allow us to make more indepth changes to the way the second
tquery is run, rather than only having control over the text of the
search.

Bug: T106888
Change-Id: Iebce6a59931ae0c295e13ccc12a22a1ae2081777
---
M includes/CirrusSearch.php
M includes/Search/ResultSet.php
M tests/browser/features/step_definitions/search_steps.rb
3 files changed, 71 insertions(+), 4 deletions(-)

Approvals:
  Cindy-the-browser-test-bot: Looks good to me, but someone else must approve
  DCausse: Looks good to me, approved
  EBernhardson: Looks good to me, but someone else must approve
  jenkins-bot: Verified



diff --git a/includes/CirrusSearch.php b/includes/CirrusSearch.php
index 2bd58b9..028b234 100644
--- a/includes/CirrusSearch.php
+++ b/includes/CirrusSearch.php
@@ -3,6 +3,7 @@
 use CirrusSearch\InterwikiSearcher;
 use CirrusSearch\Search\FullTextResultsType;
 use CirrusSearch\Searcher;
+use CirrusSearch\Search\ResultSet;
 
 /**
  * SearchEngine implementation for CirrusSearch.  Delegates to
@@ -68,9 +69,46 @@
        /**
         * Overridden to delegate prefix searching to Searcher.
         * @param string $term text to search
-        * @return Search\ResultSet|null|Status results, no results, or error 
respectively
+        * @return ResultSet|null|Status results, no results, or error 
respectively
         */
        public function searchText( $term ) {
+               $matches = $this->searchTextReal( $term );
+               if (!$matches instanceof ResultSet) {
+                       return $matches;
+               }
+               if ( $this->isFeatureEnabled( 'rewrite' ) && 
$matches->isQueryRewriteAllowed() ) {
+                       $matches = $this->searchTextSecondTry( $matches );
+               }
+               return $matches;
+       }
+
+       private function isFeatureEnabled( $feature ) {
+               return isset( $this->features[$feature] ) && 
$this->features[$feature];
+       }
+
+       private function searchTextSecondTry( ResultSet $zeroResult ) {
+               if ( $zeroResult->hasSuggestion() ) {
+                       $rewritten = $zeroResult->getSuggestionQuery();
+                       $rewrittenSnippet = $zeroResult->getSuggestionSnippet();
+                       $this->showSuggestion = false;
+               } else {
+                       // Don't have any other options yet.
+                       return $zeroResult;
+               }
+
+               $rewrittenResult = $this->searchTextReal( $rewritten );
+               if (
+                       $rewrittenResult instanceof ResultSet
+                       && $rewrittenResult->numRows() > 0
+               ) {
+                       $rewrittenResult->setRewrittenQuery( $rewritten, 
$rewrittenSnippet );
+                       return $rewrittenResult;
+               } else {
+                       return $zeroResult;
+               }
+       }
+
+       private function searchTextReal( $term ) {
                global $wgCirrusSearchInterwikiSources;
 
                // Convert the unicode character 'idiographic whitespace' into 
standard
diff --git a/includes/Search/ResultSet.php b/includes/Search/ResultSet.php
index 40dcef2..9d98430 100644
--- a/includes/Search/ResultSet.php
+++ b/includes/Search/ResultSet.php
@@ -26,7 +26,8 @@
 class ResultSet extends SearchResultSet {
        private $result, $hits, $totalHits, $suggestionQuery, 
$suggestionSnippet;
        private $searchContainedSyntax;
-       private $interwikiPrefix, $interwikiResults;
+       private $interwikiPrefix,$interwikiResults;
+       private $rewrittenQuery;
 
        public function __construct( $suggestPrefixes, $suggestSuffixes, $res, 
$searchContainedSyntax, $interwiki = '' ) {
                $this->result = $res;
@@ -52,6 +53,15 @@
                                $this->suggestionSnippet = 
$this->suggestionSnippet . htmlspecialchars( $suggestSuffix );
                        }
                }
+       }
+
+       /**
+        * @return bool True when rewriting this query is allowed
+        */
+       public function isQueryRewriteAllowed() {
+               return $this->numRows() === 0 &&
+                       count( $this->interwikiResults ) === 0 &&
+                       !$this->searchContainedSyntax();
        }
 
        private function findSuggestion() {
@@ -164,4 +174,21 @@
        public function searchContainedSyntax() {
                return $this->searchContainedSyntax;
        }
+
+       public function setRewrittenQuery($newQuery, $newQuerySnippet=null) {
+               $this->rewrittenQuery = $newQuery;
+               $this->rewrittenQuerySnippet = $newQuerySnippet ?: 
htmlspecialchars( $newQuery );
+       }
+
+       public function hasRewrittenQuery() {
+               return $this->rewrittenQuery !== null;
+       }
+
+       public function getQueryAfterRewrite() {
+               return $this->rewrittenQuery;
+       }
+
+       public function getQueryAfterRewriteSnippet() {
+               return $this->rewrittenQuerySnippet;
+       }
 }
diff --git a/tests/browser/features/step_definitions/search_steps.rb 
b/tests/browser/features/step_definitions/search_steps.rb
index 08feb47..5f944d5 100644
--- a/tests/browser/features/step_definitions/search_steps.rb
+++ b/tests/browser/features/step_definitions/search_steps.rb
@@ -25,13 +25,15 @@
   @didyoumean_options[varname] = value
 end
 # rubocop:disable LineLength
-When(/^I api search( with disabled incoming link weighting)?(?: with offset 
(\d+))?(?: in the (.*) language)?(?: in namespaces? (\d+(?: \d+)*))? for 
(.*)$/) do |incoming_links, offset, lang, namespaces, search|
+# rubocop:disable ParameterLists
+When(/^I api search( with rewrites enabled)?( with disabled incoming link 
weighting)?(?: with offset (\d+))?(?: in the (.*) language)?(?: in namespaces? 
(\d+(?: \d+)*))? for (.*)$/) do |enable_rewrites, incoming_links, offset, lang, 
namespaces, search|
   begin
     options = {
       sroffset: offset,
       srnamespace: (namespaces || "0").split(/ /),
       uselang: lang,
-      cirrusBoostLinks: incoming_links ? "no" : "yes"
+      cirrusBoostLinks: incoming_links ? "no" : "yes",
+      enablerewrites: enable_rewrites ? 1 : 0
     }
     options = options.merge(@didyoumean_options) if defined?@didyoumean_options
 

-- 
To view, visit https://gerrit.wikimedia.org/r/227578
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Iebce6a59931ae0c295e13ccc12a22a1ae2081777
Gerrit-PatchSet: 11
Gerrit-Project: mediawiki/extensions/CirrusSearch
Gerrit-Branch: master
Gerrit-Owner: EBernhardson <ebernhard...@wikimedia.org>
Gerrit-Reviewer: Chad <ch...@wikimedia.org>
Gerrit-Reviewer: Cindy-the-browser-test-bot <bernhardsone...@gmail.com>
Gerrit-Reviewer: DCausse <dcau...@wikimedia.org>
Gerrit-Reviewer: EBernhardson <ebernhard...@wikimedia.org>
Gerrit-Reviewer: Manybubbles <never...@wikimedia.org>
Gerrit-Reviewer: MaxSem <maxsem.w...@gmail.com>
Gerrit-Reviewer: Smalyshev <smalys...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to