| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | namespace BEdita\ElasticSearch\Adapter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use BEdita\Core\Search\BaseAdapter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Cake\Database\Connection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Cake\Database\Expression\ComparisonExpression; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Cake\Database\Expression\IdentifierExpression; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Cake\Database\Schema\TableSchema; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Cake\Datasource\EntityInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Cake\Log\LogTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Cake\ORM\Locator\LocatorAwareTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Cake\ORM\Query; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Cake\ORM\Table; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Cake\Utility\Security; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Psr\Log\LogLevel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | class ElasticSearchAdapter extends BaseAdapter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     use LocatorAwareTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     use LogTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @inheritDoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     public function search(Query $query, string $text, array $options = [], array $config = []): Query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         return $this->buildQuery($query, $text, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @codeCoverageIgnore | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     public function indexResource(EntityInterface $entity, string $operation): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * Build elastic search query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @param string $text The search text | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param array $options The options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     protected function buildElasticSearchQuery(string $text, array $options): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * Build query and return it | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @param \Cake\ORM\Query $query The query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @param string $text The search text | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @param array $options The options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @return \Cake\ORM\Query | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 61 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     protected function buildQuery(Query $query, string $text, array $options): Query | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |         $results = $this->buildElasticSearchQuery($text, $options); | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         if (count($results) === 0) { | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |             // Nothing found. No results should be returned. Add a contradiction to the `WHERE` clause. | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |             return $query->where(new ComparisonExpression('1', '1', 'integer', '<>')); | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |         // Prepare temporary table with `id` and `score` from ElasticSearch results. | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |         $tempTable = $this->createTempTable($query->getConnection()); | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         $insertQuery = $tempTable->query()->insert(['id', 'score']); | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |         foreach ($results as $row) { | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |             $insertQuery = $insertQuery->values($row); | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |         $insertQuery->execute(); | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |         // Add a join with the temporary table to filter by ID and sort by relevance score. | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |         return $query | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |             ->innerJoin( | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |                 $tempTable->getTable(), | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |                 new ComparisonExpression( | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |                     new IdentifierExpression($tempTable->aliasField('id')), | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |                     new IdentifierExpression($query->getRepository()->aliasField('id')), | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |                     'integer', | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |                     '=' | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |                 ) | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |             ) | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |             ->orderDesc($tempTable->aliasField('score')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * Create a temporary table to store search results. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * The table is created with a `score` column to sort results by relevance. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * The table is dropped when the connection is closed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * Returns `null` if the table cannot be created. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @param \Cake\Database\Connection $connection The database connection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * @return \Cake\ORM\Table|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     protected function createTempTable(Connection $connection): ?Table | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $table = sprintf('elasticsearch_%s', Security::randomString(16)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         $schema = (new TableSchema($table)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             ->setTemporary(true) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             ->addColumn('id', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                 'type' => TableSchema::TYPE_INTEGER, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                 'length' => 11, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                 'unsigned' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                 'null' => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             ]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             ->addColumn('score', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |                 'type' => TableSchema::TYPE_FLOAT, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                 'null' => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             ]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             ->addConstraint( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                 'PRIMARY', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                 [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                     'type' => TableSchema::CONSTRAINT_PRIMARY, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                     'columns' => ['id'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                 ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             ->addIndex( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                 sprintf('%s_score_idx', str_replace('_', '', $table)), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                 [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                     'type' => TableSchema::INDEX_INDEX, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                     'columns' => ['score'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                 ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             // Execute SQL to create table. In MySQL the transaction is completely useless, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             // because `CREATE TABLE` implicitly implies a commit. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             $connection->transactional(function (Connection $connection) use ($schema): void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |                 foreach ($schema->createSql($connection) as $statement) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |                     $connection->execute($statement); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |             }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         } catch (Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             $this->log($e->getMessage(), LogLevel::CRITICAL); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         return (new Table(compact('connection', 'table', 'schema'))) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |             ->setPrimaryKey('id') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             ->setDisplayField('score'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 150 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 151 |  |  |  |