src/Repository/ProfileRecommendationRepository.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\Profile\Profile;
  4. use App\Specification\ElasticSearch\Spec;
  5. use FOS\ElasticaBundle\Finder\TransformedFinder;
  6. use FOS\ElasticaBundle\HybridResult;
  7. use Symfony\Component\DependencyInjection\ContainerInterface;
  8. class ProfileRecommendationRepository
  9. {
  10.     private ?array $lastQuery null;
  11.     private array $scores = [];
  12.     private TransformedFinder $finder;
  13.     public function __construct(ContainerInterface $container)
  14.     {
  15.         $this->finder $container->get('fos_elastica.finder.recommendations.profile');
  16.     }
  17.     /**
  18.      * @return mixed[]
  19.      */
  20.     public function getByRecommendationWeightsData(Spec $mainQuery, array $filtersQuery, ?array $customSpecQuery$offset$limit): array
  21.     {
  22.         $query = [
  23.             'query' => [
  24.                 'function_score' => [
  25.                     'query' => [
  26.                         //... required conditions
  27.                     ],
  28.                     'functions' => [
  29.                         //... weight filters
  30.                     ],
  31.                     'max_boost' => 99999,
  32.                     'score_mode' => 'sum',
  33.                     'boost_mode' => 'sum',
  34.                     'min_score' => 0,
  35.                 ],
  36.             ],
  37.             'from' => $offset,
  38.             'size' => $limit,
  39.         ];
  40.         $mainQuery $mainQuery->toEsQueryObject();
  41.         if(key($mainQuery) == 'bool')
  42.             $query['query']['function_score']['query'] = $mainQuery;
  43.         else
  44.             $query['query']['function_score']['query']['bool']['must'] = $mainQuery;
  45.         $customQueryIsEmpty = isset($customSpecQuery['bool']) && (empty($customSpecQuery['bool']) || empty(current($customSpecQuery['bool'])));
  46.         if($customSpecQuery && false === $customQueryIsEmpty) {
  47.             $query['query']['function_score']['query']['bool']['must'][] = [$customSpecQuery];
  48.         }
  49.         $query['query']['function_score']['functions'] = $filtersQuery;
  50.         //print_r(json_encode($query, JSON_PRETTY_PRINT));die;
  51.         $this->lastQuery $query;
  52.         //$this->debugLog->checkQuery($this->lastQuery);
  53.         $result $this->finder->findHybrid($query);
  54.         $profiles = [];
  55.         foreach ($result as /** @var HybridResult $item */ $item) {
  56.             /** @var Profile $profile */
  57.             $profile $item->getTransformed();
  58.             $profiles[] = $profile;
  59.             $this->scores[$profile->getId()] = $item->getResult()->getScore();
  60.         }
  61.         return $profiles;
  62.     }
  63.     public function getLastQuery(): ?array
  64.     {
  65.         return $this->lastQuery;
  66.     }
  67.     public function getScores(): array
  68.     {
  69.         return $this->scores;
  70.     }
  71. }