/** * {@inheritdoc} */ public function findUser($token) { $query = new \Elastica\Query\Fuzzy(); $query->setField('username', $token); $finalQuery = new \Elastica\Query($query); $finalQuery->setFields(['username', 'profilePicture']); $finalQuery->setHighlight(['pre_tags' => ['<em style="color: #FF66FF;">'], 'post_tags' => ['</em>'], 'fields' => ['username' => ['fragment_size' => 200, 'number_of_fragments' => 1]]]); $users = $this->finder->search($finalQuery); $results = []; foreach ($users as $user) { $results[] = ['username' => $user->getData()['username'][0], 'profilePicture' => $user->getData()['profilePicture'][0], 'highlights' => $user->getHighlights()['username'][0]]; } return $results; }
/** * @internal **/ public static function _query($args, $pageIndex, $size, $sortByDate = false) { $query = new \Elastica\Query($args); $query->setFrom($pageIndex * $size); $query->setSize($size); $query->setFields(array('id')); Config::apply_filters('searcher_query', $query); try { $index = Indexer::_index(false); $search = new \Elastica\Search($index->getClient()); $search->addIndex($index); if (!$query->hasParam('sort')) { if ($sortByDate) { $query->addSort(array('post_date' => 'desc')); } else { $query->addSort('_score'); } } Config::apply_filters('searcher_search', $search, $query); $results = $search->search($query); return self::_parseResults($results); } catch (\Exception $ex) { error_log($ex); return null; } }
/** * @param Hackathon_ElasticgentoCore_Model_Resource_Client $searchAdapter * @param string $queryText * @param Mage_CatalogSearch_Model_Query $query * * @return Elastica\ResultSet */ protected function fetchSearchResultFromElasticSearch($searchAdapter, $queryText, $query) { $elasticQuery = new Elastica\Query(); $queryFuzzyLikeThis = new \Elastica\Query\FuzzyLikeThis(); $queryFuzzyLikeThis->addFields(Mage::helper('elasticgento_catalogsearch/data')->getSearchableElasticSearchFieldNames()); $queryFuzzyLikeThis->setLikeText($queryText); $elasticQuery->setQuery($queryFuzzyLikeThis); $returnFields = ['entity_id', 'name']; $elasticQuery->setFields($returnFields); return $searchAdapter->getIndex($query->getStoreId())->search($elasticQuery); }
/** * Update the indexes for other wiki that also store information about $titles. * @param Title[] $titles array of titles in other indexes to update */ public function updateOtherIndex($titles) { global $wgCirrusSearchWikimediaExtraPlugin; if (!isset($wgCirrusSearchWikimediaExtraPlugin['super_detect_noop'])) { $this->logFailure($titles, 'super_detect_noop plugin not enabled'); return; } $updates = array(); // Build multisearch to find ids to update $findIdsMultiSearch = new \Elastica\Multi\Search($this->connection->getClient()); $findIdsClosures = array(); foreach ($titles as $title) { foreach (OtherIndexes::getExternalIndexes($title) as $otherIndex) { if ($otherIndex === null) { continue; } $type = $this->connection->getPageType($otherIndex); $bool = new \Elastica\Filter\Bool(); // Note that we need to use the keyword indexing of title so the analyzer gets out of the way. $bool->addMust(new \Elastica\Filter\Term(array('title.keyword' => $title->getText()))); $bool->addMust(new \Elastica\Filter\Term(array('namespace' => $title->getNamespace()))); $filtered = new \Elastica\Query\Filtered(new \Elastica\Query\MatchAll(), $bool); $query = new \Elastica\Query($filtered); $query->setFields(array()); // We only need the _id so don't load the _source $query->setSize(1); $findIdsMultiSearch->addSearch($type->createSearch($query)); $findIdsClosures[] = function ($id) use($otherIndex, &$updates, $title) { $updates[$otherIndex][] = array('id' => $id, 'ns' => $title->getNamespace(), 'dbKey' => $title->getDBkey()); }; } } $findIdsClosuresCount = count($findIdsClosures); if ($findIdsClosuresCount === 0) { // No other indexes to check. return; } // Look up the ids and run all closures to build the list of updates $this->start("searching for {numIds} ids in other indexes", array('numIds' => $findIdsClosuresCount)); $findIdsMultiSearchResult = $findIdsMultiSearch->search(); try { $this->success(); for ($i = 0; $i < $findIdsClosuresCount; $i++) { $results = $findIdsMultiSearchResult[$i]->getResults(); if (count($results) === 0) { continue; } $result = $results[0]; call_user_func($findIdsClosures[$i], $result->getId()); } } catch (\Elastica\Exception\ExceptionInterface $e) { $this->failure($e); return; } if (!$updates) { return; } // These are split into a job per index so one index // being frozen doesn't block updates to other indexes // in the same update. foreach ($updates as $indexName => $actions) { $job = new Job\ElasticaWrite(reset($titles), array('clientSideTimeout' => false, 'method' => 'sendOtherIndexUpdates', 'arguments' => array($this->localSite, $indexName, $actions), 'cluster' => $this->writeToClusterName)); $job->run(); } }
$sm->flush(); //Execute a single lookup with no results echo PHP_EOL . "*** Single lookup with no results ***" . PHP_EOL; try { $user = $sm->find('Doctrine\\Tests\\Models\\Comments\\User', 'unknownid'); } catch (Doctrine\Search\Exception\NoResultException $exception) { print_r($exception->getMessage()); echo PHP_EOL; } //Search for comments with parent user. Because of the way ES returns //results, you have to explicitly ask for the _parent or _routing field if required. //On single document query e.g. find() the _parent field is returned by ES anyway. echo PHP_EOL . "*** Comments with parent user ***" . PHP_EOL; $query = new Elastica\Query(); $query->setFilter(new Elastica\Filter\HasParent(new Elastica\Filter\Term(array('username' => 'mrhash')), 'users')); $query->setFields(array('_source', '_parent')); $comments = $sm->getRepository('Doctrine\\Tests\\Models\\Comments\\Comment')->search($query); foreach ($comments as $comment) { print_r($comment); } //Paginated response with Pagerfanta library. In this case the Doctrine\Search\Query //wrapper provides a mechanism for specifying the query but it should be possible to //pass an Elastica query directly into a modified pagination adapter. echo PHP_EOL . "*** Pagerfanta paginated results ***" . PHP_EOL; $query = $sm->createQuery()->from('Doctrine\\Tests\\Models\\Comments\\Comment')->searchWith(new Elastica\Query())->setQuery(new Elastica\Query\MatchAll())->setFields(['_source', '_parent'])->setHydrationMode(Doctrine\Search\Query::HYDRATE_INTERNAL); $pager = new Pagerfanta\Pagerfanta(new PagerfantaAdapter($query)); $pager->setAllowOutOfRangePages(true); $pager->setMaxPerPage(1); $pager->setCurrentPage(2); $comments = $pager->getCurrentPageResults(); foreach ($comments as $comment) {