/**
  * @inheritDoc
  */
 public function getRouteCollectionForRequest(Request $request)
 {
     if (!$this->manager) {
         throw new \Exception('Manager must be set to execute query to the elasticsearch');
     }
     $routeCollection = new RouteCollection();
     $requestPath = $request->getPathInfo();
     $search = new Search();
     $search->addQuery(new MatchQuery('url', $requestPath));
     $results = $this->manager->execute(array_keys($this->routeMap), $search, Result::RESULTS_OBJECT);
     try {
         foreach ($results as $document) {
             $type = $this->collector->getDocumentType(get_class($document));
             if (array_key_exists($type, $this->routeMap)) {
                 $route = new Route($document->url, ['_controller' => $this->routeMap[$type], 'document' => $document, 'type' => $type]);
                 $routeCollection->add('ongr_route_' . $route->getDefault('type'), $route);
             } else {
                 throw new RouteNotFoundException(sprintf('Route for type %s% cannot be generated.', $type));
             }
         }
     } catch (\Exception $e) {
         throw new RouteNotFoundException('Document is not correct or route cannot be generated.');
     }
     return $routeCollection;
 }
 /**
  * @param string $path
  *
  * @return array
  */
 protected function getCategoriesByPath($path)
 {
     $nodeIds = array_filter(explode('/', $path));
     if (count($nodeIds) === 0) {
         return [];
     }
     $search = new Search();
     $search->addFilter(new IdsQuery(array_values($nodeIds)));
     $results = $this->manager->execute(['AppBundle:Category'], $search);
     $nodes = [];
     foreach ($results as $document) {
         $nodes[$document->getId()] = $document;
     }
     $sortedNodes = [];
     foreach ($nodeIds as $nodeId) {
         if (isset($nodes[$nodeId])) {
             $sortedNodes[] = $nodes[$nodeId];
         }
     }
     return $sortedNodes;
 }
 /**
  * Executes given search.
  *
  * @param Search $search
  * @param string $resultsType
  *
  * @return DocumentIterator|RawIterator|array
  *
  * @throws \Exception
  */
 public function execute(Search $search, $resultsType = Result::RESULTS_OBJECT)
 {
     return $this->manager->execute([$this->type], $search, $resultsType);
 }