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

Change subject: Build suggestions from titles and redirects.
......................................................................


Build suggestions from titles and redirects.

We don't use redirects if redirects aren't enabled for the search.

Bug: 54228

Change-Id: I5883efd7df63d739fb0cad8aef884425138cf57e
---
M CirrusSearchSearcher.php
1 file changed, 41 insertions(+), 44 deletions(-)

Approvals:
  Chad: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/CirrusSearchSearcher.php b/CirrusSearchSearcher.php
index 0ffc0eb..87bf29e 100644
--- a/CirrusSearchSearcher.php
+++ b/CirrusSearchSearcher.php
@@ -19,8 +19,8 @@
  * http://www.gnu.org/copyleft/gpl.html
  */
 class CirrusSearchSearcher {
-       const PHRASE_TITLE = 'phrase_title';
-       const PHRASE_TEXT = 'phrase_text';
+       const SUGGESTION_NAME_TITLE = 'title';
+       const SUGGESTION_NAME_REDIRECT = 'redirect';
        const SUGGESTION_HIGHLIGHT_PRE = '<em>';
        const SUGGESTION_HIGHLIGHT_POST = '</em>';
        const HIGHLIGHT_PRE = '<span class="searchmatch">';
@@ -110,8 +110,6 @@
        public function searchText( $term, $showRedirects ) {
                global $wgCirrusSearchPhraseRescoreBoost;
                global $wgCirrusSearchPhraseRescoreWindowSize;
-               global $wgCirrusSearchPhraseSuggestMaxErrors;
-               global $wgCirrusSearchPhraseSuggestConfidence;
                wfDebugLog( 'CirrusSearch', "Searching:  $term" );
 
                // Transform Mediawiki specific syntax to filters and extra 
(pre-escaped) query string
@@ -164,43 +162,11 @@
 
                        $this->suggest = array(
                                'text' => $term,
-                               self::PHRASE_TITLE => array(
-                                       'phrase' => array(
-                                               'field' => 'title.suggest',
-                                               'size' => 1,
-                                               'max_errors' => 
$wgCirrusSearchPhraseSuggestMaxErrors,
-                                               'confidence' => 
$wgCirrusSearchPhraseSuggestConfidence,
-                                               'direct_generator' => array(
-                                                       array(
-                                                               'field' => 
'title.suggest',
-                                                               'suggest_mode' 
=> 'always', // Forces us to generate lots of phrases to try.
-                                                       ),
-                                               ),
-                                               'highlight' => array(
-                                                       'pre_tag' => 
self::SUGGESTION_HIGHLIGHT_PRE,
-                                                       'post_tag' => 
self::SUGGESTION_HIGHLIGHT_POST,
-                                               ),
-                                       )
-                               ),
-                               self::PHRASE_TEXT => array(
-                                       'phrase' => array(
-                                               'field' => 'text.suggest',
-                                               'size' => 1,
-                                               'max_errors' => 
$wgCirrusSearchPhraseSuggestMaxErrors,
-                                               'confidence' => 
$wgCirrusSearchPhraseSuggestConfidence,
-                                               'direct_generator' => array(
-                                                       array(
-                                                               'field' => 
'text.suggest',
-                                                               'suggest_mode' 
=> 'always', // Forces us to generate lots of phrases to try.
-                                                       ),
-                                               ),
-                                               'highlight' => array(
-                                                       'pre_tag' => 
self::SUGGESTION_HIGHLIGHT_PRE,
-                                                       'post_tag' => 
self::SUGGESTION_HIGHLIGHT_POST,
-                                               ),
-                                       )
-                               )
+                               self::SUGGESTION_NAME_TITLE => 
$this->buildSuggestConfig( 'title.suggest' ),
                        );
+                       if ( $showRedirects ) {
+                               $this->suggest[ self::SUGGESTION_NAME_REDIRECT 
] = $this->buildSuggestConfig( 'redirect.title.suggest' );
+                       }
                }
                $this->description = "full text search for '$originalTerm'";
                return $this->search();
@@ -340,6 +306,34 @@
                $query->setPhraseSlop( $wgCirrusSearchPhraseSlop );
                $query->setDefaultOperator( 'AND' );
                return $query;
+       }
+
+       /**
+        * Build suggest config for $field.
+        * @var $field string field to suggest against
+        * @return array of Elastica configuration
+        */
+       private function buildSuggestConfig( $field ) {
+               global $wgCirrusSearchPhraseSuggestMaxErrors;
+               global $wgCirrusSearchPhraseSuggestConfidence;
+               return array(
+                       'phrase' => array(
+                               'field' => $field,
+                               'size' => 1,
+                               'max_errors' => 
$wgCirrusSearchPhraseSuggestMaxErrors,
+                               'confidence' => 
$wgCirrusSearchPhraseSuggestConfidence,
+                               'direct_generator' => array(
+                                       array(
+                                               'field' => $field,
+                                               'suggest_mode' => 'always', // 
Forces us to generate lots of phrases to try.
+                                       ),
+                               ),
+                               'highlight' => array(
+                                       'pre_tag' => 
self::SUGGESTION_HIGHLIGHT_PRE,
+                                       'post_tag' => 
self::SUGGESTION_HIGHLIGHT_POST,
+                               ),
+                       ),
+               );
        }
 
        /**
@@ -534,15 +528,18 @@
        private function findSuggestion() {
                // TODO some kind of weighting?
                $suggest = $this->result->getResponse()->getData();
-               if ( !array_key_exists( 'suggest', $suggest ) ) {
+               if ( !isset( $suggest[ 'suggest' ] ) ) {
                        return null;
                }
                $suggest = $suggest[ 'suggest' ];
-               foreach ( $suggest[ CirrusSearchSearcher::PHRASE_TITLE ][ 0 ][ 
'options' ] as $option ) {
+               foreach ( $suggest[ CirrusSearchSearcher::SUGGESTION_NAME_TITLE 
][ 0 ][ 'options' ] as $option ) {
                        return $option;
                }
-               foreach ( $suggest[ CirrusSearchSearcher::PHRASE_TEXT ][ 0 ][ 
'options' ] as $option ) {
-                       return $option;
+               // If the user doesn't search against redirects we don't check 
them for suggestions so the result might not be there.
+               if ( isset( $suggest[ 
CirrusSearchSearcher::SUGGESTION_NAME_REDIRECT ] ) ) {
+                       foreach ( $suggest[ 
CirrusSearchSearcher::SUGGESTION_NAME_REDIRECT ][ 0 ][ 'options' ] as $option ) 
{
+                               return $option;
+                       }
                }
                return null;
        }

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I5883efd7df63d739fb0cad8aef884425138cf57e
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/CirrusSearch
Gerrit-Branch: master
Gerrit-Owner: Manybubbles <[email protected]>
Gerrit-Reviewer: Chad <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to