Beispiel #1
0
 private function handleResultFromHook($srchres, $namespaces, $search, $limit)
 {
     $rescorer = new SearchExactMatchRescorer();
     return $rescorer->rescore($search, $namespaces, $srchres, $limit);
 }
Beispiel #2
0
 /**
  * Process completion search results.
  * Resolves the titles and rescores.
  * @param SearchSuggestionSet $suggestions
  * @return SearchSuggestionSet
  */
 protected function processCompletionResults($search, SearchSuggestionSet $suggestions)
 {
     $search = trim($search);
     // preload the titles with LinkBatch
     $titles = $suggestions->map(function (SearchSuggestion $sugg) {
         return $sugg->getSuggestedTitle();
     });
     $lb = new LinkBatch($titles);
     $lb->setCaller(__METHOD__);
     $lb->execute();
     $results = $suggestions->map(function (SearchSuggestion $sugg) {
         return $sugg->getSuggestedTitle()->getPrefixedText();
     });
     if ($this->offset === 0) {
         // Rescore results with an exact title match
         // NOTE: in some cases like cross-namespace redirects
         // (frequently used as shortcuts e.g. WP:WP on huwiki) some
         // backends like Cirrus will return no results. We should still
         // try an exact title match to workaround this limitation
         $rescorer = new SearchExactMatchRescorer();
         $rescoredResults = $rescorer->rescore($search, $this->namespaces, $results, $this->limit);
     } else {
         // No need to rescore if offset is not 0
         // The exact match must have been returned at position 0
         // if it existed.
         $rescoredResults = $results;
     }
     if (count($rescoredResults) > 0) {
         $found = array_search($rescoredResults[0], $results);
         if ($found === false) {
             // If the first result is not in the previous array it
             // means that we found a new exact match
             $exactMatch = SearchSuggestion::fromTitle(0, Title::newFromText($rescoredResults[0]));
             $suggestions->prepend($exactMatch);
             $suggestions->shrink($this->limit);
         } else {
             // if the first result is not the same we need to rescore
             if ($found > 0) {
                 $suggestions->rescore($found);
             }
         }
     }
     return $suggestions;
 }
Beispiel #3
0
 private function handleResultFromHook($srchres, $namespaces, $search, $limit, $offset)
 {
     if ($offset === 0) {
         // Only perform exact db match if offset === 0
         // This is still far from perfect but at least we avoid returning the
         // same title afain and again when the user is scrolling with a query
         // that matches a title in the db.
         $rescorer = new SearchExactMatchRescorer();
         $srchres = $rescorer->rescore($search, $namespaces, $srchres, $limit);
     }
     return $srchres;
 }
Beispiel #4
0
 /**
  * Process completion search results.
  * Resolves the titles and rescores.
  * @param SearchSuggestionSet $suggestions
  * @return SearchSuggestionSet
  */
 protected function processCompletionResults($search, SearchSuggestionSet $suggestions)
 {
     if ($suggestions->getSize() == 0) {
         // If we don't have anything, don't bother
         return $suggestions;
     }
     $search = trim($search);
     // preload the titles with LinkBatch
     $titles = $suggestions->map(function (SearchSuggestion $sugg) {
         return $sugg->getSuggestedTitle();
     });
     $lb = new LinkBatch($titles);
     $lb->setCaller(__METHOD__);
     $lb->execute();
     $results = $suggestions->map(function (SearchSuggestion $sugg) {
         return $sugg->getSuggestedTitle()->getPrefixedText();
     });
     // Rescore results with an exact title match
     $rescorer = new SearchExactMatchRescorer();
     $rescoredResults = $rescorer->rescore($search, $this->namespaces, $results, $this->limit);
     if (count($rescoredResults) > 0) {
         $found = array_search($rescoredResults[0], $results);
         if ($found === false) {
             // If the first result is not in the previous array it
             // means that we found a new exact match
             $exactMatch = SearchSuggestion::fromTitle(0, Title::newFromText($rescoredResults[0]));
             $suggestions->prepend($exactMatch);
             $suggestions->shrink($this->limit);
         } else {
             // if the first result is not the same we need to rescore
             if ($found > 0) {
                 $suggestions->rescore($found);
             }
         }
     }
     return $suggestions;
 }
 /**
  * This implementation will run the completion suggester if it's enabled and if the
  * query is for NS_MAIN. Fallback to SearchEngine default implemention otherwise.
  *
  * @param string $search the user query
  * @return SearchSuggestionSet the suggestions
  */
 public function searchSuggestions($search)
 {
     $config = ConfigFactory::getDefaultInstance()->makeConfig('CirrusSearch');
     $useCompletionSuggester = $config->getElement('CirrusSearchUseCompletionSuggester');
     $context = RequestContext::getMain();
     $request = $context->getRequest();
     // Allow experimentation with query parameters
     if ($request && $request->getVal('cirrusUseCompletionSuggester') === 'yes') {
         $useCompletionSuggester = true;
     }
     if (!$useCompletionSuggester) {
         // Completion suggester is not enabled, fallback to
         // default implementation
         return $this->searchSuggestionsPrefixSearchFallback($search);
     }
     // We use Title to extract namespace from a Title string
     // We append a random letter behind just in case the search
     // string ends with ':'.
     $title = Title::newFromText($search . "A");
     if ($title->getNamespace() != NS_MAIN || count($this->namespaces) != 1 || reset($this->namespaces) != NS_MAIN) {
         // Fallback to prefix search if we are not on content namespace
         return $this->searchSuggestionsPrefixSearchFallback($search);
     }
     $user = $context->getUser();
     // offset is omitted, searchSuggestion does not support
     // scrolling results
     $suggester = new CompletionSuggester($this->connection, $this->limit, $config, $this->namespaces, $user, $this->indexBaseName);
     // Not really useful, mostly for testing purpose
     $variants = $request->getArray('cirrusCompletionSuggesterVariant');
     if (empty($variants)) {
         global $wgContLang;
         $variants = $wgContLang->autoConvertToAllVariants($search);
     } else {
         if (count($variants) > 3) {
             // We should not allow too many variants
             $variants = array_slice($variants, 0, 3);
         }
     }
     $response = $suggester->suggest($search, $variants);
     if ($response->isOK()) {
         // Errors will be logged, let's try the exact db match
         $suggestions = $response->getValue();
     } else {
         $suggestions = SearchSuggestionSet::emptySuggestionSet();
     }
     // preload the titles with LinkBatch
     $titles = $suggestions->map(function ($sugg) {
         return $sugg->getSuggestedTitle();
     });
     $lb = new LinkBatch($titles);
     $lb->setCaller(__METHOD__);
     $lb->execute();
     $results = $suggestions->map(function ($sugg) {
         return $sugg->getSuggestedTitle()->getPrefixedText();
     });
     // now we can trim
     $search = trim($search);
     // Rescore results with an exact title match
     $rescorer = new SearchExactMatchRescorer();
     $rescoredResults = $rescorer->rescore($search, $this->namespaces, $results, $this->limit);
     if (count($rescoredResults) > 0) {
         $found = array_search($rescoredResults[0], $results);
         if ($found === false) {
             // If the first result is not in the previous array it
             // means that we found a new exact match
             $exactMatch = SearchSuggestion::fromTitle(0, Title::newFromText($rescoredResults[0]));
             $suggestions->prepend($exactMatch);
             $suggestions->shrink($this->limit);
         } else {
             // if the first result is not the same we need to rescore
             if ($found > 0) {
                 $suggestions->rescore($found);
             }
         }
     }
     return $suggestions;
 }