|
| 1 | +5.0 Upgrade Guide |
| 2 | +################# |
| 3 | + |
| 4 | +.. warning:: |
| 5 | + CakePHP ElasticSearch 5.x requires CakePHP 5.2+, ElasticSearch 9.x, and PHP 8.1+. |
| 6 | + |
| 7 | +Requirements |
| 8 | +============ |
| 9 | + |
| 10 | +* CakePHP 5.2+ |
| 11 | +* ElasticSearch 9.x |
| 12 | +* Elastica 9.x (via ruflin/elastica) |
| 13 | +* PHP 8.1+ |
| 14 | + |
| 15 | +Breaking Changes |
| 16 | +================ |
| 17 | + |
| 18 | +Version 5.x includes several breaking changes to support ElasticSearch 9.x via |
| 19 | +the Elastica 9.x library upgrade, along with compatibility updates for the latest |
| 20 | +CakePHP 5.x features. |
| 21 | + |
| 22 | +* Connection configuration format has changed to use ``hosts`` array. |
| 23 | +* ``IndexRegistry`` class has been removed (was deprecated since 3.4.3). |
| 24 | +* Several deprecated methods have been removed. |
| 25 | +* Elastica API changes require updated client initialization. |
| 26 | + |
| 27 | +Configuration Changes |
| 28 | +===================== |
| 29 | + |
| 30 | +Connection Configuration Format |
| 31 | +-------------------------------- |
| 32 | + |
| 33 | +The recommended configuration format now uses a ``hosts`` array instead |
| 34 | +of separate ``host`` and ``port`` keys. This aligns with Elastica 9.x |
| 35 | +and provides better support for clustering. |
| 36 | + |
| 37 | +Legacy configuration format (still supported):: |
| 38 | + |
| 39 | + // in config/app.php |
| 40 | + 'Datasources' => [ |
| 41 | + 'elastic' => [ |
| 42 | + 'className' => 'Cake\ElasticSearch\Datasource\Connection', |
| 43 | + 'driver' => 'Cake\ElasticSearch\Datasource\Connection', |
| 44 | + 'host' => '127.0.0.1', |
| 45 | + 'port' => 9200, |
| 46 | + ], |
| 47 | + ] |
| 48 | + |
| 49 | +New configuration format (recommended):: |
| 50 | + |
| 51 | + // in config/app.php |
| 52 | + 'Datasources' => [ |
| 53 | + 'elastic' => [ |
| 54 | + 'className' => 'Cake\ElasticSearch\Datasource\Connection', |
| 55 | + 'driver' => 'Cake\ElasticSearch\Datasource\Connection', |
| 56 | + 'hosts' => ['127.0.0.1:9200'], |
| 57 | + ], |
| 58 | + ] |
| 59 | + |
| 60 | +For HTTPS endpoints:: |
| 61 | + |
| 62 | + 'hosts' => ['https://127.0.0.1:443'] |
| 63 | + |
| 64 | +Multiple hosts can be specified in the array:: |
| 65 | + |
| 66 | + 'hosts' => [ |
| 67 | + '127.0.0.1:9200', |
| 68 | + '127.0.0.1:9201', |
| 69 | + '127.0.0.1:9202', |
| 70 | + ] |
| 71 | + |
| 72 | +Deprecated Method Removals |
| 73 | +=========================== |
| 74 | + |
| 75 | +Several deprecated methods have been removed in this version. Update your code |
| 76 | +to use the replacement methods. |
| 77 | + |
| 78 | +QueryBuilder Methods |
| 79 | +-------------------- |
| 80 | + |
| 81 | +* ``QueryBuilder::and_()`` → use ``QueryBuilder::and()`` |
| 82 | +* ``QueryBuilder::or_()`` → use ``QueryBuilder::or()`` |
| 83 | + |
| 84 | +The underscore suffix is no longer needed as modern PHP allows these method names. |
| 85 | + |
| 86 | +Old code:: |
| 87 | + |
| 88 | + $query->where(function ($builder) { |
| 89 | + return $builder->and_( |
| 90 | + $builder->gt('views', 99), |
| 91 | + $builder->term('author.name', 'sally') |
| 92 | + ); |
| 93 | + }); |
| 94 | + |
| 95 | +New code:: |
| 96 | + |
| 97 | + $query->where(function ($builder) { |
| 98 | + return $builder->and( |
| 99 | + $builder->gt('views', 99), |
| 100 | + $builder->term('author.name', 'sally') |
| 101 | + ); |
| 102 | + }); |
| 103 | + |
| 104 | +Embedded Association Methods |
| 105 | +----------------------------- |
| 106 | + |
| 107 | +* ``Embedded::property()`` → use ``setProperty()`` / ``getProperty()`` |
| 108 | +* ``Embedded::entityClass()`` → use ``setEntityClass()`` / ``getEntityClass()`` |
| 109 | +* ``Embedded::indexClass()`` → use ``setIndexClass()`` / ``getIndexClass()`` |
| 110 | + |
| 111 | +Old code:: |
| 112 | + |
| 113 | + $association->property('comments'); |
| 114 | + $class = $association->entityClass(); |
| 115 | + $association->entityClass('App\Model\Document\Comment'); |
| 116 | + |
| 117 | +New code:: |
| 118 | + |
| 119 | + $association->setProperty('comments'); |
| 120 | + $class = $association->getEntityClass(); |
| 121 | + $association->setEntityClass('App\Model\Document\Comment'); |
| 122 | + |
| 123 | +Query Methods |
| 124 | +------------- |
| 125 | + |
| 126 | +* ``Query::repository()`` → use ``setRepository()`` |
| 127 | + |
| 128 | +Old code:: |
| 129 | + |
| 130 | + $query->repository($index); |
| 131 | + |
| 132 | +New code:: |
| 133 | + |
| 134 | + $query->setRepository($index); |
| 135 | + |
| 136 | +.. note:: |
| 137 | + ``Query::order()`` remains available as it's required by the |
| 138 | + ``Cake\Datasource\QueryInterface`` contract, but ``orderBy()`` is preferred. |
| 139 | + |
| 140 | +IndexRegistry Removal |
| 141 | +===================== |
| 142 | + |
| 143 | +The deprecated ``IndexRegistry`` class has been completely removed. Use ``IndexLocator`` |
| 144 | +or ``IndexLocatorAwareTrait`` instead. |
| 145 | + |
| 146 | +Old approach (no longer available):: |
| 147 | + |
| 148 | + use Cake\ElasticSearch\IndexRegistry; |
| 149 | + |
| 150 | + $articles = IndexRegistry::get('Articles'); |
| 151 | + IndexRegistry::flush(); |
| 152 | + |
| 153 | +New approach using IndexLocator directly:: |
| 154 | + |
| 155 | + use Cake\ElasticSearch\Datasource\IndexLocator; |
| 156 | + |
| 157 | + $locator = new IndexLocator(); |
| 158 | + $articles = $locator->get('Articles'); |
| 159 | + $locator->clear(); |
| 160 | + |
| 161 | +New approach using IndexLocatorAwareTrait:: |
| 162 | + |
| 163 | + use Cake\ElasticSearch\Datasource\IndexLocatorAwareTrait; |
| 164 | + |
| 165 | + class MyController extends Controller |
| 166 | + { |
| 167 | + use IndexLocatorAwareTrait; |
| 168 | + |
| 169 | + public function index() |
| 170 | + { |
| 171 | + $articles = $this->fetchIndex('Articles'); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | +Alternative using FactoryLocator:: |
| 176 | + |
| 177 | + use Cake\Datasource\FactoryLocator; |
| 178 | + |
| 179 | + $articles = FactoryLocator::get('ElasticSearch')->get('Articles'); |
| 180 | + |
| 181 | +Migration Steps |
| 182 | +=============== |
| 183 | + |
| 184 | +1. **Update Dependencies** |
| 185 | + |
| 186 | + Update your ``composer.json`` to require version 5.x:: |
| 187 | + |
| 188 | + composer require cakephp/elastic-search:^5.0 |
| 189 | + |
| 190 | +2. **Update Configuration (Optional but Recommended)** |
| 191 | + |
| 192 | + Consider updating your datasource configuration in ``config/app.php`` to use |
| 193 | + the ``hosts`` array format for better clustering support. The old ``host`` |
| 194 | + and ``port`` format still works for backward compatibility. |
| 195 | + |
| 196 | +3. **Update Code** |
| 197 | + |
| 198 | + Search your codebase for the deprecated methods and replace them: |
| 199 | + |
| 200 | + * Replace ``IndexRegistry`` usage with ``IndexLocator`` or ``IndexLocatorAwareTrait`` |
| 201 | + * Replace ``and_()`` with ``and()`` |
| 202 | + * Replace ``or_()`` with ``or()`` |
| 203 | + * Replace getter/setter methods on embedded associations |
| 204 | + * Replace ``repository()`` with ``setRepository()`` |
| 205 | + |
| 206 | +4. **Test ElasticSearch 9.x Compatibility** |
| 207 | + |
| 208 | + Ensure your ElasticSearch cluster is running version 9.x and test your |
| 209 | + application thoroughly. |
| 210 | + |
| 211 | +5. **Run Tests** |
| 212 | + |
| 213 | + Run your test suite to catch any compatibility issues:: |
| 214 | + |
| 215 | + vendor/bin/phpunit |
0 commit comments