DCausse has uploaded a new change for review. https://gerrit.wikimedia.org/r/270260
Change subject: Completion suggester: add support for the offset param ...................................................................... Completion suggester: add support for the offset param The hack is to fetch offset+limit. Added a hard limit to prevent scrolling to far. Change-Id: Ia6f5abeb3b623736fcc07df498ddc1cf2691e445 --- M CirrusSearch.php M includes/CirrusSearch.php M includes/CompletionSuggester.php 3 files changed, 51 insertions(+), 9 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/CirrusSearch refs/changes/60/270260/1 diff --git a/CirrusSearch.php b/CirrusSearch.php index e289e78..319ab04 100644 --- a/CirrusSearch.php +++ b/CirrusSearch.php @@ -765,6 +765,13 @@ $wgCirrusSearchUseCompletionSuggester = false; /** + * Maximum number of results to ask from the elasticsearch completion + * api, note that this value will be multiplied by fetch_limit_factor + * set in Completion profiles (default to 2) + */ +$wgCirrusSearchCompletionSuggesterHardLimit = 50; + +/** * Try to recycle the completion suggester, if the wiki is small * it's certainly better to not re-create the index from scratch * since index creation is costly. Recycling the index will prevent diff --git a/includes/CirrusSearch.php b/includes/CirrusSearch.php index ae7d50c..0003250 100644 --- a/includes/CirrusSearch.php +++ b/includes/CirrusSearch.php @@ -394,7 +394,8 @@ // offset is omitted, searchSuggestion does not support // scrolling results $suggester = new CompletionSuggester( $this->connection, $this->limit, - $config, $this->namespaces, null, $this->indexBaseName ); + $this->offset, $config, $this->namespaces, null, + $this->indexBaseName ); $response = $suggester->suggest( $search, $variants ); if ( $response->isOK() ) { diff --git a/includes/CompletionSuggester.php b/includes/CompletionSuggester.php index d256dac..8828da7 100644 --- a/includes/CompletionSuggester.php +++ b/includes/CompletionSuggester.php @@ -106,6 +106,11 @@ private $limit; /** + * @var integer offset + */ + private $offset; + + /** * @var string index base name to use */ private $indexBaseName; @@ -136,7 +141,7 @@ * @param string|boolean $index Base name for index to search from, defaults to wfWikiId() * @throws \ConfigException */ - public function __construct( Connection $conn, $limit, SearchConfig $config = null, array $namespaces = null, + public function __construct( Connection $conn, $limit, $offset = 0, SearchConfig $config = null, array $namespaces = null, User $user = null, $index = false ) { if ( is_null( $config ) ) { @@ -148,6 +153,7 @@ parent::__construct( $conn, $user, $config->get( 'CirrusSearchSlowSearch' ) ); $this->config = $config; $this->limit = $limit; + $this->offset = $offset; $this->indexBaseName = $index ?: $config->getWikiId(); $this->searchContext = new SearchContext( $this->config, $namespaces ); } @@ -189,8 +195,7 @@ try { $result = $index->request( "_suggest", Request::POST, $suggest, $queryOptions ); if( $result->isOk() ) { - $result = $searcher->postProcessSuggest( $result, - $profiles, $this->limit ); + $result = $searcher->postProcessSuggest( $result, $profiles ); return $searcher->success( $result ); } return $result; @@ -293,7 +298,7 @@ 'text' => $query, 'completion' => array( 'field' => $field, - 'size' => $this->limit * $config['fetch_limit_factor'] + 'size' => ($this->limit + $this->offset) * $config['fetch_limit_factor'] ) ); if ( isset( $config['fuzzy'] ) ) { @@ -383,11 +388,12 @@ * @param int $limit Maximum suggestions to return, -1 for unlimited * @return SearchSuggestionSet a set of Suggestions */ - protected function postProcessSuggest( \Elastica\Response $response, $profiles, $limit = -1 ) { + protected function postProcessSuggest( \Elastica\Response $response, $profiles ) { $this->logContext['elasticTookMs'] = intval( $response->getQueryTime() * 1000 ); $data = $response->getData(); unset( $data['_shards'] ); + $limit = $this->getHardLimit(); $suggestions = array(); foreach ( $data as $name => $results ) { $discount = $profiles[$name]['discount']; @@ -423,12 +429,14 @@ $this->logContext['hitsTotal'] = count( $suggestions ); - if ( $limit > 0 ) { - $suggestions = array_slice( $suggestions, 0, $limit, true ); + if ( $this->offset < $limit ) { + $suggestions = array_slice( $suggestions, $this->offset, $limit - $this->offset, true ); + } else { + $suggestions = array(); } $this->logContext['hitsReturned'] = count( $suggestions ); - $this->logContext['hitsOffset'] = 0; + $this->logContext['hitsOffset'] = $this->offset; // we must fetch redirect data for redirect suggestions $missingText = array(); @@ -501,4 +509,30 @@ public function setLimit( $limit ) { $this->limit = $limit; } + + /** + * Set the offset + * @param int $offset + */ + public function setOffset( $offset ) { + $this->offset = $offset; + } + + /** + * Get the hard limit + * The completion api does not supports offset we have to add a hack + * here to work around this limitation. + * To avoid ridiculously large queris we set also a hard limit. + * Note that this limit will be changed by fetch_limit_factor set to 2 or 1.5 + * depending on the profile. + * @return int the number of results to fetch from elastic + */ + private function getHardLimit() { + $limit = $this->limit + $this->offset; + $hardLimit = $this->config->get( 'CirrusSearchCompletionSuggesterHardLimit' ); + if ( $limit > $hardLimit ) { + return $hardLimit; + } + return $limit; + } } -- To view, visit https://gerrit.wikimedia.org/r/270260 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ia6f5abeb3b623736fcc07df498ddc1cf2691e445 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/CirrusSearch Gerrit-Branch: master Gerrit-Owner: DCausse <dcau...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits