@@ -17,7 +17,7 @@ class Elasticsearch implements ClientInterface
1717 /**
1818 * Elasticsearch Client instance
1919 *
20- * @var \Elasticsearch\Client
20+ * @var \Elasticsearch\Client[]
2121 */
2222 protected $ client ;
2323
@@ -53,10 +53,19 @@ public function __construct(
5353 $ config = $ this ->buildConfig ($ options );
5454 $ elasticsearchClient = \Elasticsearch \ClientBuilder::fromConfig ($ config , true );
5555 }
56- $ this ->client = $ elasticsearchClient ;
56+ $ this ->client [ getmypid ()] = $ elasticsearchClient ;
5757 $ this ->clientOptions = $ options ;
5858 }
5959
60+ private function getClient ()
61+ {
62+ $ pid = getmypid ();
63+ if (!isset ($ this ->client [$ pid ])) {
64+ $ config = $ this ->buildConfig ($ this ->clientOptions );
65+ $ this ->client [$ pid ] = \Elasticsearch \ClientBuilder::fromConfig ($ config , true );
66+ }
67+ return $ this ->client [$ pid ];
68+ }
6069 /**
6170 * Ping the Elasticsearch client
6271 *
@@ -65,7 +74,7 @@ public function __construct(
6574 public function ping ()
6675 {
6776 if ($ this ->pingResult === null ) {
68- $ this ->pingResult = $ this ->client ->ping (['client ' => ['timeout ' => $ this ->clientOptions ['timeout ' ]]]);
77+ $ this ->pingResult = $ this ->getClient () ->ping (['client ' => ['timeout ' => $ this ->clientOptions ['timeout ' ]]]);
6978 }
7079 return $ this ->pingResult ;
7180 }
@@ -110,7 +119,7 @@ private function buildConfig($options = [])
110119 */
111120 public function bulkQuery ($ query )
112121 {
113- $ this ->client ->bulk ($ query );
122+ $ this ->getClient () ->bulk ($ query );
114123 }
115124
116125 /**
@@ -122,7 +131,7 @@ public function bulkQuery($query)
122131 */
123132 public function createIndex ($ index , $ settings )
124133 {
125- $ this ->client ->indices ()->create ([
134+ $ this ->getClient () ->indices ()->create ([
126135 'index ' => $ index ,
127136 'body ' => $ settings ,
128137 ]);
@@ -136,7 +145,7 @@ public function createIndex($index, $settings)
136145 */
137146 public function deleteIndex ($ index )
138147 {
139- $ this ->client ->indices ()->delete (['index ' => $ index ]);
148+ $ this ->getClient () ->indices ()->delete (['index ' => $ index ]);
140149 }
141150
142151 /**
@@ -147,7 +156,7 @@ public function deleteIndex($index)
147156 */
148157 public function isEmptyIndex ($ index )
149158 {
150- $ stats = $ this ->client ->indices ()->stats (['index ' => $ index , 'metric ' => 'docs ' ]);
159+ $ stats = $ this ->getClient () ->indices ()->stats (['index ' => $ index , 'metric ' => 'docs ' ]);
151160 if ($ stats ['indices ' ][$ index ]['primaries ' ]['docs ' ]['count ' ] == 0 ) {
152161 return true ;
153162 }
@@ -172,7 +181,7 @@ public function updateAlias($alias, $newIndex, $oldIndex = '')
172181 $ params ['body ' ]['actions ' ][] = ['add ' => ['alias ' => $ alias , 'index ' => $ newIndex ]];
173182 }
174183
175- $ this ->client ->indices ()->updateAliases ($ params );
184+ $ this ->getClient () ->indices ()->updateAliases ($ params );
176185 }
177186
178187 /**
@@ -183,7 +192,7 @@ public function updateAlias($alias, $newIndex, $oldIndex = '')
183192 */
184193 public function indexExists ($ index )
185194 {
186- return $ this ->client ->indices ()->exists (['index ' => $ index ]);
195+ return $ this ->getClient () ->indices ()->exists (['index ' => $ index ]);
187196 }
188197
189198 /**
@@ -198,7 +207,7 @@ public function existsAlias($alias, $index = '')
198207 if ($ index ) {
199208 $ params ['index ' ] = $ index ;
200209 }
201- return $ this ->client ->indices ()->existsAlias ($ params );
210+ return $ this ->getClient () ->indices ()->existsAlias ($ params );
202211 }
203212
204213 /**
@@ -208,7 +217,7 @@ public function existsAlias($alias, $index = '')
208217 */
209218 public function getAlias ($ alias )
210219 {
211- return $ this ->client ->indices ()->getAlias (['name ' => $ alias ]);
220+ return $ this ->getClient () ->indices ()->getAlias (['name ' => $ alias ]);
212221 }
213222
214223 /**
@@ -267,7 +276,7 @@ public function addFieldsMapping(array $fields, $index, $entityType)
267276 foreach ($ fields as $ field => $ fieldInfo ) {
268277 $ params ['body ' ][$ entityType ]['properties ' ][$ field ] = $ fieldInfo ;
269278 }
270- $ this ->client ->indices ()->putMapping ($ params );
279+ $ this ->getClient () ->indices ()->putMapping ($ params );
271280 }
272281
273282 /**
@@ -279,7 +288,7 @@ public function addFieldsMapping(array $fields, $index, $entityType)
279288 */
280289 public function deleteMapping ($ index , $ entityType )
281290 {
282- $ this ->client ->indices ()->deleteMapping ([
291+ $ this ->getClient () ->indices ()->deleteMapping ([
283292 'index ' => $ index ,
284293 'type ' => $ entityType ,
285294 ]);
@@ -293,8 +302,7 @@ public function deleteMapping($index, $entityType)
293302 */
294303 public function query ($ query )
295304 {
296- $ params = array_merge ($ query , ['client ' => ['timeout ' => $ this ->clientOptions ['timeout ' ]]]);
297- return $ this ->client ->search ($ params );
305+ return $ this ->getClient ()->search ($ query );
298306 }
299307
300308 /**
@@ -305,6 +313,6 @@ public function query($query)
305313 */
306314 public function suggest ($ query )
307315 {
308- return $ this ->client ->suggest ($ query );
316+ return $ this ->getClient () ->suggest ($ query );
309317 }
310318}
0 commit comments