/**
  * @param string $searchQuery
  * @param array $sort
  * @param int $offset
  * @param int $limit
  * @return array
  */
 public function findInIndex($searchQuery = "", $sort = [], $offset = 0, $limit = 0)
 {
     $client = $this->solr_client->getClient();
     $query = $client->createSelect();
     $query->setQuery($searchQuery);
     if (!empty($sort)) {
         foreach ($sort as $key => $value) {
             $query->addSort($key, $value);
         }
     }
     if (!empty($limit)) {
         $query->setRows($limit);
     }
     if (!empty($offset)) {
         $query->setStart($offset);
     }
     $resultset = $client->select($query);
     $result = [];
     /** @var Document $document */
     foreach ($resultset as $document) {
         $result[] = ArrayHelper::createArrayFromDocument($document);
     }
     return $result;
 }
 /**
  * @param Request $request
  * @return string
  */
 public function searchAction(Request $request)
 {
     /** @var \SearchBundle\Service\SearchService $service */
     $service = $this->container->get('solr_search');
     $postfixs = [DocumentHelper::POSTFIX => "Document", WaiverHelper::POSTFIX => "Waiver", UserHelper::POSTFIX => "User"];
     /** get document structure */
     $documentOverall = array_merge(UserHelper::$document, WaiverHelper::$document, DocumentHelper::$document);
     /** get solr alias document structure */
     $documentAlias = ArrayHelper::getSolrAlias($documentOverall);
     $searchQuery = "";
     $sources = [];
     $formData = $request->query->all();
     if (!empty($formData['search'])) {
         $searchQuery = trim($formData['search']);
         $searchQuery = strtolower($searchQuery);
     }
     $page = empty($formData['page']) ? 1 : $formData['page'];
     $sort = empty($formData['sort']) ? [] : $formData['sort'];
     if (!empty($sort)) {
         $mass = explode("_", $sort);
         $key = $mass[0];
         $sort = $mass[1];
         if (!empty($documentAlias[$key])) {
             $sort = [$documentAlias[$key] => $sort];
         }
     }
     $query = " ";
     $documents = [];
     /*
     if (!empty($sources)) {
         foreach ($sources as $postfix) {
             $documents[] = "{$documentAlias["document"]}:{$postfix}";
         }
         $query .= "( " . implode(" OR ", $documents) . ") AND ";
     } else {
         foreach ($postfixs as $postfix => $name) {
             $documents[] = "{$documentAlias["document"]}:{$postfix}";
             $sources[] = $postfix;
         }
         $query .= "( " . implode(" OR ", $documents) . ") AND ";
     }
     */
     /** build solr query */
     $query .= " ( {$documentAlias["title"]}:*{$searchQuery}*) ";
     $allResultSolr = $service->findInIndex($query, [], 0, 100000000);
     $resultSolr = $service->findInIndex($query, $sort, ($page - 1) * 10, 10);
     foreach ($postfixs as $documentPf => $name) {
         foreach ($resultSolr as $doc) {
             if ($documentPf == $doc["document"]) {
                 $ids[$documentPf][] = $doc["id"];
             }
         }
     }
     $documentResult = [];
     if (!empty($ids[DocumentHelper::POSTFIX])) {
         /** @var DocumentRepository $repoDoc */
         $repoDoc = $this->getDoctrine()->getRepository("DocumentBundle:Document");
         $documents = $repoDoc->getByIds($ids[DocumentHelper::POSTFIX]);
         /** @var \DocumentBundle\Entity\Document $doc */
         foreach ($documents as $doc) {
             $contentMarket = [];
             $documentMarket = $doc->getMarkets();
             if (!empty($documentMarket)) {
                 $contentMarket[] = implode(", ", $documentMarket);
             }
             $contentCarrier = [];
             $documentCarrier = $doc->getAirlines();
             if (!empty($documentCarrier)) {
                 $contentCarrier[] = implode(", ", $documentCarrier);
             }
             $documentResult[$doc->getId()] = ['title' => $doc->getName(), "document" => DocumentHelper::POSTFIX, "name" => DocumentHelper::NAME, "contentCarrier" => substr(implode(" ", $contentCarrier), 0, 200), "contentMarket" => substr(implode(" ", $contentMarket), 0, 200), "link" => $this->generateUrl('documentBundle_show_document', ["id" => $doc->getId()]), "author" => $doc->getCreatedBy() ? $doc->getCreatedBy()->getFullName() : "", "authorLink" => $doc->getCreatedBy() ? $this->generateUrl('userBundle_show_user', ["id" => $doc->getCreatedBy()->getId()]) : "", "update" => $doc->getCreated()];
         }
     }
     $waiverResult = [];
     if (!empty($ids[WaiverHelper::POSTFIX])) {
         /** @var DocumentRepository $repoDoc */
         $repoWaiver = $this->getDoctrine()->getRepository("WaiverBundle:Waiver");
         $waivers = $repoWaiver->getByIds($ids[WaiverHelper::POSTFIX]);
         /** @var \WaiverBundle\Entity\Waiver $waiver */
         foreach ($waivers as $waiver) {
             //$content = [];
             // $agency = $waiver->getAgency();
             // if (!empty($agency)) {
             //     $content[] = "Iata:  " . $agency->getId() . " " . $agency->getAccount();
             // }
             //
             // $waiverRecordLocator=$waiver->getRecordLocator();
             // if (!empty($waiverRecordLocator)) {
             //     $content[] = "Record Locator: " . $waiverRecordLocator;
             // }
             // $waiverStatus=$waiver->getStatus();
             // if (!empty($waiverStatus)) {
             //     $content[] = "Status: " . $this->get('translator')->trans($waiver->getTitleStatus(), [], 'Waivers');
             // }
             // $waiverCreator=$waiver->getCreator();
             // if (!empty($waiverCreator)) {
             //     $content[] = "Creator: " . $waiverCreator->getFullName();
             // }
             $contentCarrier = [];
             $waiverAirlines = [];
             $waiverCarrier = $waiver->getCarrier();
             if (!empty($waiverCarrier)) {
                 $contentCarrier[] = $waiverCarrier;
             }
             $contentMarket = [];
             $waiverMarket = [];
             $market = $waiver->getMarket();
             if (!empty($market)) {
                 $contentMarket[] = $market;
             }
             $userAirlines = $this->get('security.context')->getToken()->getUser()->getAirlines();
             $userMarket = $this->get('security.context')->getToken()->getUser()->getMarkets();
             $waiverAirlines[] = $waiverCarrier;
             $waiverMarket[] = $market;
             $resultCarrier = array_intersect($userAirlines, $waiverAirlines);
             $resultMarket = array_intersect($userMarket, $waiverMarket);
             if (!empty($resultCarrier) && !empty($resultMarket)) {
                 $waiverResult[$waiver->getId()] = ['title' => $this->get('translator')->trans("search.waiver.title", ["%id%" => "#" . $waiver->getId()], 'search'), "document" => WaiverHelper::POSTFIX, "name" => WaiverHelper::NAME, "contentCarrier" => substr(implode(" ", $contentCarrier), 0, 200), "contentMarket" => substr(implode(" ", $contentMarket), 0, 200), "link" => $this->generateUrl('waiverBundle_show_waiver', ["id" => $waiver->getId()]), "author" => $waiver->getCreator() ? $waiver->getCreator()->getFullName() : "", "authorLink" => $waiver->getCreator() ? $this->generateUrl('userBundle_show_user', ["id" => $waiver->getCreator()->getId()]) : "", "update" => $waiver->getOpenedDate()];
             }
         }
     }
     $userResult = [];
     if (!empty($ids[UserHelper::POSTFIX])) {
         /** @var UserRepository $repoUser */
         $repoUser = $this->getDoctrine()->getRepository("UserBundle:User");
         $users = $repoUser->getByIds($ids[UserHelper::POSTFIX]);
         /** @var RoleRepository $repoRole */
         $repoRole = $this->getDoctrine()->getRepository("UserBundle:Role");
         /** @var \UserBundle\Entity\User $user */
         foreach ($users as $user) {
             $userName = [];
             $content = [];
             $fullName = $user->getFullName();
             if (!empty($fullName)) {
                 $userName[] = "Name: " . $user->getFullName();
             }
             // $roles = $user->getRoles();
             // if (!empty($roles)) {
             //     $rolesName = [];
             //     /** @var Role $role */
             //     foreach ($roles as $role) {
             //         $roleObj = $repoRole->findOneByName($role);
             //         if ($roleObj) {
             //             $rolesName[] = $this->get('translator')->trans($roleObj->getTranslationKey(), [], 'FOSUserBundle');
             //         }
             //     }
             //
             //     $content[] = "Role: " . implode(", ", $rolesName);
             // }
             $contentMarket = [];
             $userMarket = [];
             $markets = $user->getMarkets();
             if (!empty($markets)) {
                 $contentMarket[] = implode(", ", $markets);
             }
             $userCarrier = [];
             $contentCarrier = [];
             $carrier = $user->getAirlines();
             if (!empty($carrier)) {
                 $contentCarrier[] = implode(", ", $carrier);
             }
             // $agencies=[];
             // $agency = $user->getAgencyObj();
             //
             // if (!empty($agency)) {
             //     $agencies[] = " Agency: " .$agency->getId() ." " .$agency->getAccount();
             // }
             //
             //
             $userAirlines = $this->get('security.context')->getToken()->getUser()->getAirlines();
             $userMarket = $this->get('security.context')->getToken()->getUser()->getMarkets();
             $resultCarrier = array_intersect($userAirlines, $carrier);
             $resultMarket = array_intersect($userMarket, $markets);
             //if(!empty($resultCarrier)&&!empty($resultMarket)){
             $userResult[$user->getId()] = ['title' => $user->getFullName(), "document" => UserHelper::POSTFIX, "name" => UserHelper::NAME, "contentCarrier" => substr(implode(" ", $contentCarrier), 0, 200), "contentMarket" => substr(implode(" ", $contentMarket), 0, 200), "link" => $this->generateUrl('userBundle_show_user', ["id" => $user->getId()]), "author" => $user->getFullName(), "authorLink" => $this->generateUrl('userBundle_show_user', ["id" => $user->getId()]), "update" => $user->getLastLogin()];
             //}
         }
     }
     $resultArray = [];
     foreach ($resultSolr as $solr) {
         switch ($solr["document"]) {
             case WaiverHelper::POSTFIX:
                 if (!empty($waiverResult[$solr["id"]])) {
                     $waiverResult[$solr["id"]]["score"] = $solr["score"];
                     $resultArray[] = $waiverResult[$solr["id"]];
                 }
                 break;
             case UserHelper::POSTFIX:
                 if (!empty($userResult[$solr["id"]])) {
                     $userResult[$solr["id"]]["score"] = $solr["score"];
                     $resultArray[] = $userResult[$solr["id"]];
                 }
                 break;
             case DocumentHelper::POSTFIX:
                 if (!empty($documentResult[$solr["id"]])) {
                     $documentResult[$solr["id"]]["score"] = $solr["score"];
                     $resultArray[] = $documentResult[$solr["id"]];
                 }
                 break;
         }
     }
     // Get the total amount of users in the database
     $itemCount = count($allResultSolr);
     // Get the total amount of pages to display
     $pageCount = ceil($itemCount / 10);
     $currentFiler = '';
     // Is there a next page?
     if ($itemCount > $page * 10) {
         $next = true;
     } else {
         $next = false;
     }
     // Is there a previous page?
     if ($page > 1) {
         $prev = true;
     } else {
         $prev = false;
     }
     unset($formData['page']);
     return $this->render('SearchBundle:Default:searchResult.html.twig', array('countResult' => count($allResultSolr), 'resultArray' => $resultArray, 'searchQuery' => $searchQuery, 'linkParam' => $this->generateUrl('searchBundle_searchResultPage', $formData), 'next' => $next, 'prev' => $prev, 'page' => $page, 'count' => $pageCount, 'filter' => $currentFiler, 'menu' => array('top' => 'Search')));
 }