@@ -64,6 +64,12 @@ class Index
6464 'index.warmer.enabled ' ,
6565 ];
6666
67+ static protected $ allowedIndexCreateKeys = [
68+ 'settings ' ,
69+ 'aliases ' ,
70+ 'mappings '
71+ ];
72+
6773 /**
6874 * @var DynamicIndexSettingService
6975 * @Flow\Inject
@@ -88,6 +94,7 @@ class Index
8894 protected $ client ;
8995
9096 /**
97+ * These are the Flow "Settings" aka Configuration, NOT the index settings
9198 * @var array
9299 */
93100 protected $ settings ;
@@ -114,21 +121,14 @@ public function __construct(string $name, Client $client = null)
114121 }
115122
116123 /**
117- * Inject the settings
124+ * Inject the framework settings
118125 *
119126 * @param array $settings
120127 * @return void
121128 */
122129 public function injectSettings (array $ settings ): void
123130 {
124131 $ this ->settings = $ settings ;
125- $ indexSettings = $ this ->getSettings ();
126- if (!isset ($ indexSettings ['prefix ' ]) || empty ($ indexSettings ['prefix ' ])) {
127- return ;
128- }
129- // This is obviously a side effect but can only be done after injecting settings
130- // and it needs to be done as early as possible
131- $ this ->name = $ settings ['prefix ' ] . '- ' . $ this ->name ;
132132 }
133133
134134 /**
@@ -141,7 +141,7 @@ public function findType(string $typeName): AbstractType
141141 }
142142
143143 /**
144- * @param array <AbstractType> $types
144+ * @param array<AbstractType> $types
145145 * @return TypeGroup
146146 */
147147 public function findTypeGroup (array $ types ): TypeGroup
@@ -173,11 +173,11 @@ public function exists(): bool
173173 public function request (string $ method , string $ path = null , array $ arguments = [], $ content = null , bool $ prefixIndex = true ): Response
174174 {
175175 if ($ this ->client === null ) {
176- throw new ElasticSearchException ('The client of the index " ' . $ this ->name . '" is not set, hence no requests can be done. ' , 1566313883 );
176+ throw new ElasticSearchException ('The client of the index " ' . $ this ->prefixName () . '" is not set, hence no requests can be done. ' , 1566313883 );
177177 }
178178 $ path = ltrim ($ path ? trim ($ path ) : '' , '/ ' );
179179 if ($ prefixIndex === true ) {
180- $ path = '/ ' . $ this ->name . '/ ' . $ path ;
180+ $ path = '/ ' . $ this ->prefixName () . '/ ' . $ path ;
181181 } else {
182182 $ path = '/ ' . ltrim ($ path , '/ ' );
183183 }
@@ -191,22 +191,24 @@ public function request(string $method, string $path = null, array $arguments =
191191 */
192192 public function create (): void
193193 {
194- $ this ->request ('PUT ' , null , [], json_encode ($ this ->getSettings ()));
194+ $ indexConfiguration = $ this ->getConfiguration () ?? [];
195+ $ indexCreateObject = array_filter ($ indexConfiguration , static fn ($ key ) => in_array ($ key , self ::$ allowedIndexCreateKeys , true ), ARRAY_FILTER_USE_KEY );
196+ $ this ->request ('PUT ' , null , [], $ this ->encodeRequestBody ($ indexCreateObject ));
195197 }
196198
197199 /**
198200 * @return array|null
199201 */
200- protected function getSettings (): ?array
202+ protected function getConfiguration (): ?array
201203 {
202204 if ($ this ->client instanceof Client) {
203205 $ path = 'indexes. ' . $ this ->client ->getBundle () . '. ' . $ this ->settingsKey ;
204206 } else {
205207 $ path = 'indexes.default ' . '. ' . $ this ->settingsKey ;
206208 }
207209
208- $ settings = Arrays::getValueByPath ($ this ->settings , $ path );
209- return $ settings !== null ? $ this ->dynamicIndexSettingService ->process ($ settings , $ path , $ this ->getName ()) : $ settings ;
210+ $ cconfiguration = Arrays::getValueByPath ($ this ->settings , $ path );
211+ return $ cconfiguration !== null ? $ this ->dynamicIndexSettingService ->process ($ cconfiguration , $ path , $ this ->name ) : $ cconfiguration ;
210212 }
211213
212214 /**
@@ -215,15 +217,18 @@ protected function getSettings(): ?array
215217 */
216218 public function updateSettings (): void
217219 {
218- $ settings = $ this ->getSettings ();
220+ // we only ever need the settings path from all the settings.
221+ $ settings = $ this ->getConfiguration ()['settings ' ] ?? [];
219222 $ updatableSettings = [];
220223 foreach (static ::$ updatableSettings as $ settingPath ) {
221224 $ setting = Arrays::getValueByPath ($ settings , $ settingPath );
222225 if ($ setting !== null ) {
223226 $ updatableSettings = Arrays::setValueByPath ($ updatableSettings , $ settingPath , $ setting );
224227 }
225228 }
226- $ this ->request ('PUT ' , '/_settings ' , [], json_encode ($ updatableSettings ));
229+ if ($ updatableSettings !== []) {
230+ $ this ->request ('PUT ' , '/_settings ' , [], $ this ->encodeRequestBody ($ updatableSettings ));
231+ }
227232 }
228233
229234 /**
@@ -252,6 +257,11 @@ public function refresh(): Response
252257 * @return string
253258 */
254259 public function getName (): string
260+ {
261+ return $ this ->prefixName ();
262+ }
263+
264+ public function getOriginalName (): string
255265 {
256266 return $ this ->name ;
257267 }
@@ -273,4 +283,28 @@ public function setSettingsKey(string $settingsKey): void
273283 {
274284 $ this ->settingsKey = $ settingsKey ;
275285 }
286+
287+ /**
288+ * Prepends configured preset to the base index name
289+ *
290+ * @return string
291+ */
292+ private function prefixName (): string
293+ {
294+ $ indexConfiguration = $ this ->getConfiguration ();
295+ if (!isset ($ indexConfiguration ['prefix ' ]) || empty ($ indexConfiguration ['prefix ' ])) {
296+ return $ this ->name ;
297+ }
298+
299+ return $ indexConfiguration ['prefix ' ] . '- ' . $ this ->name ;
300+ }
301+
302+ private function encodeRequestBody (array $ content ): string
303+ {
304+ if ($ content === []) {
305+ return '' ;
306+ }
307+
308+ return json_encode ($ content );
309+ }
276310}
0 commit comments