/** * @param string $field * @return array * @throws \Exception */ public function analyseField($field) { try { $data = $this->{$field}; } catch (\Exception $exc) { throw new \Exception("Property {$field} does not exist for " . get_class()); } return SearchAnalyser::analyse($data); }
/** * Find the entities that have the appropriate keywords in their searchIndex * * @param string $searchText * @param int $maxResults * @return Collection found entities */ public function indexSearch($searchText, $maxResults) { // split the phrase into words $words = SearchAnalyser::analyse($searchText); if (!$words) { return []; } $query = $this->createQueryBuilder('o')->setMaxResults($maxResults); $parameters = []; foreach ($words as $k => $word) { $subquery = $query->getEntityManager()->createQueryBuilder()->select("i{$k}.keyword")->from($this->getSearchIndexClass(), "i{$k}")->where("i{$k}.object = o")->andWhere("i{$k}.keyword LIKE :search{$k}"); $query->andWhere($query->expr()->exists($subquery->getDql())); $parameters["search{$k}"] = $word . '%'; } $query->setParameters($parameters); //dump($query->getQuery()->getSQL()); $results = $query->getQuery()->execute(); return $results; }