/** * We want all files (and maybe just videos) from video wiki and the current wiki * @see \Wikia\Search\QueryService\Select\Dismax\AbstractDismax::getQueryClausesString() * @return string */ protected function getQueryClausesString() { $config = $this->getConfig(); $queryClauses = []; $queryClauses[] = sprintf('+((wid:%d AND (%s)) OR wid:%d)', Video::VIDEO_WIKI_ID, $this->getTopicsAsQuery(), $config->getWikiId()); $queryClauses[] = Utilities::valueForField('ns', \NS_FILE); if ($config->getCombinedMediaSearchIsVideoOnly()) { $queryClauses[] = Utilities::valueForField('is_video', 'true'); } if ($config->getCombinedMediaSearchIsImageOnly()) { $queryClauses[] = Utilities::valueForField('is_image', 'true'); } return sprintf('(%s)', implode(' AND ', $queryClauses)); }
/** * @group Slow * @slowExecutionTime 0.10161 ms * @covers Wikia\Search\QueryService\Select\Dismax\OnWiki::registerFilterQueryForMatch */ public function testRegisterFilterQueryForMatch() { $mockConfig = $this->getMock('Wikia\\Search\\Config', array('hasArticleMatch', 'getArticleMatch', 'setFilterQuery')); $mockMatch = $this->getMockBuilder('Wikia\\Search\\Match\\Article')->disableOriginalConstructor()->setMethods(array('getResult'))->getMock(); $mockResult = $this->getMockBuilder('Wikia\\Search\\Result')->setMethods(array('getVar'))->getMock(); $dc = new Wikia\Search\QueryService\DependencyContainer(array('config' => $mockConfig)); $mockSelect = $this->getMockBuilder('Wikia\\Search\\QueryService\\Select\\Dismax\\OnWiki')->setConstructorArgs(array($dc))->setMethods(null)->getMock(); $mockConfig->expects($this->once())->method('hasArticleMatch')->will($this->returnValue(true)); $mockConfig->expects($this->once())->method('getArticleMatch')->will($this->returnValue($mockMatch)); $mockMatch->expects($this->once())->method('getResult')->will($this->returnValue($mockResult)); $mockResult->expects($this->once())->method('getVar')->with('id')->will($this->returnValue(123)); $mockConfig->expects($this->once())->method('setFilterQuery')->with(Wikia\Search\Utilities::valueForField('id', 123, array('negate' => true)), 'ptt'); $register = new ReflectionMethod('Wikia\\Search\\QueryService\\Select\\Dismax\\OnWiki', 'registerFilterQueryForMatch'); $register->setAccessible(true); $this->assertEquals($mockSelect, $register->invoke($mockSelect)); }
protected function generateArrayQuery($queryArray, $name, $values) { $q = ''; if (!empty($values)) { if (!is_array($values)) { $values = [$values]; } foreach ($values as $item) { if (!$item) { continue; } $q .= ($q ? ' OR ' : '') . Utilities::valueForField($name, $item); } if ($q) { $queryArray[] = ' ( ' . $q . ' ) '; } } return $queryArray; }
/** * Builds the string used with filter queries based on search config * @return string */ protected function getFilterQueryString() { $namespaces = []; foreach ($this->config->getNamespaces() as $ns) { $namespaces[] = Utilities::valueForField('ns', $ns); } $minArticleQuality = $this->config->getMinArticleQuality(); $filters = [sprintf('(%s)', implode(' OR ', $namespaces)), Utilities::valueForField('wid', $this->config->getCityId())]; if ($minArticleQuality) { $filters[] = Utilities::rangeIntValueField('article_quality_i', $minArticleQuality); } return implode(' AND ', $filters); }
protected function getTopArticles($wikiId, $lang) { return \WikiaDataAccess::cache(wfSharedMemcKey("CombinedSearchService", $wikiId, $lang), self::TOP_ARTICLES_CACHE_TIME, function () use($wikiId, $lang) { $timer = Time::start(["CombinedSearchService", "getTopArticles"]); $requestedFields = ["title", "url", "id", "score", "pageid", "lang", "wid", "article_quality_i", Utilities::field('html', $lang)]; $topArticlesMap = \DataMartService::getTopArticlesByPageview($wikiId, null, [NS_MAIN], false, self::TOP_ARTICLES_PER_WIKI + 1); $query = " +(" . Utilities::valueForField("wid", $wikiId) . ") "; $query .= " +( " . implode(" OR ", array_map(function ($x) { return Utilities::valueForField("pageid", $x); }, array_keys($topArticlesMap))) . ") "; $query .= " +(is_main_page:false) "; $searchConfig = new Config(); $searchConfig->setLimit(self::TOP_ARTICLES_PER_WIKI)->setQuery($query)->setPage(1)->setRequestedFields($requestedFields)->setDirectLuceneQuery(true)->setWikiId($wikiId); $resultSet = (new Factory())->getFromConfig($searchConfig)->search(); $currentResults = $resultSet->toArray($requestedFields); $articles = []; foreach ($currentResults as $article) { $articles[$article['pageid']] = $this->processArticle($article); if (sizeof($articles) >= self::TOP_ARTICLES_PER_WIKI) { break; } } $result = []; foreach ($topArticlesMap as $id => $a) { if (isset($articles[$id])) { $result[] = $articles[$id]; } } $timer->stop(); return $result; }); }
/** * @group Slow * @slowExecutionTime 0.09949 ms * @covers Wikia\Search\Utilities::valueForField */ public function testValueForField() { $this->assertEquals('-(wid:"123")^123', Utils::valueForField('wid', '123', array('negate' => true, 'boost' => 123, 'valueQuote' => '"'))); }
/** * Given a set of page IDs, deletes by query * @param array $documentIds * @return bool true */ public function deleteBatch(array $documentIds = array()) { $updateHandler = $this->getClient()->createUpdate(); foreach ($documentIds as $id) { $updateHandler->addDeleteQuery(Utilities::valueForField('id', $id)); } $updateHandler->addCommit(); try { $this->getClient()->update($updateHandler); } catch (\Exception $e) { $this->getLogger()->log(__METHOD__, implode(',', $documentIds), $e); } return true; }
/** * Constructor method * @param array $params */ public function __construct(array $params = []) { $dynamicFilterCodes = [self::FILTER_CAT_VIDEOGAMES => Utilities::valueForField('categories', 'Games', ['quote' => '"']), self::FILTER_CAT_ENTERTAINMENT => Utilities::valueForField('categories', 'Entertainment'), self::FILTER_CAT_LIFESTYLE => Utilities::valueForField('categories', 'Lifestyle')]; $this->filterCodes = array_merge($this->filterCodes, $dynamicFilterCodes); $this->configureByArray($params); }
/** * @group Slow * @slowExecutionTime 0.10235 ms * @covers Wikia\Search\QueryService\Select\Dismax\VideoEmbedTool::getBoostQueryString */ public function testGetBoostQueryString() { $service = $this->getMockBuilder(self::CLASSNAME)->disableOriginalConstructor()->setMethods(['getConfig', 'getTopicsAsQuery', 'getService'])->getMock(); $mwService = $this->getMock('Wikia\\Search\\MediaWikiService', ['getHubForWikiId', 'getWikiId']); $config = $this->getMock('Wikia\\Search\\Config', ['getQuery']); $query = $this->getMock('Wikia\\Search\\Query\\Select', ['getSolrQuery'], ['foo']); $service->expects($this->once())->method('getService')->will($this->returnValue($mwService)); $mwService->expects($this->once())->method('getWikiId')->will($this->returnValue(123)); $mwService->expects($this->once())->method('getHubForWikiId')->with(123)->will($this->returnValue('Entertainment')); $service->expects($this->once())->method('getConfig')->will($this->returnValue($config)); $config->expects($this->once())->method('getQuery')->will($this->returnValue($query)); $query->expects($this->once())->method('getSolrQuery')->will($this->returnValue('solrquery')); $service->expects($this->once())->method('getTopicsAsQuery')->will($this->returnValue('topicquery')); $this->assertEquals(sprintf('%s^150 AND (%s)^250 AND (html_media_extras_txt:(%s))^300', Utilities::valueForField('categories', 'Entertainment'), 'solrquery', 'topicquery'), $service->getBoostQueryString()); }
/** * Boosts results that match the the current hub category and wiki title * @see \Wikia\Search\QueryService\Select\Video::getBoostQueryString() * @return string */ public function getBoostQueryString() { $service = $this->getService(); return sprintf('%s^150 AND (%s)^250 AND (html_media_extras_txt:(%s))^300', Utilities::valueForField('categories', $service->getHubForWikiId($service->getWikiId())), $this->getConfig()->getQuery()->getSolrQuery(), $this->getTopicsAsQuery()); }