Smalyshev has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/384907 )
Change subject: Move resultsType to search context ...................................................................... Move resultsType to search context This allows fulltext query builders to override result type, which is relevant for wikidata when it wants to display different fields than a regular result type. Change-Id: Ic818924f83699b1bc333d03499808d390b75faf2 --- M includes/Search/SearchContext.php M includes/Searcher.php 2 files changed, 34 insertions(+), 17 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/CirrusSearch refs/changes/07/384907/1 diff --git a/includes/Search/SearchContext.php b/includes/Search/SearchContext.php index bde76d7..c767117 100644 --- a/includes/Search/SearchContext.php +++ b/includes/Search/SearchContext.php @@ -211,6 +211,11 @@ private $isDirty = false; /** + * @var ResultsType Type of the result for the context. + */ + private $resultsType; + + /** * @param SearchConfig $config * @param int[]|null $namespaces */ @@ -879,4 +884,22 @@ $this->isDirty = true; $this->fulltextQueryBuilderProfile = $profile; } + + /** + * @param ResultsType $resultsType results type to return + */ + public function setResultsType( $resultsType ) { + $this->resultsType = $resultsType; + } + + /** + * @return ResultsType $resultsType results type to return + */ + public function getResultsType() { + if ( !$this->resultsType === null ) { + return new FullTextResultsType( FullTextResultsType::HIGHLIGHT_ALL ); + } + return $this->resultsType; + } + } diff --git a/includes/Searcher.php b/includes/Searcher.php index 5d7bb4c..724441f 100644 --- a/includes/Searcher.php +++ b/includes/Searcher.php @@ -3,7 +3,6 @@ namespace CirrusSearch; use CirrusSearch\Query\SimpleKeywordFeature; -use CirrusSearch\Search\FullTextResultsType; use CirrusSearch\Search\TitleResultsType; use CirrusSearch\Search\ResultsType; use CirrusSearch\Search\RescoreBuilder; @@ -90,10 +89,6 @@ private $language; /** - * @var ResultsType|null type of results. null defaults to FullTextResultsType - */ - protected $resultsType; - /** * @var string sort type */ private $sort = 'relevance'; @@ -164,7 +159,7 @@ * @param ResultsType $resultsType results type to return */ public function setResultsType( $resultsType ) { - $this->resultsType = $resultsType; + $this->searchContext->setResultsType( $resultsType ); } /** @@ -552,13 +547,11 @@ * @return \Elastica\Search */ protected function buildSearch() { - if ( $this->resultsType === null ) { - $this->resultsType = new FullTextResultsType( FullTextResultsType::HIGHLIGHT_ALL ); - } + $resultsType = $this->searchContext->getResultsType(); $query = new \Elastica\Query(); - $query->setSource( $this->resultsType->getSourceFiltering() ); - $query->setStoredFields( $this->resultsType->getStoredFields() ); + $query->setSource( $resultsType->getSourceFiltering() ); + $query->setStoredFields( $resultsType->getStoredFields() ); $extraIndexes = []; $namespaces = $this->searchContext->getNamespaces(); @@ -581,7 +574,7 @@ $this->installBoosts(); $query->setQuery( $this->searchContext->getQuery() ); - $highlight = $this->searchContext->getHighlight( $this->resultsType ); + $highlight = $this->searchContext->getHighlight( $resultsType ); if ( $highlight ) { $query->setHighlight( $highlight ); } @@ -703,10 +696,11 @@ * * @param \Elastica\Search[] $searches * @param ResultsType[] $resultsTypes Specific ResultType instances to use with $searches. Any - * search without a matching key in this array uses $this->resultsType. + * search without a matching key in this array uses context result type. * @return Status results from the query transformed by the resultsType */ protected function searchMulti( $searches, array $resultsTypes = [] ) { + $contextResultsType = $this->searchContext->getResultsType(); if ( $this->limit <= 0 && ! $this->returnQuery ) { if ( $this->returnResult ) { return Status::newGood( [ @@ -718,7 +712,7 @@ $this->searchContext->setResultsPossible( false ); $retval = []; foreach ( $searches as $key => $search ) { - $retval[$key] = $this->resultsType->createEmptyResult(); + $retval[$key] = $contextResultsType->createEmptyResult(); } return Status::newGood( $retval ); } @@ -797,12 +791,12 @@ // Wrap with caching if needed, but don't cache debugging queries $skipCache = $this->returnResult || $this->returnExplain; if ( $this->searchContext->getCacheTtl() > 0 && !$skipCache ) { - $work = function () use ( $work, $searches, $log, $resultsTypes ) { + $work = function () use ( $work, $searches, $log, $resultsTypes, $contextResultsType ) { $requestStats = MediaWikiServices::getInstance()->getStatsdDataFactory(); $cache = ObjectCache::getLocalClusterInstance(); $keyParts = []; foreach ( $searches as $key => $search ) { - $resultsType = isset( $resultsTypes[$key] ) ? $resultsTypes[$key] : $this->resultsType; + $resultsType = isset( $resultsTypes[$key] ) ? $resultsTypes[$key] : $contextResultsType; $keyParts[] = $search->getPath() . serialize( $search->getOptions() ) . serialize( $search->getQuery()->toArray() ) . @@ -875,7 +869,7 @@ // @todo error handling $retval[$key] = null; } else { - $resultsType = isset( $resultsTypes[$key] ) ? $resultsTypes[$key] : $this->resultsType; + $resultsType = isset( $resultsTypes[$key] ) ? $resultsTypes[$key] : $contextResultsType; $retval[$key] = $resultsType->transformElasticsearchResult( $this->searchContext, $resultSet -- To view, visit https://gerrit.wikimedia.org/r/384907 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ic818924f83699b1bc333d03499808d390b75faf2 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/CirrusSearch Gerrit-Branch: master Gerrit-Owner: Smalyshev <smalys...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits