DCausse has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/398230 )

Change subject: [WIP] Fetch redirect namespace during prefix search
......................................................................

[WIP] Fetch redirect namespace during prefix search

Use the experimental highlighter to fetch redirect namesapces.

WIP: because Erik implemented a more generic solution that does
not depend on the exp highlighter.

Bug: T115756
Change-Id: I2f48f06216752386df342b05a96b9fa69d9a3bc4
---
M includes/CirrusSearch.php
M includes/CompletionSuggester.php
M includes/Search/ResultsType.php
3 files changed, 34 insertions(+), 15 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/CirrusSearch 
refs/changes/30/398230/1

diff --git a/includes/CirrusSearch.php b/includes/CirrusSearch.php
index b5c2725..36f539f 100644
--- a/includes/CirrusSearch.php
+++ b/includes/CirrusSearch.php
@@ -650,7 +650,9 @@
                                return SearchSuggestionSet::fromTitles( 
$status->getValue() );
                        }
                        $results = array_filter( array_map(
-                               [ FancyTitleResultsType::class, 
'chooseBestTitleOrRedirect' ],
+                               function( $result ) {
+                                       return 
FancyTitleResultsType::chooseBestTitleOrRedirect( $result, $this->namespaces );
+                               },
                                $status->getValue() ) );
                        return SearchSuggestionSet::fromTitles( $results );
                }
diff --git a/includes/CompletionSuggester.php b/includes/CompletionSuggester.php
index e553aca..98245d1 100644
--- a/includes/CompletionSuggester.php
+++ b/includes/CompletionSuggester.php
@@ -285,7 +285,8 @@
 
                foreach ( $prefixResults->getResults() as $res ) {
                        $pageId = $this->config->makePageId( $res->getId() );
-                       $title = 
FancyTitleResultsType::chooseBestTitleOrRedirect( 
$rType->transformOneElasticResult( $res ) );
+                       $title = 
FancyTitleResultsType::chooseBestTitleOrRedirect( 
$rType->transformOneElasticResult( $res ),
+                               
$this->prefixSearchRequestBuilder->getSearchContext()->getNamespaces() );
                        if ( $title === false ) {
                                continue;
                        }
diff --git a/includes/Search/ResultsType.php b/includes/Search/ResultsType.php
index 372ef36..318ef0a 100644
--- a/includes/Search/ResultsType.php
+++ b/includes/Search/ResultsType.php
@@ -118,6 +118,9 @@
        /** @var string */
        private $matchedAnalyzer;
 
+       /** @var  boolean */
+       private $useExperimentalHighlighter;
+
        /**
         * Build result type.   The matchedAnalyzer is required to detect if 
the match
         * was from the title or a redirect (and is kind of a leaky 
abstraction.)
@@ -125,7 +128,9 @@
         * @param string $matchedAnalyzer the analyzer used to match the title
         */
        public function __construct( $matchedAnalyzer ) {
+               global $wgCirrusSearchUseExperimentalHighlighter;
                $this->matchedAnalyzer = $matchedAnalyzer;
+               $this->useExperimentalHighlighter = 
$wgCirrusSearchUseExperimentalHighlighter;
        }
 
        /**
@@ -133,9 +138,8 @@
         * @return array|null
         */
        public function getHighlightingConfiguration( array $highlightSource ) {
-               global $wgCirrusSearchUseExperimentalHighlighter;
 
-               if ( $wgCirrusSearchUseExperimentalHighlighter ) {
+               if ( $this->useExperimentalHighlighter ) {
                        // This is much less esoteric then the plain 
highlighter based
                        // invocation but does the same thing.  The magic is 
that the none
                        // fragmenter still fragments on multi valued fields.
@@ -148,6 +152,9 @@
                                'type' => 'experimental',
                                'fragmenter' => 'none',
                                'order' => 'score',
+                               'options' => [
+                                       'fetch_fields' => [ 
'redirect.namespace' ]
+                               ]
                        ];
                } else {
                        // This is similar to the FullTextResults type but 
against the near_match and
@@ -189,6 +196,7 @@
        public function transformElasticsearchResult( SearchContext $context, 
\Elastica\ResultSet $resultSet ) {
                $results = [];
                foreach ( $resultSet->getResults() as $r ) {
+                       wfDebug( print_r( $r, true ) );
                        $results[] = $this->transformOneElasticResult( $r );
                }
                return $results;
@@ -197,15 +205,20 @@
        /**
         * Finds best title or redirect
         * @param array $match array returned by self::transformOneElasticResult
+        * @param array $namespaceFilter filter on these namespaces
         * @return \Title|false choose best
         */
-       public static function chooseBestTitleOrRedirect( array $match ) {
+       public static function chooseBestTitleOrRedirect( array $match, 
$namespaceFilter = [] ) {
                if ( isset( $match['titleMatch'] ) ) {
                        return $match['titleMatch'];
                } else {
-                       if ( isset( $match['redirectMatches'][0] ) ) {
-                               // TODO maybe dig around in the redirect 
matches and find the best one?
-                               return $match['redirectMatches'][0];
+                       if ( isset( $match['redirectMatches'] ) ) {
+                               foreach( $match['redirectMatches'] as $redir ) {
+                                       if ( empty( $namespaceFilter ) || 
in_array( $redir->getNamespace(), $namespaceFilter ) ) {
+                                               // TODO maybe dig around in the 
redirect matches and find the best one?
+                                               return $redir;
+                                       }
+                               }
                        }
                }
 
@@ -251,20 +264,23 @@
                                        
$highlights["redirect.title.{$this->matchedAnalyzer}_asciifolding"] );
                }
                if ( count( $redirectHighlights ) !== 0 ) {
-                       foreach ( $redirectHighlights as $redirectTitle ) {
+                       for ( $i = 0; $i < count( $redirectHighlights ); $i++ ) 
{
+                               // Without the experimental highlighter we 
don't really know the namespace
+                               // of the redirect.
+                               $namespace = $r->namespace;
+                               $redirectTitle = $redirectHighlights[$i];
+                               if ( $this->useExperimentalHighlighter ) {
+                                       // fetched fields are just after the 
highlight snippet
+                                       $namespace = $redirectHighlights[++$i];
+                               }
                                // The match was against a redirect so we 
should replace the $title with one that
                                // represents the redirect.
                                // The first step is to strip the actual 
highlighting from the title.
                                $redirectTitle = str_replace( [ 
Searcher::HIGHLIGHT_PRE, Searcher::HIGHLIGHT_POST ],
                                        '', $redirectTitle );
 
-                               // Instead of getting the redirect's real 
namespace we're going to just use the namespace
-                               // of the title.  This is not great but OK 
given that we can't find cross namespace
-                               // redirects properly any way.
-                               // TODO: ask the highlighter to return the 
namespace for this kind of matches
-                               // this would perhaps help to partially fix 
T115756
                                $redirectTitle =
-                                       TitleHelper::makeRedirectTitle( $r, 
$redirectTitle, $r->namespace );
+                                       TitleHelper::makeRedirectTitle( $r, 
$redirectTitle, $namespace );
                                $resultForTitle['redirectMatches'][] = 
$redirectTitle;
                        }
                }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2f48f06216752386df342b05a96b9fa69d9a3bc4
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/CirrusSearch
Gerrit-Branch: master
Gerrit-Owner: DCausse <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to