| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the ONGR package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) NFQ Technologies UAB <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace ONGR\ElasticsearchBundle\Service; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Elasticsearch\ClientBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use ONGR\ElasticsearchBundle\Mapping\MetadataCollector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use ONGR\ElasticsearchBundle\Result\Converter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Psr\Log\LoggerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * Elasticsearch Manager factory class. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 21 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  | class ManagerFactory | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      * @var MetadataCollector | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     private $metadataCollector; | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * @var Converter | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     private $converter; | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      * @var LoggerInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     private $logger; | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |      * @var LoggerInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     private $tracer; | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |      * @param MetadataCollector $metadataCollector Metadata collector service. | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      * @param Converter         $converter         Converter service to transform arrays to objects and visa versa. | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |      * @param LoggerInterface   $tracer | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |      * @param LoggerInterface   $logger | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |     public function __construct($metadataCollector, $converter, $tracer = null, $logger = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         $this->metadataCollector = $metadataCollector; | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         $this->converter = $converter; | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         $this->tracer = $tracer; | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         $this->logger = $logger; | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |      * Factory function to create a manager instance. | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |      * @param string $managerName   Manager name. | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |      * @param array  $connection    Connection configuration. | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |      * @param array  $analysis      Analyzers, filters and tokenizers config. | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |      * @param array  $managerConfig Manager configuration. | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |      * @return Manager | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     public function createManager($managerName, $connection, $analysis, $managerConfig) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         foreach (array_keys($analysis) as $analyzerType) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             foreach ($connection['analysis'][$analyzerType] as $name) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 $connection['settings']['analysis'][$analyzerType][$name] = $analysis[$analyzerType][$name]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         unset($connection['analysis']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         if (!isset($connection['settings']['number_of_replicas'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             $connection['settings']['number_of_replicas'] = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         if (!isset($connection['settings']['number_of_shards'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             $connection['settings']['number_of_shards'] = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $mappings = $this->metadataCollector->getClientMapping($managerConfig['mappings']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $client = ClientBuilder::create(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         $client->setHosts($connection['hosts']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $client->setTracer($this->tracer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         if ($this->logger && $managerConfig['logger']['enabled']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             $client->setLogger($this->logger); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $indexSettings = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             'index' => $connection['index_name'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             'body' => array_filter( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                     'settings' => $connection['settings'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                     'mappings' => $mappings, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         $manager = new Manager( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             $managerName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             $managerConfig, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             $client->build(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             $indexSettings, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             $this->metadataCollector, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             $this->converter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $manager->setCommitMode($managerConfig['commit_mode']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $manager->setBulkCommitSize($managerConfig['bulk_size']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         return $manager; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 118 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 119 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 120 |  |  |  |