/** * Returns objects extracted from simple search * * @param User $user * @param string $entityClass * @param string $searchString * @param int $offset * @param int $maxResults * * @return array */ protected function getObjects(User $user, $entityClass, $searchString, $offset, $maxResults) { $objects = []; if (!$this->configManager->hasConfig($entityClass)) { return $objects; } $classNames = $this->shareScopeProvider->getClassNamesBySharingScopeConfig($entityClass); if (!$classNames) { return $objects; } $tables = []; foreach ($classNames as $className) { $metadata = $this->em->getClassMetadata($className); $tables[] = $metadata->getTableName(); } $searchResults = $this->indexer->simpleSearch($searchString, $offset, $maxResults, $tables); list($userIds, $buIds, $orgIds) = $this->getIdsByClass($searchResults, $user); if ($orgIds) { $organizations = $this->em->getRepository('OroOrganizationBundle:Organization')->getEnabledOrganizations($orgIds); $objects = array_merge($objects, $organizations); } if ($buIds) { $businessUnits = $this->em->getRepository('OroOrganizationBundle:BusinessUnit')->getBusinessUnits($buIds); $objects = array_merge($objects, $businessUnits); } if ($userIds) { $users = $this->em->getRepository('OroUserBundle:User')->findUsersByIds($userIds); $objects = array_merge($objects, $users); } return $objects; }
/** * @param string $expectedQuery * @param string $string * @param int $offset * @param int $maxResults * @param null $from * @param int $page * @dataProvider simpleSearchDataProvider */ public function testSimpleSearch($expectedQuery, $string, $offset = 0, $maxResults = 0, $from = null, $page = 0) { $searchResults = ['one', 'two', 'three']; $this->engine->expects($this->any())->method('search')->will($this->returnCallback(function (Query $query) use($searchResults) { return new Result($query, $searchResults, count($searchResults)); })); $result = $this->indexService->simpleSearch($string, $offset, $maxResults, $from, $page); $actualQuery = $result->getQuery()->getStringQuery(); if ($result->getQuery()->getFrom()) { $this->assertEquals($searchResults, $result->getElements()); $this->assertEquals(count($searchResults), $result->getRecordsCount()); } else { $this->assertEmpty($result->getElements()); $this->assertEquals(0, $result->getRecordsCount()); } $this->assertEquals($actualQuery, $expectedQuery); }
/** * @param string $search * @param int $firstResult * @param int $maxResults * @return array */ protected function searchIds($search, $firstResult, $maxResults) { $result = $this->indexer->simpleSearch($search, $firstResult, $maxResults, $this->entitySearchAlias); $elements = $result->getElements(); $ids = []; foreach ($elements as $element) { $ids[] = $element->getRecordId(); } return $ids; }
/** * {@inheritdoc} */ public function search($query, $page, $perPage) { $page = (int) $page > 0 ? (int) $page : 1; $perPage = (int) $perPage > 0 ? (int) $perPage : 10; $firstResult = ($page - 1) * $perPage; $perPage++; $items = []; $from = $this->getSearchAliases(); if ($from) { $items = $this->indexer->simpleSearch($query, $firstResult, $perPage, $from, $page)->getElements(); } $hasMore = count($items) === $perPage; if ($hasMore) { $items = array_slice($items, 0, $perPage - 1); } return ['results' => $this->convertItems($items), 'more' => $hasMore]; }
/** * * @param $query * @return \Oro\Bundle\SearchBundle\Query\Result */ public function getResults($query) { return $this->indexer->simpleSearch($query); }