/**
  * Exports es index to provided file.
  *
  * @param Manager         $manager
  * @param string          $filename
  * @param array           $types
  * @param int             $chunkSize
  * @param OutputInterface $output
  */
 public function exportIndex(Manager $manager, $filename, $types, $chunkSize, OutputInterface $output)
 {
     $typesMapping = $manager->getMetadataCollector()->getMappings($manager->getConfig()['mappings']);
     $typesToExport = [];
     if ($types) {
         foreach ($types as $type) {
             if (!array_key_exists($type, $typesMapping)) {
                 throw new \InvalidArgumentException(sprintf('Type "%s" does not exist.', $type));
             }
             $typesToExport[] = $typesMapping[$type]['bundle'] . ':' . $typesMapping[$type]['class'];
         }
     } else {
         foreach ($typesMapping as $type => $typeConfig) {
             $typesToExport[] = $typeConfig['bundle'] . ':' . $typeConfig['class'];
         }
     }
     $repo = $manager->getRepository($typesToExport);
     $results = $this->getResults($repo, $chunkSize);
     $progress = new ProgressBar($output, $results->count());
     $progress->setRedrawFrequency(100);
     $progress->start();
     $metadata = ['count' => $results->count(), 'date' => date(\DateTime::ISO8601)];
     $writer = $this->getWriter($this->getFilePath($filename), $metadata);
     foreach ($results as $data) {
         $doc = array_intersect_key($data, array_flip(['_id', '_type', '_source', 'fields']));
         $writer->push($doc);
         $progress->advance();
     }
     $writer->finalize();
     $progress->finish();
     $output->writeln('');
 }
 /**
  * @param Category $document
  *
  * @return array
  */
 private function getChildrens($document)
 {
     $repository = $this->manager->getRepository('AppBundle:Category');
     /** @var Search $search */
     $search = $repository->createSearch();
     $search->addQuery(new TermQuery('parentKey', $document->id));
     $search->setSize(1000);
     $categories = $repository->execute($search);
     $tree = [];
     /** @var Category $category */
     foreach ($categories as $category) {
         $tree[$category->key]['document'] = $category;
         //            $tree[$category->key]['children'] = $this->getChildrens($category);
     }
     return $tree;
 }
 /**
  * Returns pre-built category tree.
  *
  * Output example:
  *
  *     [
  *         'abc' => [
  *             'document' => (object) Category,
  *         ],
  *         'def' => [
  *             'document' => (object) Category,
  *             'children' => [
  *                 'ghj' => [
  *                     'document' => (object) Category,
  *                 ],
  *             ],
  *         ],
  *     ]
  *
  * @return array
  */
 public function getCategoryTree()
 {
     $repository = $this->manager->getRepository('AppBundle:Category');
     $categories = $repository->findBy([]);
     return $this->buildTree($categories);
 }