/** * @param Command|SearchTracksCommand $command * * @return mixed * @throws TrackNotFoundException */ public function handle(Command $command) { $from = ($command->page() - 1) * TrackRepository::SIZE; $tracks = $this->trackRepository->findByCriteria(TrackRepositoryCriteria::instance($command->albumId(), $command->albumTitle(), $command->trackName(), $command->composer(), $command->page(), ['id' => 'asc'], TrackRepository::SIZE, $from)); return $this->searchTracksCommandResultDataTransformer->transform(SearchTracksCommandResult::instance($tracks)); }
public function findByCriteria(TrackRepositoryCriteria $criteria) { $boolQuery = new \Elastica\Query\BoolQuery(); if ($criteria->albumId()) { $query = new \Elastica\Query\Term(); $query->setParam('album.id', $criteria->albumId()); $boolQuery->addMust($query); } if ($criteria->albumTitle()) { $query = new \Elastica\Query\Match(); $query->setFieldQuery('album.title', $criteria->albumTitle()); $query->setFieldFuzziness('album.title', 2); $boolQuery->addMust($query); } if ($criteria->trackName()) { $query = new \Elastica\Query\Match(); $query->setFieldQuery('name', $criteria->trackName()); $query->setFieldFuzziness('name', 2); $boolQuery->addMust($query); } if ($criteria->composer()) { $query = new \Elastica\Query\Match(); $query->setFieldQuery('composer', $criteria->composer()); $query->setFieldFuzziness('composer', 2); $boolQuery->addMust($query); } $this->elasticaSearch->setQuery($boolQuery); $query = $this->elasticaSearch->getQuery(); $query->setSize($criteria->size()); $query->setFrom(($criteria->page() - 1) * $criteria->size()); $query->addSort(['name_not_analyzed' => ['order' => 'asc']]); return $this->buildEntities($this->elasticaSearch->search()->getResults()); }